If I have a tag field querying some pages:
filters:
options: query
type: tags
query:
fetch: site.index.template('tag')
text: "{{ page.title }}"
value: "{{ page.slug }}"
My users have access the pages using the tag template, and can rename and deleted them. If a tag has been selected in the tag field, and the user then deletes the tag (page), the tag does not get deleted from the pages where this tag is selected. Is there a built in way to solve this?
One idea, hook into page.delete:before, find all pages where the tag is selected, and then remove the tag.
Thanks
Well, you can do that but the question is if this is really necessary. If you convert the field to pages, the non-existing page will not be included.
The non-existing page still appears in the tag field, which I don’t really like from a usability / logical point of view.
True, in a tags field they don’t disappear automatically, they only do in a pages field (although even there they are not deleted from the content file).
If anyone else needs this, here’s how you can do it with the page.delete.before hook:
'page.delete:before' => function ($page) {
$template = $page->intendedTemplate()->name();
// If template name matches
if ($template === 'tag') {
// Get the tag uid
$tag = $page->uid();
// Find all pages with the tag in the filters field
$pages = site()->find('veranstaltungen', 'ausstellungen')->children()->filterBy('filters', $tag, ',');
if ($pages) {
// For each page
foreach ($pages as $tP) {
// Split the filters field into and array
$tF = $tP->filters()->split(',');
// Find the key (index) of the tag
$key = array_search($tag, $tF, true);
// Remove the index
if ($key !== false) {
unset($tF[$key]);
}
// Join the array back into a string and save the field
$tP->update(['filters' => implode(', ', $tF)]);
}
}
}
},