3

Now that I have my custom field displaying in the Entries Table, how can I set up sources to view entries containing specific data from my custom field? My custom field holds several different values, but I want to have a filter set up to view specific things.

Note: this is being copied from the Craft Slack channel so we don't lose it

1 Answer 1

3

Setting up the sources in the sidebar isn't difficult. Add the modifyEntrySources()(link) hook to MyCustomPlugin.php.

public function modifyEntrySources(&$sources, $context)
{
    if ($context == 'index')
    {
        $sources[] = array('heading' => 'My Custom Heading');

        $sources['mypluginhandle:mysectionname'] = array(
            'label' => 'My Section Name',
            'criteria' => array('myPluginHandle' => 'SQL CODE HERE')
        );
    }
}

At this point you should have a new entry heading and source in the Entries Listing page. Clicking on it isn't going to filter your data as you may expect... yet.

Notice the 'criteria' => array('myPluginHandle' => 'SQL CODE HERE')? This is what you are going to use to filter the entries according to what you want to see. Everything that is in the value of the 'myPluginHandle' array will be passed over to the modifyElementsQuery() (link) method in the MyCustomFieldtype.php file. This is where you can build out your SQL statement accordingly.

public function modifyElementsQuery(DbCommand $query, $params)
{
    if ($params != null) {

        // whatever you need to do to dig in to your custom fields' data...
        // using the code above, $params would be equal to 'SQL CODE HERE'

        $query->where($params);
    }
}

This should be all you need to filter your entries by your custom field!

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.