When adding custom sources as per the docs https://craftcms.com/docs/plugins/hooks-reference#modifyEntrySources like so:

foreach($this->regions as $region)
    $sources[] = ['heading' => $region['title']];
    $sources['news:'.$region['id']] = [
        'label'    => 'News',
        'criteria' => ['relatedTo' => $region['id'], 'section' => 'news', 'editable' => true],
        'data'     => array('type' => SectionType::Channel, 'handle' => 'news'),
        'defaultSort' => array('postDate', 'desc'),
    $sources['notifications:'.$region['id']] = [
        'label'    => 'Notifications',
        'criteria' => ['relatedTo' => $region['id'], 'section' => 'notifications', 'editable' => true],
        'data'     => array('type' => SectionType::Channel, 'handle' => 'notifications'),
        'defaultSort' => array('postDate', 'desc'),

the headings are not being shown between the sources. Am I missing something?

UPDATE: Ok, I've managed to figure out that it is to do with ElementIndexSettings. In ElementIndexesService->getSources() is a check for sourceOrder in the settings...

// Should we output the sources in a custom order?
if (isset($settings['sourceOrder']))

If I reverse the logic to mimic there being no settings my headings are displayed as expected.

So my question has changed somewhat, firstly is this expected behaviour? If it is I'm only applying these sources for certain users, can I temporarily disable the index settings from within my plugin's init function?



So I have my headings displaying now but this is more of a flawed workaround than an answer.

Basically when the ElementIndexesService->getSources() function finds element index settings it then works through the sources ordering them and applying headings based on the settings.

As my sources are custom ones added by a plugin they don't exist in the settings. Because of this they are shuffled to the end of the list and the headings are removed.

My fix is to add them to the settings in the database. This can be done using...

$elementIndexesService = new ElementIndexesService();
$settings = $elementIndexesService->getSettings(ElementType::Entry);
// and
$elementIndexesService->setSettings(ElementType::Entry, $settings);

$settings consists of two sub arrays containing the sources and their order. By adding my custom sources to these everything appears in the entries index as planned.

The flaws are that I now need to maintain this whenever the entries that effect the sources are created or deleted. Also the index settings modal overwrites the settings in the database so again I have to maintain it if they are changed.

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.