1

I've created an Asset Source and assigned my own field via the field layout.

Then, I created an Assets Field to display a single image on a page. Using my plugins twig extension I can query the image object like so:

{{ entry.featuredImage.first()|myTwigExt }}

Now, in the Twig extension I want to target the first instance of my field using its Type, not the Handle.

enter image description here

The reason for this is because the handle can be defined by the user, but the type name is always the same. I want to avoid the need to parse in the field handle along with the Twig Extension like this:

{{ entry.featuredImage.first()|myTwigExt('notThis') }}

If this is even possible, my ideal situation would have my Twig Extension looking something like this:

  public function getFilters() {
    return array(
      'myTwigExt' => new Twig_Filter_Method($this, 'myTwigExtension'),
    );
  }

  public function myTwigExtension($image, $param) {
   if ( $image->type('myCustomField') ) {
     // YAY!
     return $image->type('myCustomField')->someDataFromMyField;
   }

   if ( $image->$param ) {
     // "Boo!";
     return $image->$param->someDataFromMyField;
   }
  }

This is the cleanest way I can ask this question. Without going into too much detail about my plugin (which would just confuse the situation more).

1 Answer 1

2

I am not sure if I am understanding your question correctly, so here is my understanding before I give my solution:

You want to be able to access a field on an asset by it's field type rather than it's handle.

Given this understanding, here is my solution:

I have not been able to accomplish this using Craft relations. However, you can loop through the fields attached to the asset, and check their type. It is a little bit confusing as Craft stores the data for fields and field layouts across several tables.

An element has a field layout (FieldLayoutModel), and the layout then has fields (FieldLayoutFieldModels). Then each of those FieldLayoutFieldModels has fields (FieldModel) which are the actual fields we are looking for. Putting this together, we can loop through and check the field type like so:

    // get the fieldLayoutModel
    $fieldLayoutModel = $image->fieldLayout;

    // get array of FieldLayoutFieldModel
    $fieldLayoutFields = $fieldLayoutModel->fields;

    foreach($fieldLayoutFields as $fieldLayoutField){

        // get the field associated with the layout
        $field = $fieldLayoutField->field;
        if($field->type == 'myFieldType'){
            // do whatever
            break;
        }
    }

Hopefully this is what you were looking for!

4
  • Oh man, I could kiss you! When I tested this out and I was waiting for my page to refresh, I could feel my heart pumping... and then it just worked. Such a ball-ache, but very satisfying to see it can be done. Thank you very much 'kcolls'. Commented Nov 11, 2015 at 23:46
  • No problem! Good luck with your plugin!
    – kcolls
    Commented Nov 11, 2015 at 23:50
  • Bear in mind this will only get the first field of that type for the element.
    – Jay
    Commented Nov 12, 2015 at 9:52
  • That is true, but it can be easily modified to return all fields of that type.
    – kcolls
    Commented Nov 12, 2015 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.