1

So I have some entries and one of the fields is for a "hotline". The hotlines are all entries. The CMS part works, I can click the field and choose an entry from the hotlines channel, however this is where I'm stuck.

In my plugin code I cannot seem to figure out a way to actually get the hotline that was chosen.

I have a $center variable which is an EntryModel of the entry that has the field for "hotlines" attached to it, with the value set (according to the admin).

$center->hotline and $center->getFieldValue('hotline') just return an ElementCriteriaModel and when I inspect that object inside xdebug there is nothing there that would be usable in getting the actual hotline that's selected. No ID. No object. No nothing. Just information about the field it self, not the value.

What the heck am I doing wrong here? This is so "unintuitive" compared to the rest of craft!

1

This is the same as using craft.entries in Twig. To get the entries from a ElementCriteriaModel you need to call the find() method.

In a plugin you can do this like this:

$relatedEntries = $center->hotline->find();

The variable $relatedEntries will be an array of EntryModels if multiple entries are related in the field or a single EntryModel if there is a single entry related in the field.

| improve this answer | |
1

Nevermind.

Apparently I have to grab first() which will give me the selected entry.

I know I tried this before and it didn't work. I must have been in the wrong context or something.

$center->getFieldValue('hotline')->first(); or $center->hotline->first(); is what I needed.

| improve this answer | |

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.