You are here

function shs_field_widget_afterbuild in Simple hierarchical select 7

Afterbuild callback for widgets of type "taxonomy_shs".

1 string reference to 'shs_field_widget_afterbuild'
shs_field_widget_form in ./shs.module
Implements hook_field_widget_form().

File

./shs.module, line 533
Provides an additional widget for term fields to create hierarchical selects.

Code

function shs_field_widget_afterbuild($element, &$form_state) {
  $js_added =& drupal_static(__FUNCTION__ . '_js_added', array());

  // 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 =& drupal_static(__FUNCTION__ . '_js_hash');
  if (empty($js_hash)) {
    $js_hash = _shs_create_hash();
  }
  $parents = array();

  // Get default value from form state and set it to element.
  $default_value = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  if (!empty($default_value)) {

    // Use value from form_state (for example for fields with cardinality = -1).
    $element['#default_value'] = $default_value;
  }

  // Add main Javascript behavior and style only once.
  if (count($js_added) == 0) {

    // Add behavior.
    drupal_add_js(drupal_get_path('module', 'shs') . '/js/shs.js');

    // Add styles.
    drupal_add_css(drupal_get_path('module', 'shs') . '/theme/shs.form.css');
  }

  // Create Javascript settings for the element only if it hasn't been added
  // before.
  if (empty($js_added[$element['#name']][$js_hash])) {
    $element_value = $element['#default_value'];

    // Ensure field is rendered if it is required but not selected.
    if (empty($element_value) || $element_value == '_none') {

      // Add fake parent for new items or field required submit fail.
      $parents[] = array(
        'tid' => 0,
      );
    }
    else {
      $term_parents = taxonomy_get_parents_all($element_value);
      foreach ($term_parents as $term) {

        // Create term lineage.
        $parents[] = array(
          'tid' => $term->tid,
        );
      }
    }
    $vocabularies = $element['#shs_vocabularies'];
    $vocabulary_identifier = NULL;
    if (count($vocabularies) == 1) {

      // Get ID from first (and only) vocabulary.
      $vocabulary_identifier = $vocabularies[0]->vid;
    }
    else {
      $vocabulary_identifier = array(
        'field_name' => $element['#field_name'],
      );
    }

    // Create token to prevent against CSRF attacks.
    $token = drupal_get_token('shs-' . $element['#name']);

    // Create settings needed for our js magic.
    $settings_js = array(
      'shs' => array(
        "{$element['#name']}" => array(
          $js_hash => array(
            'vid' => $vocabulary_identifier,
            'settings' => $element['#shs_settings'],
            'default_value' => $element['#default_value'],
            'parents' => array_reverse($parents),
            'any_label' => empty($element['#required']) ? t('- None -', array(), array(
              'context' => 'shs',
            )) : t('- Select a value -', array(), array(
              'context' => 'shs',
            )),
            'any_value' => '_none',
            'token' => $token,
          ),
        ),
      ),
    );

    // Allow other modules to alter these settings.
    drupal_alter(array(
      'shs_js_settings',
      "shs_{$element['#field_name']}_js_settings",
    ), $settings_js, $element['#field_name'], $vocabulary_identifier);

    // Add settings.
    drupal_add_js($settings_js, 'setting');
    if (empty($js_added[$element['#name']])) {
      $js_added[$element['#name']] = array();
    }
    $js_added[$element['#name']][$js_hash] = TRUE;
  }
  unset($element['#needs_validation']);
  return $element;
}