Answers.Body: <p>I am not sure if I am understanding your question correctly, so here is my understanding before I give my solution:</p>
  
  <p>You want to be able to access a field on an asset by it's field type rather than it's handle.</p>
  
  <p>Given this understanding, here is my solution:</p>
  
  <p>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. </p>
  
  <p>An element has a field layout (<a href="https://buildwithcraft.com/classreference/models/FieldLayoutModel" rel="nofollow">FieldLayoutModel</a>), and the layout then has fields (<a href="https://buildwithcraft.com/classreference/models/FieldLayoutFieldModel" rel="nofollow">FieldLayoutFieldModels</a>). Then each of those FieldLayoutFieldModels has fields (<a href="https://buildwithcraft.com/classreference/models/FieldModel" rel="nofollow">FieldModel</a>) which are the actual fields we are looking for. Putting this together, we can loop through and check the field type like so:</p>
  
  <pre><code>    // get the fieldLayoutModel
      $fieldLayoutModel = $image-&gt;fieldLayout;
  
      // get array of FieldLayoutFieldModel
      $fieldLayoutFields = $fieldLayoutModel-&gt;fields;
  
      foreach($fieldLayoutFields as $fieldLayoutField){
  
          // get the field associated with the layout
          $field = $fieldLayoutField-&gt;field;
          if($field-&gt;type == 'myFieldType'){
              // do whatever
              break;
          }
      }
  </code></pre>
  
  <p>Hopefully this is what you were looking for!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-11T23:46:23.567Z
Answers.Text: 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'.
Answers.UserDisplayName: Mark Notton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-11T23:50:04.577Z
Answers.Text: No problem! Good luck with your plugin!
Answers.UserDisplayName: kcolls
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-12T09:52:38.023Z
Answers.Text: Bear in mind this will only get the first field of that type for the element.
Answers.UserDisplayName: Jay
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-12T15:06:52.243Z
Answers.Text: That is true, but it can be easily modified to return all fields of that type.
Answers.UserDisplayName: kcolls
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-09T18:42:16.330Z
Answers.LastActivityDate: 2015-11-09T18:42:16.330Z
Answers.UserDisplayName: kcolls
Body: <p>I've created an <a href="http://buildwithcraft.com/docs/assets" rel="nofollow noreferrer">Asset Source</a> and assigned my own field via the field layout. </p>
  
  <p>Then, I created an <a href="http://buildwithcraft.com/docs/assets-fields" rel="nofollow noreferrer">Assets Field</a> to display a single image on a page. Using my plugins twig extension I can query the image object like so:</p>
  
  <pre><code>{{ entry.featuredImage.first()|myTwigExt }}
  </code></pre>
  
  <p>Now, in the Twig extension I want to target the first instance of my field using its Type, not the Handle.</p>
  
  <p><a href="https://i.sstatic.net/Cx87s.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Cx87s.jpg" alt="enter image description here"></a></p>
  
  <p>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 <strong>avoid</strong> the need to parse in the field handle along with the Twig Extension like this:</p>
  
  <pre><code>{{ entry.featuredImage.first()|myTwigExt('notThis') }}
  </code></pre>
  
  <p>If this is even possible, my ideal situation would have my Twig Extension looking something like this: </p>
  
  <pre><code>  public function getFilters() {
      return array(
        'myTwigExt' =&gt; new Twig_Filter_Method($this, 'myTwigExtension'),
      );
    }
  
    public function myTwigExtension($image, $param) {
     if ( $image-&gt;type('myCustomField') ) {
       // YAY!
       return $image-&gt;type('myCustomField')-&gt;someDataFromMyField;
     }
  
     if ( $image-&gt;$param ) {
       // "Boo!";
       return $image-&gt;$param-&gt;someDataFromMyField;
     }
    }
  </code></pre>
  
  <p>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). </p>
  
Tags: <plugin-development><custom-fieldtype><twigextension>
Title: Is it possible to target a fieldtype by it's Type name, rather than the handle name?
ThreadText: # Is it possible to target a fieldtype by it's Type name, rather than the handle name?
  
  (Asked by: username_0 on 2015-11-08)
  
  I've created an [Asset Source](http://buildwithcraft.com/docs/assets) and assigned my own field via the field layout.
  
  Then, I created an [Assets Field](http://buildwithcraft.com/docs/assets-fields) 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](https://i.sstatic.net/Cx87s.jpg)](https://i.sstatic.net/Cx87s.jpg)
  
  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).
  
  **Tags:** plugin-development, custom-fieldtype, twigextension
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-11-09. Score: 2 | Accepted answer
  
  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](https://buildwithcraft.com/classreference/models/FieldLayoutModel)), and the layout then has fields ([FieldLayoutFieldModels](https://buildwithcraft.com/classreference/models/FieldLayoutFieldModel)). Then each of those FieldLayoutFieldModels has fields ([FieldModel](https://buildwithcraft.com/classreference/models/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!
  
  > **Comments:**
  > **username_0**: 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 'username_1'.  
  > **username_1**: No problem! Good luck with your plugin!  
  > **username_2**: Bear in mind this will only get the first field of that type for the element.  
  > **username_1**: That is true, but it can be easily modified to return all fields of that type.  
  
  ---
  
  