You are here

function shs_field_widget_form in Simple hierarchical select 7

Implements hook_field_widget_form().

File

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

Code

function shs_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $field_column = key($field['columns']);

  // Get value.
  $element_value = NULL;
  $submitted_value = NULL;
  if (!empty($form_state['values']) && !empty($element['#parents'])) {
    $submitted_value = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  }
  if (!empty($items[$delta][$field_column])) {

    // Use saved value from database or cache.
    $element_value = $items[$delta][$field_column];
  }
  elseif (!empty($submitted_value)) {

    // Use value from form_state (for example for fields with cardinality = -1).
    $element_value = array(
      'tid' => $submitted_value,
    );
  }

  // Get vocabulary names from allowed values.
  if ($field['type'] == 'entityreference') {
    if ('views' === $field['settings']['handler']) {
      $vocabulary_names = array();
      $view_settings = $field['settings']['handler_settings']['view'];

      // Try to load vocabularies from view filter.
      $vocabulary_names = _shs_entityreference_views_get_vocabularies($view_settings['view_name'], $view_settings['display_name']);
    }
    else {
      $vocabulary_names = $field['settings']['handler_settings']['target_bundles'];
    }
  }
  else {
    $allowed_values = reset($field['settings']['allowed_values']);
    $vocabulary_names = empty($allowed_values['vocabulary']) ? FALSE : $allowed_values['vocabulary'];
  }
  if (empty($vocabulary_names) && (empty($field['settings']['handler']) || 'views' !== $field['settings']['handler'])) {

    // No vocabulary selected yet.
    return array();
  }
  if (!is_array($vocabulary_names)) {
    $vocabulary_names = array(
      $vocabulary_names,
    );
  }
  $vocabularies = array();
  foreach ($vocabulary_names as $vocabulary_name) {
    if (($vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name)) === FALSE) {

      // Vocabulary not found. Stop here.
      return array();
    }
    $vocabularies[] = $vocabulary;
  }

  // Check if term exists (the term could probably be deleted meanwhile).
  if ($element_value && taxonomy_term_load($element_value) === FALSE) {
    $element_value = 0;
  }
  if (count($vocabularies) > 1 || !isset($vocabulary) || isset($vocabulary) && !user_access('edit terms in ' . $vocabulary->vid)) {

    // Creating new terms is allowed only with proper permission and if only one
    // vocabulary is selected as source.
    $instance['widget']['settings']['shs']['create_new_terms'] = FALSE;
  }
  $instance['widget']['settings']['shs']['test_create_new_terms'] = module_implements('shs_add_term_access');
  $instance['widget']['settings']['shs']['required'] = $element['#required'];

  // Prepare the list of options.
  if ($field['type'] == 'entityreference') {

    // Get current selection handler.
    $handler = entityreference_get_selection_handler($field, $instance, $element['#entity_type'], $element['#entity']);
    $referencable_entities = $handler
      ->getReferencableEntities();
    $options = array(
      '_none' => empty($element['#required']) ? t('- None -', array(), array(
        'context' => 'shs',
      )) : t('- Select a value -', array(), array(
        'context' => 'shs',
      )),
    );
    foreach ($referencable_entities as $terms) {
      $options += $terms;
    }
  }
  else {
    $properties = _options_properties('select', FALSE, $element['#required'], !empty($element_value));
    $options = _options_get_options($field, $instance, $properties, $element['#entity_type'], $element['#entity']);
  }

  // Create element.
  $element += array(
    '#type' => 'select',
    '#default_value' => empty($element_value) ? NULL : $element_value,
    '#options' => $options,
    '#attributes' => array(
      'class' => array(
        'shs-enabled',
      ),
    ),
    // Prevent errors with drupal_strlen().
    '#maxlength' => NULL,
    '#element_validate' => array(
      'shs_field_widget_validate',
    ),
    '#after_build' => array(
      'shs_field_widget_afterbuild',
    ),
    '#shs_settings' => $instance['widget']['settings']['shs'],
    '#shs_vocabularies' => $vocabularies,
  );
  $return = array(
    $field_column => $element,
  );
  if (!empty($element['#title'])) {

    // Set title to "parent" element to enable label translation.
    $return['#title'] = $element['#title'];
  }
  return $return;
}