You are here

function _webform_render_shs in Webform Simple Hierarchical Select 7

Implements _webform_render_component().

File

components/shs.inc, line 55
Webform simple hierarchical select component.

Code

function _webform_render_shs($component, $value = NULL) {
  global $language;
  $vocabulary = taxonomy_vocabulary_load($component['extra']['vid']);
  if (empty($vocabulary)) {
    $form_item['markup'] = array(
      '#markup' => '<div class="form-item">' . t('An invalid vocabulary is selected. Please change it in the options.') . '</div>',
    );
    return $form_item;
  }

  // Add fake item for next level.
  $parents[] = array(
    'tid' => 0,
  );
  $element_settings = array(
    'create_new_terms' => FALSE,
    'create_new_levels' => FALSE,
    'required' => FALSE,
    'language' => $language,
  );
  if (!empty($component['required'])) {
    $element_settings['required'] = TRUE;
  }
  $multiple = !empty($component['extra']['multiple']);

  // Generate a random hash to avoid merging of settings by drupal_add_js.
  // This is necessary until http://drupal.org/node/208611 lands for D7.
  $js_hash = _shs_create_hash();
  $node = node_load($component['nid']);
  $component_parents = webform_component_parent_keys($node, $component);
  $identifier = 'submitted[' . implode('][', $component_parents) . ']';

  // Create settings needed for our js magic.
  $settings_js = array(
    'shs' => array(
      $identifier => array(
        $js_hash => array(
          'vid' => $vocabulary->vid,
          'settings' => $element_settings,
          'default_value' => isset($value) ? $value[0] : $component['value'],
          'parents' => $parents,
          'multiple' => $multiple,
          'any_label' => variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? t('<Any>') : t('- Any -'),
          'any_value' => 'All',
        ),
      ),
    ),
  );
  drupal_alter(array(
    'shs_js_settings',
    "shs_{$identifier}_js_settings",
  ), $settings_js, $identifier, $vocabulary->vid);

  // Add settings.
  drupal_add_js($settings_js, 'setting');

  // Add behavior.
  drupal_add_js(drupal_get_path('module', 'shs') . '/js/shs.js');
  $form_item = array(
    '#type' => 'textfield',
    '#title' => $component['name'],
    '#required' => $component['required'],
    '#weight' => $component['weight'],
    '#description' => $component['extra']['description'],
    '#default_value' => $component['value'],
    '#prefix' => '<div class="form-item webform-component webform-component--' . str_replace('_', '-', $component['form_key']) . ' webform-component-' . $component['type'] . ' ' . $component['extra']['wrapper_classes'] . '" id="webform-component-' . $component['form_key'] . '">',
    '#suffix' => '</div>',
    '#attributes' => array(
      'class' => array(
        'element-invisible',
        'shs-enabled',
      ),
    ),
  );
  if (isset($value)) {
    $form_item['#default_value'] = $value[0];
  }
  return $form_item;
}