Answers.Body: <p>My solution, thanks to @carlcs:</p>
  
  <pre><code>public function modifyEntrySources(&amp;$sources, $context)
  {
      $sources[] = array('heading' =&gt; Craft::t('Themes'));
  
      $themes = array(
          'THEME_FOO' =&gt; 'themeFoo',
          'THEME_BAR' =&gt; 'themeBar'
      );
  
      $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
      $criteria-&gt;section = 'article';
      $articles = $criteria-&gt;find();
  
      foreach ($themes as $theme =&gt; $label)
      {
          // Loop through all articles and check if they contain the current theme.
          // If they do, add it to the ids array which we will later use to filter
          // just the articles with this theme.
          $ids = [];
          foreach ($articles as $article)
          {
              foreach ($article-&gt;theme as $articleTheme)
              {
                  if ($articleTheme == $themes[$theme])
                  {
                      array_push($ids, $article-&gt;id);
                      break;
                  }
              }
          }
  
          $sources['theme:' . $label] = array(
              'label' =&gt; Craft::t('Theme') . ': ' . Craft::t($theme),
              'criteria' =&gt; array(
                  'section' =&gt; 'article',
                  'id' =&gt; $ids
              )
          );
      }
  }
  </code></pre>
  
  <p>The PHP itself could be cleaned up, but this works for now.</p>
  
  <p>Categories would probably have been a simpler solution had I gone for that originally.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-03T14:57:27.793Z
Answers.LastActivityDate: 2016-03-03T14:57:27.793Z
Answers.UserDisplayName: Esseb
Body: <p>I have articles with a multi-select named <code>theme</code>.</p>
  
  <p>This works fine in a template:</p>
  
  <pre><code>{% set entries = craft.entries.section('article').search('theme:themeFoobar') %}
  &lt;p&gt;{{ entries | length }}&lt;/p&gt;
  </code></pre>
  
  <p>I'm working on a plugin to create a custom source containing only those articles that have <code>themeFoobar</code> selected in their multi-select, this is what I have:</p>
  
  <pre><code>public function modifyEntrySources(&amp;$sources, $context)
  {
    $sources[] = array('heading' =&gt; 'Themes');
  
    $sources['theme:foobar'] = array(
      'label' =&gt; 'Theme: Foobar',
      'criteria' =&gt; array(
        'section' =&gt; 'article',
        'search' =&gt; 'theme:themeFoobar'
      )
    );
  }
  </code></pre>
  
  <p>The result of this is that the source contains all articles, it looks like the <code>search</code> criteria is completely ignored.</p>
  
  <p>Is <code>search</code> not the correct criteria to use here? Is there another criteria I can use to find only the articles with <code>themeFoobar</code> selected in the multi-select?</p>
  
  <h2>Further information</h2>
  
  <p>The multi-select used to be just a dropdown. When it was a dropdown, this plugin worked fine:</p>
  
  <pre><code>public function modifyEntrySources(&amp;$sources, $context)
  {
    $sources[] = array('heading' =&gt; 'Themes');
  
    $sources['theme:foobar'] = array(
      'label' =&gt; 'Theme: Foobar',
      'criteria' =&gt; array(
        'section' =&gt; 'article',
        'theme' =&gt; 'themeFoobar'
      )
    );
  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-03-03T13:00:07.573Z
Comments.Text: Does it work if you change it to `'search' => 'theme:*themeFoobar*'`? The multi select stores the array of values as a JSON string to the database.
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-03-03T13:10:05.900Z
Comments.Text: @carlcs nope, same result. All articles are returned, the search criteria appears to have no effect.
Comments.UserDisplayName: Esseb
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-03-03T14:13:19.440Z
Comments.Text: Hey @esseb, the solutions I posted to this question should also work from PHP. [Search against a checkboxes field's options](http://craftcms.stackexchange.com/q/13190/125)
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-03-03T14:45:30.963Z
Comments.Text: @carlcs If you'd like, go ahead and write that as an answer and I'll accept it. I'll post my own final code later once I clean it up a bit.
Comments.UserDisplayName: Esseb
Tags: <plugin-development><search><sources>
Title: Using search criteria in modifyEntrySources plugin
ThreadText: # Using search criteria in modifyEntrySources plugin
  
  (Asked by: username_0 on 2016-03-03)
  
  I have articles with a multi-select named `theme`.
  
  This works fine in a template:
  
  ```
  {% set entries = craft.entries.section('article').search('theme:themeFoobar') %}
  <p>{{ entries | length }}</p>
  
  ```
  
  I'm working on a plugin to create a custom source containing only those articles that have `themeFoobar` selected in their multi-select, this is what I have:
  
  ```
  public function modifyEntrySources(&$sources, $context)
  {
    $sources[] = array('heading' => 'Themes');
  
    $sources['theme:foobar'] = array(
      'label' => 'Theme: Foobar',
      'criteria' => array(
        'section' => 'article',
        'search' => 'theme:themeFoobar'
      )
    );
  }
  
  ```
  
  The result of this is that the source contains all articles, it looks like the `search` criteria is completely ignored.
  
  Is `search` not the correct criteria to use here? Is there another criteria I can use to find only the articles with `themeFoobar` selected in the multi-select?
  
  ## Further information
  
  The multi-select used to be just a dropdown. When it was a dropdown, this plugin worked fine:
  
  ```
  public function modifyEntrySources(&$sources, $context)
  {
    $sources[] = array('heading' => 'Themes');
  
    $sources['theme:foobar'] = array(
      'label' => 'Theme: Foobar',
      'criteria' => array(
        'section' => 'article',
        'theme' => 'themeFoobar'
      )
    );
  }
  
  ```
  
  **Tags:** plugin-development, search, sources
  
  > **Comments:**
  > **username_1**: Does it work if you change it to `'search' => 'theme:*themeFoobar*'`? The multi select stores the array of values as a JSON string to the database.  
  > **username_0**: @username_1 nope, same result. All articles are returned, the search criteria appears to have no effect.  
  > **username_1**: Hey @username_0, the solutions I posted to this question should also work from PHP. [Search against a checkboxes field's options](http://craftcms.stackexchange.com/q/13190/125)  
  > **username_0**: @username_1 If you'd like, go ahead and write that as an answer and I'll accept it. I'll post my own final code later once I clean it up a bit.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2016-03-03. Score: 3 | Accepted answer
  
  My solution, thanks to @username_1:
  
  ```
  public function modifyEntrySources(&$sources, $context)
  {
      $sources[] = array('heading' => Craft::t('Themes'));
  
      $themes = array(
          'THEME_FOO' => 'themeFoo',
          'THEME_BAR' => 'themeBar'
      );
  
      $criteria = craft()->elements->getCriteria(ElementType::Entry);
      $criteria->section = 'article';
      $articles = $criteria->find();
  
      foreach ($themes as $theme => $label)
      {
          // Loop through all articles and check if they contain the current theme.
          // If they do, add it to the ids array which we will later use to filter
          // just the articles with this theme.
          $ids = [];
          foreach ($articles as $article)
          {
              foreach ($article->theme as $articleTheme)
              {
                  if ($articleTheme == $themes[$theme])
                  {
                      array_push($ids, $article->id);
                      break;
                  }
              }
          }
  
          $sources['theme:' . $label] = array(
              'label' => Craft::t('Theme') . ': ' . Craft::t($theme),
              'criteria' => array(
                  'section' => 'article',
                  'id' => $ids
              )
          );
      }
  }
  
  ```
  
  The PHP itself could be cleaned up, but this works for now.
  
  Categories would probably have been a simpler solution had I gone for that originally.
  
  
  ---
  
  