2

UPDATE: I've confirmed that the theme_select function is being called, generates the expected html for the select element and that that output isn't actually being used.


I have a module which adds some functionality to webforms. As part of that I'm adding tabs when a webform is displayed so that our staff can do some fangling when necessary.

I have a tab being added:

//linker tab
    $items['node/%node/linker'] = array(
      'title' => 'Payment Linker',
      'page callback' => 'addLinkerTab',
      'page arguments' => array(1),
      'access callback' => accessLinkerTab,
      'access arguments' => array(1),
      'weight' => 100,
      'type' => MENU_LOCAL_TASK,
    );

which displays a form to do the initial setup of the modules functionality. This has been in place for over a year and works fine. It also includes:

$form['linkform']['components']['add']=array(
        '#type' => 'button',
        '#attributes' => array('class' => 'add-component'),
        '#value' => t('add'),
        '#ahah' => array(
          'path' => 'webform_checkout/link-editor/add-link-component',
          'wrapper' => 'item-holder',
          'method' => 'append',
          'effect' => 'fade',
          )
    );

I have a custom theme_form_element:

function bmc_zen_form_element($element, $value) {

  // This is also used in the installer, pre-database setup.
  $t = get_t();

  $output = '<div class="form-item"';
  if (!empty($element['#id'])) {
    $output .= ' id="' . $element['#id'] . '-wrapper"';
  }
  $output .= ">\n";
  $required = !empty($element['#required']) ? '<span class="form-required" title="' . $t('This field is required.') . '">*</span>' : '';

  if (!empty($element['#title'])) {
    $title = $element['#title'];
    if (!empty($element['#id'])) {
      $output .= ' <label for="' . $element['#id'] . '">' . $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) . "</label>\n";
    }
    else {
      $output .= ' <label>' . $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) . "</label>\n";
    }
  }

  //not every element looks right with the description above it.
  If (array_key_exists('#webform_component',$element) && $element['#type']!='checkbox'){
      if (!empty($element['#description'])) {
        $output .= ' <div class="description">' . $element['#description'] . "</div>\n";
      }
      $output .= '<div id="' . $element['#id'] . '">' . $value . '</div>' . "\n";
  } else {
    $output .= $value ."\n";
    if (!empty($element['#description'])) {
        $output .= ' <div class="description">' . $element['#description'] . "</div>\n";
      }
  }

  $output .= "</div>\n";

  return $output;
}

and a custom hook_theme_Select

function webform_checkout_theme_select($element)
  {

    //if the array_parents is a 0 then we're in the ChooseForm form so
    if ($element['#array_parents'][0] == "linkform") {
      $elementName = "";
      //build the element name. we don't want to end up with a dangling dash
      $size = sizeof($element['#array_parents']);
      for ($i = 1; $i < $size; $i++) {
        $elementName .= $element['#array_parents'][$i] . '-';
      }
      $elementName = rtrim($elementName, '-');
    }

    $select = '';
    $size = $element['#size'] ? ' size="' . $element['#size'] . '"' : '';
    _form_set_class($element, array('form-select'));
    $multiple = $element['#multiple'];
    return theme('form_element', $element, '<select name="' . $elementName . '' . ($multiple ? '[]' : '') . '"' . ($multiple ? ' multiple="multiple" ' : '') . drupal_attributes($element['#attributes']) . ' id="' . $element['#id'] . '" ' . $size . '>' . form_select_options($element) . '</select>');
  }

I've just added a second tab

 $items['node/%node/formadmin'] = array(
      'title' => 'Admin',
      'page callback' => 'admin_tab',
      'page arguments' => array(1),
      'access callback' => accessLinkerTab,
      'access arguments' => array(1),
      'weight' => 100,
      'type' => MENU_LOCAL_TASK,
    );

Which adds two select elements to a form

  function orders_tab_form(&$form_state,$node) {
    $order_admin_form = array();
    $submissions = getAllSubmissionsForWebform($node->nid);
    $orders = getAllOrders();

    $order_admin_form['order_submission_linking_form']['submissions']['select']=array(
      '#type' => 'select',
      '#attributes' => array('name'=>'submissions-select'),
      '#title' => t('Submissions'),
      '#options' => $submissions
    );
    $order_admin_form['order_submission_linking_form']['orders']['select']=array(
      '#type' => 'select',
      '#attributes' => array('name'=>'orders-select'),
      '#title' => t('Orders'),
      '#options' => $orders
    );

    $order_admin_form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save Link'),
    );
    return $order_admin_form;
  }

When the form is displayed the two select elements are named 'add-component' and 'add-maths' which means that they aren't showing in the form_state when submitted.

<select name="add-component" class="form-select" id="edit-select"><option value="0">1727</option><option value="1">1728</option></select>

for example is the output of $order_admin_form['order_submission_linking_form']['submissions']['select']...

I'm utterly confused as to why the name is being set incorrectly.

Apologies for such a long question and thanks in advance for any suggestions.

1

The solution to this problem is that I am a massive idiot!

Often I've found when the answer seems to be "The framework with hundreds of contributors is wrong" then you've done something stupid and forgotten.

'Drunk Paul' has clearly been coding as I just found tucked at the bottom of some javascript

$("#edit-select").attr("name","add-component");
$("#edit-select-1").attr("name","add-math");

Which certainly explains my problem.

Now I just need to find out why I did that!

Apologies and thanks to anyone who has or will view.

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.