You are here

function term_reference_filter_by_views_field_widget_form_alter in Taxonomy Term Reference Filter by Views 7

Same name and namespace in other branches
  1. 7.2 term_reference_filter_by_views.module \term_reference_filter_by_views_field_widget_form_alter()

Implements hook_widget_field_form().

File

./term_reference_filter_by_views.module, line 18

Code

function term_reference_filter_by_views_field_widget_form_alter(&$element, &$form_state, $context) {

  // Add a css class to widget form elements for all fields of type mytype.
  if ($context['field']['type'] == 'taxonomy_term_reference') {

    //dsm($element);
    if (!empty($context['instance']['filter_view'])) {
      if ($element['#type'] == 'select' || $element['#type'] == 'radios' || $element['#type'] == 'checkboxes') {
        $allowed = _term_reference_filter_by_views_get_allowed_values($context['instance']['filter_view']);
        if (!empty($allowed)) {

          // dsm($allowed);
          $options = array();
          $options = array_intersect_key($element['#options'], $allowed);
          $element['#options'] = $options;
        }
      }
      elseif (isset($element['#autocomplete_path'])) {

        //dsm($context['field']);
        $element['#autocomplete_path'] = 'term_reference_filter_by_views/autocomplete/' . $element['#field_name'] . '/' . $context['instance']['filter_view'];
      }
    }

    // Be sure not to overwrite existing attributes.
  }
}