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 }}
  • What do you mean by "plugin's fields"? Are you referring to the plugin's settings? Or a custom fieldtype? – Mats Mikkel Rummelhoff Aug 15 '16 at 16:48
  • @MatsMikkelRummelhoff it is field associated with the plugin's custom attributes not associate with settings or a custom field type. – aran Aug 15 '16 at 17:59
  • 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 Aug 15 '16 at 18:07
  • I have updated my question. – aran Aug 15 '16 at 18:34

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.

  • The code is now tested and works. Thank you. – aran Aug 15 '16 at 21:02
  • Awesome... lucky guess! – Brad Bell Aug 15 '16 at 21:02

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.