2

I'm writing a plugin that works with Fields in various ways, one thing I need to do is get the category source groups available to a particular category field. I have the field's handle and id and can get it's FieldModel instance from that. But that fieldModel doesn't appear to have any information about what sources it is using.

So for example, I have a Category field named 'peopleClassification', and it has the category group 'peopleWhoWorkHere' as it's source set in the field settings page.

When I inspect the FieldModel object for the peopleClassification field, I'd expect to see the id or handle for the peopleWhoWorkHere group somewhere inside the object. I'm dumping the variables and checking the API docs, and I don't see a way to do that.

I thought that the CategoriesService might have a method for returning sources for a given field ID, but I couldn't find anything.

Does anyone have suggestions on where in the API I can connect these two? It's obviously possible, else Craft wouldn't work at all.

To clarify, I'm not working with specific entry objects here, I'm trying to match field handles to group handles.

@carlcs answer got me to the info I needed (thanks). Here's how I completed it.

$field->getFieldType()->getSettings() returns an array with various pieces of meta data including a source field with a value like group:42. That number is the category group ID I needed to create an instance of category group object.

list($type, $gid) = split(':', $field->getFieldType()->getSettings()['source']);

Then use $gid to load an instance of the Group from categories service

$categoryGroup = craft()->categories->getGroupById($gid);

$categoryGroup contained the info I needed, including the group's handle, which I used to create a list of categories.

1 Answer 1

1

From the FieldModel you need to get the FieldTypeModel and then its settings. Something like this:

$settings = $field–>getFieldType()->getSettings();

Your Answer

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

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