<p>If I'm following you correctly, seems like you could override your Model's <a href="https://craftcms.com/classreference/models/BaseModel#getAttribute-detail" rel="nofollow"><code>getAttribute</code></a> method to do something like this completely untested code:</p>

<pre><code>public function getAttribute($name, $flattenValue = false)
{
    if ($name == 'location' &amp;&amp; $flattenValue != false)
    {
        $criteria = craft()-&gt;elements-&gt;getCriteria('MyPlugin_MyElementType');
        $criteria-&gt;location = parent::getAttribute($name, $flattenValue);
        return $criteria;
    }

    return parent::getAttribute($name, $flattenValue);
}
</code></pre>

<p>If I'm not following you correctly, then that was pointless.</p>

CC BY-SA 3.0
2016-08-15T21:02:01.387Z
The code is now tested and works. Thank you.
a-am
CC BY-SA 3.0
2016-08-15T21:02:41.033Z
Awesome... lucky guess!
Brad Bell
CC BY-SA 3.0
2016-08-15T20:47:49.027Z
2016-08-15T22:32:42.367Z
2016-08-15T22:32:42.367Z
Brad Bell
<p>In one of my Plugin's CP templates I am using a element select field to grab elements from a custom element type.  This field saves an array of element ids to a specific record's attribute called "location".  How do you return an element criteria model instead of the array of ids when the model attribute is called via twig?</p>

<pre><code>// Model
protected function defineAttributes()
{
    return array_merge(parent::defineAttributes(), array(
        'location'          =&gt; Attributetype::Mixed,
        'specificLocation'  =&gt; AttributeType::String,
        'registration'      =&gt; AttributeType::Mixed,
    ));
}

// Front end template
{{ craft.pluginname.variable.location }}
</code></pre>

CC BY-SA 3.0
2016-08-15T16:48:28.890Z
What do you mean by "plugin's fields"? Are you referring to the plugin's settings? Or a custom fieldtype?
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-08-15T17:59:15.017Z
@MatsMikkelRummelhoff it is field associated with the plugin's custom attributes not associate with settings or a custom field type.
a-am
CC BY-SA 3.0
2016-08-15T18:07:22.557Z
Sorry, I might be dense but I'm not following - there's no such thing as "plugin attributes". It might help if you shared some code.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-08-15T18:34:59.640Z
I have updated my question.
a-am
<plugin-development><elementcriteriamodel>
Return element criteria model from attribute of a model
# Return element criteria model from attribute of a model

(Asked by: username_0 on 2016-08-15)

In one of my Plugin's CP templates I am using a element select field to grab elements from a custom element type.  This field saves an array of element ids to a specific record's attribute called "location".  How do you return an element criteria model instead of the array of ids when the model attribute is called via twig?
// Model
protected function defineAttributes()
{
    return array_merge(parent::defineAttributes(), array(
        'location'          => Attributetype::Mixed,
        'specificLocation'  => AttributeType::String,
        'registration'      => AttributeType::Mixed,
    ));
}

// Front end template
{{ craft.pluginname.variable.location }}

**Tags:** plugin-development, elementcriteriamodel
> **Comments:**
> **username_1**: What do you mean by "plugin's fields"? Are you referring to the plugin's settings? Or a custom fieldtype?  
> **username_0**: @MatsMikkelRummelhoff it is field associated with the plugin's custom attributes not associate with settings or a custom field type.  
> **username_1**: Sorry, I might be dense but I'm not following - there's no such thing as "plugin attributes". It might help if you shared some code.  
> **username_0**: I have updated my question.  

---

## 1 answers

### Answer by username_2 on 2016-08-15. Score: 2 | Accepted answer

If I'm following you correctly, seems like you could override your Model's getAttribute method to do something like this completely untested code:
public function getAttribute($name, $flattenValue = false)
{
    if ($name == 'location' && $flattenValue != false)
    {
        $criteria = craft()->elements->getCriteria('MyPlugin_MyElementType');
        $criteria->location = parent::getAttribute($name, $flattenValue);
        return $criteria;
    }

    return parent::getAttribute($name, $flattenValue);
}

If I'm not following you correctly, then that was pointless.

> **Comments:**
> **username_0**: The code is now tested and works. Thank you.  
> **username_2**: Awesome... lucky guess!  

---

