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;


$plugin = craft()->plugins->getPlugin('myPlugin');
$settings = $plugin->getSettings();
$fieldHandle = $settings->categoryFieldHandle;

$url = $entry->getField($fieldHandle)->last()->uri;

Any suggestions?

  • Wouldn't $entry->$fieldHandle->last()->uri work? – Brad Bell Jul 2 '15 at 16:46
  • It does indeed. I had no idea you could put a variable after the ->. Thank you. – kr37 Jul 2 '15 at 18:03
  • Cool... I'll go ahead and add that as an answer if you want to mark it as accepted. – Brad Bell Jul 3 '15 at 1:23

This should do it for you:


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.