Hello,
Plugin used:
I’ve managed to create and capture the tags when creating a new article. However, when I try to filter the article based on Tags, it shows an error:
Expected key slug not present in find('tagged') options argument.
In my controller:
use Cake\Utility\Hash;
...
public function index()
{
$query = $this->Articles->find('search', ['search' => $this->request->getQuery()])->contain(['Tags']);
$articles = $this->paginate($query)->toArray();
$categories = $this->Articles->Categories->find('list', ['limit' => 200]);
$tags = $this->Articles->Tagged->find()->distinct(['Tags.slug', 'Tags.label'])->contain(['Tags'])->toArray();
$tags = Hash::combine($tags, '{n}.tag.slug', '{n}.tag.label');
$this->set(compact('articles','categories','tags'));
}
In my model:
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('articles');
$this->setDisplayField('title');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->addBehavior('Tools.Slugged',
['label' => 'title', 'unique' => true, 'mode' => 'ascii', 'field' => 'slug']
);
$this->addBehavior('Search.Search');
$this->searchManager()
->value('title')
->value('featured')
->value('category_id')
->value('tag')
->add('search', 'Search.Like', [
'before' => true,
'after' => true,
'fieldMode' => 'OR',
'comparison' => 'LIKE',
'wildcardAny' => '*',
'wildcardOne' => '?',
'fields' => ['title'],
])
->callback('tag', [
'callback' => function (Query $query, array $args, $manager) {
// Here you would have to remap $args if key isn't the expected "tag"
$query->find('tagged', $args);
}
]);
//debug($callback);
//exit;
$this->addBehavior('Tags.Tag', ['taggedCounter' => false]);
}
Maybe there is something that I left in the configuration that causing this error but I cannot identify it. However, can somebody give some ideas to solve this error?
Thank you