1

I saw this older question but it does not appear to work in Craft >=3.x as far as I can tell.

I've got a structure setup with 3 top level pages and some subpages. In this case each top level page will have similarly named subpages.

In the entry listing view for that structure in the control panel if you search for a subpage (one that matches other subpage titles) you have no indication of what its parent is. Originally I thought this was a non-issue and tried to add a 'parent' column to the listing but that is not an option in the customize columns UI.

Here's some screen captures with annotations to better illustrate the issue and my thinking:

enter image description here

Maybe I need to flip the problem on its head and attack it from another direction altogether though?

2
  • This feels like you might be approaching your content model from the "wrong" direction (there's no such thing as "wrong" really, just different). I've seen lots of sites, both Craft and EE, where Structure hierarchies have been abused to hold data where the parent and child items are not the same type of thing - it has instead been used as a grouping/categorisation strategy. In those cases, a flat hierarchy with relationships generally works better and is much more flexible. If you could give more detail on the nature of your content model that'll help. Mar 9, 2022 at 18:09
  • The idea is to have multiple top level pages that function almost as 'sub-sites' that then have their own children. Each of these would have its own navigation based on it's subpages. Does that make sense? Mar 9, 2022 at 19:31

1 Answer 1

2

You can modify the available table attributes for element indexes using the event Element::EVENT_REGISTER_TABLE_ATTRIBUTES. You can register that hook in a custom module. The craft\base\Entry class already has a parent attribute, which will be empty for all top-level and non-structure entries. So all you have to do is to make this attribute available using the hook above:

use craft\elements\Entry;
use craft\events\RegisterElementTableAttributesEvent;
use yii\base\Event;

Event::on(
    Entry::class,
    Entry::EVENT_REGISTER_TABLE_ATTRIBUTES,
    function (RegisterElementTableAttributesEvent $event) {
        $event->tableAttributes['parent'] = [
            'label' => Craft::t('app', 'Parent'),
        ];
    }
);

Now the Parent column should be available in element indexes for entries. By default, the column will just display the parent's title in the column. If you want to modify that (for example, to display the parent's slug with a link to the parent similar to your screenshot), you can do so using the Element::EVENT_SET_TABLE_ATTRIBUTE_HTML event:

use craft\elements\Entry;
use craft\events\SetElementTableAttributeHtmlEvent;
use yii\base\Event;

Event::on(
    Entry::class,
    Entry::EVENT_SET_TABLE_ATTRIBUTE_HTML,
    function(SetElementTableAttributeHtmlEvent $event) {
        /** @var Entry $entry */
        $entry = $event->sender;

        if ($event->attribute != 'parent') return;
        if (!($parent = $entry->getParent())) return;

        $event->html = sprintf(
            '<a href="%1$s">%2$s</a>',
            $parent->getUrl(),
            $parent->slug
        );
    }
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.