You are here

function menu_position_menu_position_rule_taxonomy_form in Menu Position 7

Same name and namespace in other branches
  1. 6 plugins/menu_position.taxonomy.inc \menu_position_menu_position_rule_taxonomy_form()
  2. 7.2 plugins/menu_position.taxonomy.inc \menu_position_menu_position_rule_taxonomy_form()

Adds form elements for the "taxonomy" plugin to the rule configuration form.

Parameters

array $form: A reference to the "add/edit rule" form array. New form elements should be added directly to this array.

array $form_state: A reference to the current form state.

File

plugins/menu_position.taxonomy.inc, line 108
Provides the "Taxonomy" rule plugin for the Menu Position module.

Code

function menu_position_menu_position_rule_taxonomy_form(array &$form, array &$form_state) {

  // If this is an existing rule, load the variables stored in the rule for this
  // plugin.
  $variables = !empty($form_state['#menu-position-rule']['conditions']['taxonomy']) ? $form_state['#menu-position-rule']['conditions']['taxonomy'] : array();

  // Fallback for legacy configuration.
  if (empty($variables['match_types'])) {
    $variables['match_types'] = array(
      'terms' => '',
      'content' => 'content',
    );
  }

  // Load the taxonomy terms.
  if (!empty($variables['tid'])) {
    $terms = array();
    foreach (taxonomy_term_load_multiple($variables['tid']) as $term) {
      $terms[] = $term->name;
    }
    $terms = implode(', ', $terms);
  }
  else {
    $terms = '';
  }
  $form['conditions']['taxonomy'] = array(
    '#type' => 'fieldset',
    '#title' => t('Taxonomy'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'menu_position') . '/plugins/menu_position.taxonomy.js',
      ),
    ),
  );
  $form['conditions']['taxonomy']['description'] = array(
    '#type' => 'item',
    '#description' => t('Apply this rule only on pages that display content having the given vocabulary or taxonomy term.'),
  );
  $form['conditions']['taxonomy']['match_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Type of match'),
    '#default_value' => !empty($variables['match_types']) ? $variables['match_types'] : array(
      'terms' => '',
      'content' => 'content',
    ),
    '#options' => array(
      'terms' => t('Match when viewing term pages'),
      'content' => t('Match when viewing content tagged with the term'),
    ),
  );
  $vocabularies = taxonomy_vocabulary_get_names();
  $vocabulary_options = array();
  foreach ($vocabularies as $vocabulary) {

    // $vocabulary['machine_name']
    $vocabulary_options[$vocabulary->vid] = $vocabulary->name;
  }
  $form['conditions']['taxonomy']['vid'] = array(
    '#type' => 'select',
    '#title' => t('Vocabulary'),
    '#default_value' => !empty($variables['vid']) ? $variables['vid'] : '0',
    '#options' => array(
      '0' => t('- None -'),
    ) + $vocabulary_options,
  );
  $form['conditions']['taxonomy']['vid_or_tid'] = array(
    '#type' => 'radios',
    '#title' => t('In this vocabulary, match:'),
    '#default_value' => !empty($variables['tid']) ? 'tid' : 'vid',
    '#options' => array(
      'vid' => t('any taxonomy term'),
      'tid' => t('only a selected taxonomy term'),
    ),
    '#states' => array(
      'invisible' => array(
        ':input[name=vid]' => array(
          'value' => '0',
        ),
      ),
    ),
  );
  $form['#attached']['js'][] = array(
    'data' => array(
      'menu_position_taxonomy_url' => url('menu-position/taxonomy/autocomplete'),
    ),
    'type' => 'setting',
  );
  $form['conditions']['taxonomy']['term'] = array(
    '#type' => 'textfield',
    '#maxlength' => 1024,
    '#title' => t('Taxonomy term'),
    '#default_value' => $terms,
    '#autocomplete_path' => 'menu-position/taxonomy/autocomplete/' . (isset($form_state['input']['vid']) ? $form_state['input']['vid'] : $form['conditions']['taxonomy']['vid']['#default_value']),
    '#element_validate' => array(
      'menu_position_taxonomy_autocomplete_validate',
    ),
    '#description' => t('Match at least one of these taxonomy terms.'),
    '#states' => array(
      'visible' => array(
        ':input[name=vid_or_tid]' => array(
          'value' => 'tid',
        ),
      ),
    ),
  );

  // Add a submit handler.
  $form['#submit'][] = 'menu_position_menu_position_rule_taxonomy_form_submit';
}