You are here

function synonyms_field_widget_form in Synonyms 7

Implements hook_field_widget_form().

File

./synonyms.module, line 294
Provide synonyms feature for Drupal entities.

Code

function synonyms_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $default_value = synonyms_select_default_value($field, $instance, $items);
  switch ($instance['widget']['type']) {
    case 'synonyms_autocomplete_taxonomy_term':
    case 'synonyms_autocomplete_entity':
      switch ($instance['widget']['type']) {
        case 'synonyms_autocomplete_taxonomy_term':
          $default_value_string = taxonomy_implode_tags(taxonomy_term_load_multiple($default_value));
          $element_validate = array(
            'taxonomy_autocomplete_validate',
            'synonyms_autocomplete_taxonomy_term_validate',
          );
          break;
        case 'synonyms_autocomplete_entity':
          $default_value_string = array();
          $entity = isset($element['#entity']) ? $element['#entity'] : NULL;
          $handler = entityreference_get_selection_handler($field, $instance, $instance['entity_type'], $entity);
          $target_entities = entity_load($field['settings']['target_type'], $default_value);
          foreach ($target_entities as $target_entity_id => $target_entity) {
            $default_value_string[] = synonyms_autocomplete_escape($handler
              ->getLabel($target_entity));
          }
          $default_value_string = drupal_implode_tags($default_value_string);
          $element_validate = array(
            'synonyms_autocomplete_entity_validate',
          );
          break;
      }
      $element += array(
        '#type' => 'textfield',
        '#default_value' => $default_value_string,
        '#autocomplete_path' => $instance['widget']['settings']['synonyms_autocomplete_path'] . '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'],
        '#size' => $instance['widget']['settings']['size'],
        '#maxlength' => 1024,
        '#element_validate' => $element_validate,
        '#attached' => array(
          'js' => array(
            drupal_get_path('module', 'synonyms') . '/js/synonyms-autocomplete.js' => array(),
          ),
        ),
        '#attributes' => array(
          'class' => array(
            'synonyms-autocomplete',
          ),
        ),
      );
      if (isset($instance['widget']['settings']['auto_creation'])) {
        $element['#auto_creation'] = $instance['widget']['settings']['auto_creation'];
      }
      break;
    case 'synonyms_select_taxonomy_term':
      $multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
      $options = array();
      foreach ($field['settings']['allowed_values'] as $tree) {
        if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
          switch ($instance['widget']['settings']['sort']) {
            case 'weight':
              if ($terms = taxonomy_get_tree($vocabulary->vid, $tree['parent'], NULL, TRUE)) {
                $behavior_implementations = synonyms_behavior_get('select', 'taxonomy_term', field_extract_bundle('taxonomy_term', $vocabulary), TRUE);
                foreach ($terms as $term) {
                  $options[] = synonyms_select_option_entity($term, 'taxonomy_term', NULL, NULL, array(
                    'depth',
                  ));
                  foreach ($behavior_implementations as $implementation) {
                    foreach ($implementation['object']
                      ->extractSynonyms($term) as $synonym) {
                      $options[] = synonyms_select_option_entity($term, 'taxonomy_term', $synonym, $implementation, array(
                        'depth',
                      ));
                    }
                  }
                }
              }
              break;
            case 'name':

              // TODO: is there any way to leverage DB for the sorting routine?
              $options = synonyms_select_taxonomy_term_sort_name_options_recursive($vocabulary, $tree['parent']);
              break;
          }
        }
      }
      $element += array(
        '#type' => 'select',
        '#multiple' => $multiple,
        '#options' => $options,
        '#default_value' => $default_value,
        '#element_validate' => array(
          'synonyms_select_validate',
          'synonyms_select_form_to_storage',
        ),
        '#empty_option' => t('- None -'),
      );
      break;
    case 'synonyms_select_entity':
      $multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
      $element += array(
        '#type' => 'select',
        '#multiple' => $multiple,
        '#options' => synonyms_select_entity_options($field, $instance, $element['#entity']),
        '#default_value' => $default_value,
        '#element_validate' => array(
          'synonyms_select_validate',
          'synonyms_select_form_to_storage',
        ),
        '#empty_option' => t('- None -'),
      );
      break;
  }
  return $element;
}