<p>Your plugin will need a few things.</p>

<ul>
<li>A <a href="http://buildwithcraft.com/docs/plugins/field-types" rel="nofollow">FieldType</a></li>
<li><a href="http://buildwithcraft.com/docs/plugins/field-types#giving-your-fieldtype-some-settings" rel="nofollow">FieldType Settings</a> using <code>defineSettings()</code> and <code>getSettingsHtml()</code> method</li>
<li>A template that will render your settings which will be referenced in the <code>craft()-&gt;templates-&gt;render</code> function of the <code>getSettingsHtml()</code> method</li>
</ul>

<p>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.</p>

<p>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:</p>

<p>Get all available Sections</p>

<pre class="lang-php prettyprint-override"><code>$sections = craft()-&gt;sections-&gt;getAllSections()
</code></pre>

<p>Get a specific section by ID</p>

<pre class="lang-php prettyprint-override"><code>$section = craft()-&gt;sections-&gt;getSectionById(1);
</code></pre>

<p>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 <code>getEntryTypes()[0]</code> however in your code, you'll likely have to do a bit more logic to make sure you're getting what you want:</p>

<pre class="lang-php prettyprint-override"><code>$fields = $section-&gt;getEntryTypes()[0]-&gt;getFieldLayout()-&gt;getFields();
</code></pre>

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

<pre class="lang-php prettyprint-override"><code>foreach ($fields as $field) 
{
  $field = $field-&gt;getField();          

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

<p>Once you build an array of your options, you can hand them off to your fields settings template in the <code>getSettingsHtml()</code> 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.</p>

<pre class="lang-php prettyprint-override"><code>return craft()-&gt;templates-&gt;render('yourplugin/_fields/settings', array(
    'settings' =&gt; $this-&gt;getSettings(),
    'matrixFieldOptions' =&gt; $matrixFieldOptions
));
</code></pre>

CC BY-SA 3.0
2014-07-08T20:20:41.183Z
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
CC BY-SA 3.0
2014-07-05T07:27:02.927Z
2014-07-06T17:39:22.030Z
2014-07-06T17:39:22.030Z
Ben Parizek
<p>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?</p>

<plugin-development>
How can I create list of a group’s fields for a select field’s options in my Field Settings?
# How can I create list of a group’s fields for a select field’s options in my Field Settings?

(Asked by: username_0 on 2014-07-03)

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?

**Tags:** plugin-development


---

## 1 answers

### Answer by username_1 on 2014-07-05. Score: 3 | Accepted answer

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
));

> **Comments:**
> **username_0**: 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.  

---

