Tags
Tags copied to clipboard
Improve Quick Start Guide
After following the instructions in the Quick Start Guide, the tags field (e.g. in edit.ctp) is not auto-polulated with the tags.
How do you suggest improving it?
@jadb can you add screenshots, a picture is worth 1000 words
@mistaguy, screenshots of?
Agree with matthiasmoritz I've been spending about 3 hours so far going though the code trying to figure out how to display the tags once they're added.
@zhunt I have the same problem: I was able to add the tag but was not able to load it : any hint?
I've not tried the plugin but taking a look to the behavior it adds relation to the Tagged
model. So retrieving tags should be as easy as using contain
:
$this->Posts->find('all', ['contain' => ['Tagged.Tags']]);
This works:
$this->Posts->find('all', ['contain' => ['Tags']]);
and in your view simply loop through the tags:
<th><?= __('Tags') ?></th>
<td>
<?php foreach ($post->tags as $tag): ?>
<?= h($tag->label) ?>
<?php endforeach; ?>
</td>
I used this way to modify tags:
Controller
$delimiter = ','; // same as delimiter at TagBehavior
$tags = [];
foreach ($post->tags as $tag):
$tags[] = $tag->label;
endforeach;
$post->tags = implode($delimiter, $tags);
View
echo $this->Form->input('tags');
How to edit associated tags? In my edit.ctp, in the input for tags instead of seeing the tag name I see the whole object. For example I have 3 tags associated with a specific article: "test1", "test2" and "test3". When the form is rendered, in the input I get this:
{ "id": "789e9a86-a341-4401-a338-629f4f940dea", "namespace": null, "slug": "", "label": "test3", "counter": 2, "created": "2016-10-25T04:38:59+00:00", "modified": "2016-10-25T04:52:31+00:00", "tag_key": "test3", "_joinData": { "id": "7dee31c6-5949-4aed-82a0-cc043d1fd257", "tag_id": "789e9a86-a341-4401-a338-629f4f940dea", "fk_id": "5b2efebb-6823-496c-99f9-61f11fbe8d4a", "fk_table": "documents", "created": "2016-10-25T04:40:47+00:00", "modified": "2016-10-25T04:40:47+00:00" }} { "id": "e04e4b6e-e705-4dc0-8d30-dfdbc3e015d9", "namespace": null, "slug": "", "label": "test2", "counter": 2, "created": "2016-08-10T07:13:12+00:00", "modified": "2016-10-25T04:52:31+00:00", "tag_key": "test2", "_joinData": { "id": "b396dd1b-8ce2-4719-bd80-b8e443512b6b", "tag_id": "e04e4b6e-e705-4dc0-8d30-dfdbc3e015d9", "fk_id": "5b2efebb-6823-496c-99f9-61f11fbe8d4a", "fk_table": "documents", "created": "2016-10-25T04:39:22+00:00", "modified": "2016-10-25T04:39:22+00:00" }} { "id": "ca8bb2ca-ebe2-4ba0-bbb4-b96212cee718", "namespace": null, "slug": "", "label": "test1", "counter": 2, "created": "2016-08-10T07:13:12+00:00", "modified": "2016-10-25T04:52:31+00:00", "tag_key": "test1", "_joinData": { "id": "d9d93a45-0f55-4921-8335-16dec90f6059", "tag_id": "ca8bb2ca-ebe2-4ba0-bbb4-b96212cee718", "fk_id": "5b2efebb-6823-496c-99f9-61f11fbe8d4a", "fk_table": "documents", "created": "2016-10-25T04:39:22+00:00", "modified": "2016-10-25T04:39:22+00:00" }}
Any advise?
@virusvn your solution gives me the following warnings:
Warning (2): Invalid argument supplied for foreach() [CORE\src\ORM\Marshaller.php, line 760]
Warning (2): Invalid argument supplied for foreach() [CORE\src\ORM\Marshaller.php, line 641]
Suggestions to get rid of these?
@chrmina I didn't get any problems. Where did you get that errors (Request Save or Get)?
@virusvn the errors come just after saving. Here is my edit method:
public function edit($id = null)
{
$document = $this->Documents->get($id, [
'contain' => ['Tags']
]);
// This results in 2 warnings. Need to fix
$delimiter = ','; // same as delimiter at TagBehavior
$tags = [];
$alltags = $document->tags;
foreach ($alltags as $tag):
$tags[] = $tag->label;
endforeach;
$document->tags = implode($delimiter, $tags);
if ($this->request->is(['patch', 'post', 'put'])) {
$document = $this->Documents->patchEntity($document, $this->request->data);
if ($this->Documents->save($document)) {
$this->Flash->success(__('The document has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The document could not be saved. Please, try again.'));
}
}
$this->set(compact('document'));
}
@chrmina sorry for late reply,
My code is used for request is GET, not for save action.
For save the document, you don't need to implode tags array, because $document->tags is an array, not a string. So $document->tags = implode($delimiter, $tags);
is not correct.
`
public function edit($id = null){
$document = $this->Documents->get($id, [
'contain' => ['Tags']
]);
$delimiter = ','; // same as delimiter at TagBehavior
//$this->request->data['tags'] should be an array, if in case $this->request->data['tags'] is a string, must convert to array:
// $this->request->data['tags'] = explode($delimiter, $this->request->data['tags']));
if ($this->request->is(['patch', 'post', 'put'])) {
$document = $this->Documents->patchEntity($document, $this->request->data);
if ($this->Documents->save($document)) {
$this->Flash->success(__('The document has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The document could not be saved. Please, try again.'));
}
}
// I placed that code at this line, to convert tags array to string, for input box (I used Twitter Typeahead, so when we submit form '$this->request->data['tags']' is an array)
$tags = [];
$alltags = $document->tags;
foreach ($alltags as $tag):
$tags[] = $tag->label;
endforeach;
$document->tags = implode($delimiter, $tags);
$this->set(compact('document'));
}`
Sorry for have no-good explanation
@virusvn thank you for your reply. The problem was resolved by simply rearranging the code as below:
public function edit($id = null)
{
$document = $this->Documents->get($id, ['contain' => ['Tags']]);
if ($this->request->is(['patch', 'post', 'put'])) {
$document = $this->Documents->patchEntity($document, $this->request->data);
if ($this->Documents->save($document)) {
$this->Flash->success(__('The document has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The document could not be saved. Please, try again.'));
}
}
// Manage Tags
$delimiter = ','; // same as delimiter at TagBehavior
$tags = [];
$alltags = $document->tags;
foreach ($alltags as $tag):
$tags[] = $tag->label;
endforeach;
$document->tags = implode($delimiter, $tags);
$this->set(compact('document'));
}