5

We have a channel called Articles with multiple entry types. Is it possible to filter the entries within Craft (on the Entries page) by entry type?

  • With "on the Entries page" you meant the Entries Index page in the CP, right? Forgot to ask this before trying to give an answer... – carlcs Oct 7 '15 at 16:57
  • Yup. The Entries Index page in the CP is exactly where I mean. My client has asked if this is possible as they want to filter the long Articles list by entry type. Is your answer based on this assumption? If so would I need to modify a core file? – Simon Leadbetter Oct 7 '15 at 17:07
  • A simple yet perfect solution. That worked a treat – thank you. – Simon Leadbetter Oct 7 '15 at 17:44
4

What you can do is to create additional entry sources using the modifyEntrySources hook.

This is a very simple example, that will add a heading and two sources to the end of the sources list (below your default Singles, Channels and Structure sources).

public function modifyEntrySources(&$sources, $context)
{
    $newSources = array(
        array('heading' => 'Post Formats'),
        array(
            'label' => 'Articles',
            'criteria' => array('section' => 'blog', 'type' => 'article')
        ),
        array(
            'label' => 'Links',
            'criteria' => array('section' => 'blog', 'type' => 'link')
        )
    );

    $sources = array_merge($sources, $newSources);
}

You could also get all entry types of a section with getEntryTypes() and add them programatically if you want.

| improve this answer | |
  • See comments above for more info on this – Simon Leadbetter Oct 7 '15 at 17:45
2

In case it is useful to anyone upgrading to Craft 2.5, I had to modify the modifyEntrySources to this:

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

    $sources['link:1'] = array(
        'label' => 'Articles',
        'criteria' => array('section' => 'blog', 'type' => 'article')
    );
    $sources['link:2'] = array(
        'label' => 'Links',
        'criteria' => array('section' => 'blog', 'type' => 'link')
    );

}

Note the need for unique indexes $sources['link:1']

| improve this answer | |
2

To enable sorting on Entry Types in the Section's index table, you can add this to a custom plugin's primary class:

public function modifyEntrySortableAttributes(&$attributes)
{
    $attributes['typeId'] = Craft::t('Entry Type');
}

Note that AFAIK there's no way to sort on Entry Type name, only the Entry Type ID.

If you don't want to write a custom plugin, I already wrote one called CP Sortable Custom Columns which will add Entry Type to the index table's sortable attributes automatically, provided the Entry Type has been added to and is visible in the Section's index table (for instance, via the Customizable Element Index feature).

| 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.