2

I’m trying to create a plugin that relies on the values of a Matrix field in the same entry. Knowing that a single entry type could have more than 1 Matrix field, I think I need a way to select which Matrix field to pull from in my field settings. Any thoughts?

3

Your plugin will need a few things.

  • A FieldType
  • FieldType Settings using defineSettings() and getSettingsHtml() method
  • A template that will render your settings which will be referenced in the craft()->templates->render function of the getSettingsHtml() method

On the settings template, I believe you will need two dropdowns. The first, will just be a dropdown of available Sections. Upon selecting the section, you would update the second Dropdown to have a list of the Matrix fields within that section, or a message saying that no Matrix fields we're available. Or perhaps, you can just sort out which Sections have Matrix fields to start off with, and only display those Sections in the dropdown.

I'm not sure if there is a more efficient way to just grab Matrix fields, but here are some functions that may help you get to what you need:

Get all available Sections

$sections = craft()->sections->getAllSections()

Get a specific section by ID

$section = craft()->sections->getSectionById(1);

Get the FieldLayoutFieldModels attached to a Section (Note Fields are not related to the Section directly, they are related to a FieldLayout, which is related to an EntryType, which is related to your Section). In the example below, I have assumed you are grabbing the first EntryType using getEntryTypes()[0] however in your code, you'll likely have to do a bit more logic to make sure you're getting what you want:

$fields = $section->getEntryTypes()[0]->getFieldLayout()->getFields();

Once you have the FieldLayoutFieldModels, you can access each FieldModel using the getField() method and check which type of field type it is:

foreach ($fields as $field) 
{
  $field = $field->getField();          

  if ($field->type == 'Matrix') 
  {
    // Build an array of the options for your Matrix fields dropdown
  }             
}

Once you build an array of your options, you can hand them off to your fields settings template in the getSettingsHtml() method and loop through and output them in your template file. The doc pages referenced at the beginning of this post go over that in more detail.

return craft()->templates->render('yourplugin/_fields/settings', array(
    'settings' => $this->getSettings(),
    'matrixFieldOptions' => $matrixFieldOptions
));
| improve this answer | |
  • Thanks guys, this has been super helpful. I’m actually going to just show all the Matrix fields from the site in one dropdown, but this got me moving in the right direction and I used all of it. – noahread Jul 8 '14 at 20:20

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.