You are here

function select2_field_widget_form_alter in Select 2 7

Implements hook_field_widget_form_alter().

File

./select2.module, line 828
Main file for Select2 module.

Code

function select2_field_widget_form_alter(&$element, &$form_state, $context) {
  $context['instance']['settings'] += array(
    'select2_integration' => array(
      'use_select2' => isset($context['instance']['settings']['use_select2']) ? $context['instance']['settings']['use_select2'] : FALSE,
    ),
  );
  if (!$context['instance']['settings']['select2_integration']['use_select2']) {
    return;
  }
  $settings = $context['instance']['settings']['select2_integration'];
  $styling_settings = isset($settings['styling']) ? $settings['styling'] : array();
  $settings = array_merge($settings, $styling_settings);
  $element['#select2'] = $settings;
  if (isset($element['#autocomplete_path'])) {
    $settings['allow_add_onfly'] = FALSE;
  }
  if (isset($settings['allow_add_onfly']) && $settings['allow_add_onfly']) {
    $element['#type'] = 'select2_hidden';
    $element['#value_callback'] = 'select_2_hidden_value_callback';
    $element['#select2']['taxonomy_vocabulary'] = $context['field']['settings']['allowed_values'][0]['vocabulary'];
    $element['#select2']['minimumResultsForSearch'] = 0;
    $element['#element_validate'] = array_merge(array(
      'select2_taxonomy_term_reference_validate',
    ), $element['#element_validate']);
    $descrition = t('This element supports "Add terms on fly" function.<br />You can select terms from defined terms list, or add your new term(s), by typing name of term(s) manually, press Enter key after typing new term name, for adding it to selected items list. <br /><b>New term(s) will be saved when form will be submitted</b>.');
    $terms_edit_page_link = '<br />' . l(t('Edit vocabulary terms'), 'admin/structure/taxonomy/' . $element['#select2']['taxonomy_vocabulary'], array(
      'attributes' => array(
        'target' => '_blank',
        'class' => array(
          'taxonomy_voc_terms_page_link',
        ),
      ),
    ));
    if (isset($settings['show_voc_terms_page_link']) && $settings['show_voc_terms_page_link']) {
      $descrition .= $terms_edit_page_link;
    }
    elseif (!isset($settings['show_voc_terms_page_link'])) {
      $descrition .= $terms_edit_page_link;
    }
    $element['#description'] = (isset($element['#description']) ? $element['#description'] . '<br/>' : '') . $descrition;
    if (isset($element['#multiple']) && $element['#multiple']) {
      $element['#select2']['multiple'] = TRUE;
      $element['#select2']['separator'] = SELECT2_VALUES_SEPARATOR;
    }
  }
}