Answers.Body: <p>So I have my headings displaying now but this is more of a flawed workaround than an answer.</p>
  
  <p>Basically when the <code>ElementIndexesService-&gt;getSources()</code> function finds element index settings it then works through the sources ordering them and applying headings based on the settings.</p>
  
  <p>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.</p>
  
  <p>My fix is to add them to the settings in the database. This can be done using... </p>
  
  <pre><code>$elementIndexesService = new ElementIndexesService();
  $settings = $elementIndexesService-&gt;getSettings(ElementType::Entry);
  // and
  $elementIndexesService-&gt;setSettings(ElementType::Entry, $settings);
  </code></pre>
  
  <p><code>$settings</code> 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.</p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-21T22:53:22.773Z
Answers.LastActivityDate: 2016-09-21T22:53:22.773Z
Answers.UserDisplayName: fcb
Body: <p>When adding custom sources as per the docs <a href="https://craftcms.com/docs/plugins/hooks-reference#modifyEntrySources" rel="nofollow">https://craftcms.com/docs/plugins/hooks-reference#modifyEntrySources</a> like so:</p>
  
  <pre><code>foreach($this-&gt;regions as $region)
  {
      $sources[] = ['heading' =&gt; $region['title']];
      $sources['news:'.$region['id']] = [
          'label'    =&gt; 'News',
          'criteria' =&gt; ['relatedTo' =&gt; $region['id'], 'section' =&gt; 'news', 'editable' =&gt; true],
          'data'     =&gt; array('type' =&gt; SectionType::Channel, 'handle' =&gt; 'news'),
          'defaultSort' =&gt; array('postDate', 'desc'),
      ];
      $sources['notifications:'.$region['id']] = [
          'label'    =&gt; 'Notifications',
          'criteria' =&gt; ['relatedTo' =&gt; $region['id'], 'section' =&gt; 'notifications', 'editable' =&gt; true],
          'data'     =&gt; array('type' =&gt; SectionType::Channel, 'handle' =&gt; 'notifications'),
          'defaultSort' =&gt; array('postDate', 'desc'),
      ];
  }
  </code></pre>
  
  <p>the headings are not being shown between the sources. Am I missing something?</p>
  
  <p>UPDATE: Ok, I've managed to figure out that it is to do with ElementIndexSettings. In <code>ElementIndexesService-&gt;getSources()</code> is a check for <code>sourceOrder</code> in the settings...</p>
  
  <pre><code>// Should we output the sources in a custom order?
  if (isset($settings['sourceOrder']))
  ...
  </code></pre>
  
  <p>If I reverse the logic to mimic there being no settings my headings are displayed as expected.</p>
  
  <p>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 <code>init</code> function?</p>
  
  <p>Thanks.</p>
  
Tags: <plugin-development><entries><control-panel>
Title: Headings Not Shown When Adding Custom Source
ThreadText: # Headings Not Shown When Adding Custom Source
  
  (Asked by: username_0 on 2016-09-20)
  
  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?
  
  Thanks.
  
  **Tags:** plugin-development, entries, control-panel
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2016-09-21. Score: 1
  
  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.
  
  
  ---
  
  