Note that there are some explanatory texts on larger screens.

plurals
  1. PODrupal Form API - Using foreach loops to build forms
    text
    copied!<p>I'm building a Drupal module to tie an icon to a particular page using an administration form. Each image placed within a certain directory needs to be output with a select box next to it showing all the primary link titles.</p> <p>I've built the form using a <code>foreach</code> loop but when I check the output using <code>dpm($form);</code> in the _submit function the #value for each images page element is always equal to what ever is set for the last image.</p> <p>Here's my code:</p> <pre><code>function titleicon_admin_settings() { $settings = variable_get('titleicon_settings', $default); //build an array of primary link titles $primary_links_items = menu_primary_links(); foreach ($primary_links_items as $item) { $title = $item['attributes']['title']; $href = $item['href']; $titles[$href] = $title; } //build array of icons $directory = file_directory_path() . '/icons'; $mask = '(jpg|jpeg|gif|png|JPG|JPEG|GIF|PNG)'; $icons = file_scan_directory($directory, $mask); foreach ($icons as $icon) { $name = $icon-&gt;name; $path = base_path() . $icon-&gt;filename; $html = '&lt;img src="' . $path . '" width="50" height="50" /&gt;'; $default_value = $settings[$name]['page']; $form[$name] = array( '#type' =&gt; 'fieldset', '#title' =&gt; $name, ); $form[$name]['path_to_icon'] = array( '#type' =&gt; 'value', '#value' =&gt; $path, ); $form[$name]['icon'] = array( '#type' =&gt; 'markup', '#value' =&gt; $html, ); $form[$name]['page'] = array( '#type' =&gt; 'select', '#title' =&gt; t('Show icon on page'), '#default_value' =&gt; $default_value, '#description' =&gt; t('Choose which page to show icon on.'), '#options' =&gt; $titles, ); } $form['submit'] = array( '#type' =&gt; 'submit', '#value' =&gt; t('Save'), ); return $form; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload