<p>Of course this is the expected behavior because you don't return an <code>ElementCriteriaModel</code> from your plugin.</p>

<p>You do <code>return $criteria-&gt;find();</code> so you'll return an array of entries but not an <code>ElementCriteriaModel</code> if you want to return the object instead of the entries you have to do <code>return $criteria</code>.</p>

<p>As soon as you use the <code>find()</code> method Craft builds a query based on your <code>ElementCriteriaModel</code>, executes it and returns you an array of elements or null.</p>

CC BY-SA 3.0
2018-02-21T22:17:48.897Z
2018-02-21T22:17:48.897Z
Robin Schambach
<p>I'm outputting an element criteria model in a Craft 2 plugin of some entries in a channel.  I noticed that when I fetch the entries the standard template way </p>

<pre><code>{% set entries = craft.entries.section('channelHandle') %}
</code></pre>

<p>I am then able to do something like the following to refine the number of entries with the lightswitch turned on: </p>

<pre><code>{% set refinedEntries = entries.customLightswitchField(1)|length %}
</code></pre>

<p>However, when I fetch my entries in my plugin, output them into the template, then try refining it as above:</p>

<pre><code>// plugin
$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'channelHandle';
$entries = $criteria-&gt;find();
return $entries;

// template
{% set entries = craft.myPlugin.fetch() %}
{% set refinedEntries = entries.customLightswitchField(1)|length %}
</code></pre>

<p>...I then get an <code>Array to string conversion</code> PHP notice.  I'm also not able to use things like <code>.first()</code> like I normally would.</p>

<p>Is this expected behavior?  Am I missing something when returning an element criteria model through a plugin?</p>

<plugin-development><elementcriteriamodel>
Returning ElementCriteriaModel of entries via plugin
# Returning ElementCriteriaModel of entries via plugin

(Asked by: username_0 on 2018-02-21)

I'm outputting an element criteria model in a Craft 2 plugin of some entries in a channel.  I noticed that when I fetch the entries the standard template way 
{% set entries = craft.entries.section('channelHandle') %}

I am then able to do something like the following to refine the number of entries with the lightswitch turned on: 
{% set refinedEntries = entries.customLightswitchField(1)|length %}

However, when I fetch my entries in my plugin, output them into the template, then try refining it as above:
// plugin
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'channelHandle';
$entries = $criteria->find();
return $entries;

// template
{% set entries = craft.myPlugin.fetch() %}
{% set refinedEntries = entries.customLightswitchField(1)|length %}

...I then get an Array to string conversion PHP notice.  I'm also not able to use things like .first() like I normally would.
Is this expected behavior?  Am I missing something when returning an element criteria model through a plugin?

**Tags:** plugin-development, elementcriteriamodel

---

## 1 answers

### Answer by username_1 on 2018-02-21. Score: 2 | Accepted answer

Of course this is the expected behavior because you don't return an `ElementCriteriaModel` from your plugin.

You do `return $criteria->find();` so you'll return an array of entries but not an `ElementCriteriaModel` if you want to return the object instead of the entries you have to do `return $criteria`.

As soon as you use the `find()` method Craft builds a query based on your `ElementCriteriaModel`, executes it and returns you an array of elements or null.


---

