Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal Form API - Using foreach loops to build forms

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.

I've built the form using a foreach loop but when I check the output using dpm($form); in the _submit function the #value for each images page element is always equal to what ever is set for the last image.

Here's my 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->name;
    $path = base_path() . $icon->filename;
    $html = '<img src="' . $path . '" width="50" height="50" />';
    $default_value = $settings[$name]['page'];

    $form[$name] = array(
      '#type' => 'fieldset',
      '#title' => $name,
    );

    $form[$name]['path_to_icon'] = array(
      '#type' => 'value',
      '#value' => $path, 
    );

    $form[$name]['icon'] = array(
      '#type' => 'markup',
      '#value' => $html, 
    );

    $form[$name]['page'] = array(
      '#type' => 'select',
      '#title' => t('Show icon on page'),
      '#default_value' => $default_value,
      '#description' => t('Choose which page to show icon on.'),
      '#options' => $titles,
    );
  }

  $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'), 
    );

  return $form;
}   
like image 655
splatio Avatar asked Feb 24 '23 19:02

splatio


1 Answers

Which makes perfect sense. If your fields are declared like this:

$form[$name]['path_to_icon'] = array(
  '#type' => 'value',
  '#value' => $path, 
);

then for each file you are updating the same variable - 'path_to_icon'. Fieldset key "$name" does not matter here, as it is used only for grouping form fields together.

You would need to use something more like:

$form[$name]['path_to_icon_'.$name] = array(
  '#type' => 'value',
  '#value' => $path, 
);

then you will get multiple values after posting the form.

However, to tell the truth, I wouldn't use $name as element of variable name, you should rather have something like auto-incrementing $fid (file id from files table) or any other unique and SAFE identifier for each file...

like image 108
Maciej Zgadzaj Avatar answered Mar 08 '23 15:03

Maciej Zgadzaj