You are here

function _hs_process_render_create_new_item in Hierarchical Select 7.3

1 call to _hs_process_render_create_new_item()
form_hierarchical_select_process in ./hierarchical_select.module
Hierarchical select form element type #process callback.

File

./hierarchical_select.module, line 574
This module defines the "hierarchical_select" form element, which is a greatly enhanced way for letting the user select items in a hierarchy.

Code

function _hs_process_render_create_new_item($element, $hierarchy) {
  $creating_new_item = FALSE;

  // This container and the "Create" / "Cancel" buttons must always be part of
  // the form, even when HS is not in create mode, in order for AJAX submit
  // callbacks on the "Create" and "Cancel" buttons to be processed correctly.
  //
  // Basically, FAPI looks through each of the buttons in the form to determine
  // which one was clicked. If it can't find the responsible button, it
  // assumes it was the first button in the form. This is problematic when the
  // user clicks on the "Create" or "Cancel" buttons because we only want them
  // to show up when HS is in create mode. To fix this, we always render the
  // buttons as part of the form, then disable access to them in an
  // "#after_build" callback.
  //
  // This might not be necessary if we used D7's native AJAX callback function,
  // ajax_form_callback().
  $element['hierarchical_select']['create_new_item'] = array(
    '#prefix' => '<div class="create-new-item">',
    '#suffix' => '</div>',
    '#after_build' => array(
      'hierarchical_select_create_new_item_after_build',
    ),
  );

  // @todo Port to use built-in D7 AJAX callback?
  $element['hierarchical_select']['create_new_item']['create'] = array(
    '#type' => 'submit',
    '#value' => t('Create'),
    '#attributes' => array(
      'class' => array(
        'create-new-item-create',
      ),
    ),
    '#limit_validation_errors' => array(
      $element['#parents'],
    ),
    '#validate' => array(),
    '#submit' => array(
      'hierarchical_select_ajax_update_submit',
    ),
  );
  $element['hierarchical_select']['create_new_item']['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#attributes' => array(
      'class' => array(
        'create-new-item-cancel',
      ),
    ),
    '#limit_validation_errors' => array(
      $element['#parents'],
    ),
    '#validate' => array(),
    '#submit' => array(
      'hierarchical_select_ajax_update_submit',
    ),
  );
  if (isset($element['#value']['hierarchical_select']['selects'])) {
    foreach ($element['#value']['hierarchical_select']['selects'] as $depth => $value) {
      if ($value == 'create_new_item' && _hierarchical_select_create_new_item_is_allowed($element['#config'], $depth)) {
        $creating_new_item = TRUE;

        // We want to override the select in which the "create_new_item"
        // option was selected and hide all selects after that, if they exist.
        // If depth == 0, then that means all selects should be hidden.
        if ($depth == 0) {
          unset($element['hierarchical_select']['selects']);
        }
        else {
          for ($i = $depth; $i < count($hierarchy->lineage); $i++) {
            unset($element['hierarchical_select']['selects'][$i]);
          }
        }
        $item_type_depth = $value == 'create_new_item' ? $depth : $depth + 1;
        $item_type = count($element['#config']['editability']['item_types']) == $item_type_depth ? t($element['#config']['editability']['item_types'][$item_type_depth]) : t('item');
        $element['hierarchical_select']['create_new_item']['input'] = array(
          '#type' => 'textfield',
          '#size' => 20,
          '#maxlength' => 255,
          '#default_value' => t('new @item', array(
            '@item' => $item_type,
          )),
          '#attributes' => array(
            'title' => t('new @item', array(
              '@item' => $item_type,
            )),
            'class' => array(
              'create-new-item-input',
            ),
          ),
          // Prevent the textfield from being wrapped in a div. This
          // simplifies the CSS and JS code.
          '#theme_wrappers' => array(),
          // Place the textfield above the "Create" / "Cancel" buttons.
          '#weight' => -1,
        );
      }
    }
  }
  $element['hierarchical_select']['create_new_item']['#creating_new_item'] = $creating_new_item;
  return array(
    $element,
    $creating_new_item,
  );
}