I have a custom fieldtype for Craft which stores its value as an array of Models using AttributeType::Mixed. See below for an example of the FieldType and Model setup
class MyCustomFieldType extends BaseFieldType
{
public function defineContentAttribute()
{
return AttributeType::Mixed;
}
}
class MyCustomModel extends BaseModel
{
protected function defineAttributes()
{
return array(
'url' => AttributeType::String,
'category' => AttributeType::String
);
}
}
Then when the value is persisted, it's saved as an array, which Craft natively saves as JSON
$values = [new MyCustomModel(), new MyCustomModel(), ...];
To display the value in Twig I can do something like
{% for item in entry.myCustomField %}
{{ item.category }}
{% endfor %}
My question is, how can I only display the elements in my field which have a specific attribute value? For example, if I wanted to display only those items with a category of main, where 'category' is an attribute on my model?
{% for item in entry.myCustomField.category('main') %}
{{ item.category }}
{% endfor %}