3

I'm working on a custom Craft 3 site, and I need to be able to configure the element indexes within the control panel for a few of my custom EntryTypes. There's two particular things I'd like to control (which may merit two separate answers):

  • I'd like to be add custom columns to the table. These columns do not correspond to fields on the Entry, but they're more of a "computed property" if that makes sense.
  • I'd also like to modify the query that is ran for fetching those entries. In my particular case, some entries should not show up based on which user is logged in, and some custom business logic.

Is there a way to do this? My current investigation with ElementIndexesController::actionGetElements (the API call that the CP appears to be making) hasn't panned out: I don't see any global events or view hooks that I could wire into.

3

For the second part of your question, look into Element Sources. You might be able to pull off something fancy in a module, where certain users have access to different sources.

For the first part of your question, look into the EVENT_REGISTER_TABLE_ATTRIBUTES and EVENT_SET_TABLE_ATTRIBUTE_HTML events. Here is a rough example of how it will look...

// Add index table attributes
Event::on(
    Entry::class,
    Element::EVENT_REGISTER_TABLE_ATTRIBUTES,
    static function(RegisterElementTableAttributesEvent $event) {

        $event->tableAttributes['computedColumn'] = ['label' => 'Computed Column'];
        
    }
);

// Modify index table display values
Event::on(
    Entry::class,
    Element::EVENT_SET_TABLE_ATTRIBUTE_HTML,
    static function(SetElementTableAttributeHtmlEvent $event) {

        /** @var Entry $entry */
        $entry = $event->sender;

        switch ($event->attribute) {
            case 'computedColumn': // How to identify a computed column
                $event->html = 'any valid HTML';
                break;
            case 'field:101': // How to identify a normal field
                $event->html = 'whatever you want it to be';
                break;
        }
        
    }
);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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