Answers.Body: <p>This should do it for you:</p>
  
  <pre><code>$entry-&gt;$fieldHandle-&gt;last()-&gt;uri;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-03T01:24:09.927Z
Answers.LastActivityDate: 2015-07-03T01:24:09.927Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm trying to make a plugin more generally usable (so others could use it). For my own use, it uses a category field called 'mainCategory' but I'd like that the field handle could be specified in the plugin settings.</p>
  
  <p>What I imagine is:</p>
  
  <pre><code>$url = $entry-&gt;mainCategory-&gt;last()-&gt;uri;
  </code></pre>
  
  <p>becomes:</p>
  
  <pre><code>$plugin = craft()-&gt;plugins-&gt;getPlugin('myPlugin');
  $settings = $plugin-&gt;getSettings();
  $fieldHandle = $settings-&gt;categoryFieldHandle;
  
  $url = $entry-&gt;getField($fieldHandle)-&gt;last()-&gt;uri;
  </code></pre>
  
  <p>Any suggestions?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-07-02T16:46:42.760Z
Comments.Text: Wouldn't `$entry->$fieldHandle->last()->uri` work?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-07-02T18:03:55.807Z
Comments.Text: It does indeed. I had no idea you could put a variable after the `->`. Thank you.
Comments.UserDisplayName: kr37
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-07-03T01:23:50.867Z
Comments.Text: Cool... I'll go ahead and add that as an answer if you want to mark it as accepted.
Comments.UserDisplayName: Brad Bell
Tags: <plugin-development><entrymodel><entries-field>
Title: How to use a dynamically defined field handle in a plugin?
ThreadText: # How to use a dynamically defined field handle in a plugin?
  
  (Asked by: username_0 on 2015-07-02)
  
  I'm trying to make a plugin more generally usable (so others could use it). For my own use, it uses a category field called 'mainCategory' but I'd like that the field handle could be specified in the plugin settings.
  
  What I imagine is:
  
  ```
  $url = $entry->mainCategory->last()->uri;
  
  ```
  
  becomes:
  
  ```
  $plugin = craft()->plugins->getPlugin('myPlugin');
  $settings = $plugin->getSettings();
  $fieldHandle = $settings->categoryFieldHandle;
  
  $url = $entry->getField($fieldHandle)->last()->uri;
  
  ```
  
  Any suggestions?
  
  **Tags:** plugin-development, entrymodel, entries-field
  
  > **Comments:**
  > **username_1**: Wouldn't `$entry->$fieldHandle->last()->uri` work?  
  > **username_0**: It does indeed. I had no idea you could put a variable after the `->`. Thank you.  
  > **username_1**: Cool... I'll go ahead and add that as an answer if you want to mark it as accepted.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-07-03. Score: 3 | Accepted answer
  
  This should do it for you:
  
  ```
  $entry->$fieldHandle->last()->uri;
  
  ```
  
  
  ---
  
  