You are here

taxonomy_menu_trails.module in Taxonomy Menu Trails 8

Changes menu trail of current entity to its term's menu item.

File

taxonomy_menu_trails.module
View source
<?php

/**
 * @file
 * Changes menu trail of current entity to its term's menu item.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeTypeInterface;

/**
 * Implements hook_form_alter().
 */
function taxonomy_menu_trails_form_alter(&$form, FormStateInterface &$form_state, $form_id) {
  if ($form_id == 'node_type_edit_form') {
    _taxonomy_menu_trails_alter_bundle_form($form, 'node', $form['type']['#default_value'], $form_state);
    $form['taxonomy_menu_trails']['#group'] = 'additional_settings';
  }
}

/**
 * Add Taxonomy Menu Trails settings to bundle form.
 *
 * @param array $form
 * @param string $entity_type
 * @param mixed $bundle
 */
function _taxonomy_menu_trails_alter_bundle_form(&$form, $entity_type, $bundle, $form_state) {
  $bundle_label = \Drupal::entityTypeManager()
    ->getStorage('node_type')
    ->load($bundle)
    ->label();
  $bundleFields = [];
  foreach (\Drupal::entityManager()
    ->getFieldDefinitions($entity_type, $bundle) as $field_name => $field_definition) {
    if (!empty($field_definition
      ->getTargetBundle())) {
      if ($field_definition
        ->getType() == 'entity_reference') {
        $bundleFields[$field_name] = $field_definition
          ->getLabel();
      }
    }
  }
  switch ($entity_type) {
    case 'node':
      $default_paths = t('paths "node/[nid]" and "node/[nid]/*"');
      break;
  }
  if (!empty($bundleFields)) {

    // Build term path options.
    $term_path_options = array(
      'default' => t('Default'),
      'custom' => t('Custom patterns'),
      'api' => t('Ask modules using API'),
    );
    if (\Drupal::moduleHandler()
      ->moduleExists('taxonomy_menu')) {
      $term_path_options += array(
        'tm' => t('Ask Taxonomy Menu'),
      );
    }
    $type = $form_state
      ->getFormObject()
      ->getEntity();
    $default_value = $type
      ->getThirdPartySetting('taxonomy_menu_trails', 'taxonomy_menu_trails');
    $subform = array(
      '#tmt_entity' => $entity_type,
      '#tmt_bundle' => $bundle,
      'taxonomy_term_references' => array(
        '#type' => 'checkboxes',
        '#title' => t('Term references for setting active trail'),
        '#options' => $bundleFields,
        '#default_value' => $default_value['taxonomy_term_references'],
      ),
      'selection_method' => array(
        // TODO convert it to 'value' if there is one term reference with
        // Number of values == 1
        '#type' => 'select',
        '#title' => t("Term selection method"),
        '#description' => t("This option defines how module chooses term for processing:<ul><li><b>First/Last</b> - select first/last term with menu item.</li><li><b>Deepest in menu</b> - use term with deepest menu item.</ul>"),
        '#default_value' => $default_value['selection_method'],
        '#options' => array(
          'first' => t('First term'),
          'last' => t('Last term'),
          'deepest-in-menu' => t('Deepest in menu'),
        ),
      ),
      'terms_with_current_language' => \Drupal::moduleHandler()
        ->moduleExists('i18n_taxonomy') ? array(
        '#type' => 'checkbox',
        '#title' => t("Allow only terms with the current language"),
        '#description' => t("This option will check the taxonomy term language and allow only those terms that are localized into current content language."),
        '#default_value' => $default_value['terms_with_current_language'],
      ) : array(),
      'only_without_menu' => array(
        '#type' => 'checkbox',
        '#title' => t("Only if @entity doesn't have enabled menu item", array(
          '@entity' => \Drupal\Component\Utility\Unicode::strtolower($bundle_label),
        )),
        '#description' => t("This option also applies to pages detected with path pattern. You should think twice before turning this option on, because it'll reduce site performance a bit."),
        '#default_value' => $default_value['only_without_menu'],
      ),
      'trail_per_menu' => array(
        '#type' => 'checkbox',
        '#title' => t("Separate active trail for each menu"),
        '#description' => t("Makes it possible to expand multiple menus. Otherwise only one trail in the most preferred menu will be active."),
        '#default_value' => $default_value['trail_per_menu'],
      ),
      'set_breadcrumb' => array(
        '#type' => 'select',
        '#title' => t('Add active trail to the breadcrumb'),
        '#description' => t('Menu items from active trail will be inserted to the beginning of default breadcrumb when this option is enabled and the term was chosen.'),
        '#options' => array(
          'never' => t('Never'),
          'if_empty' => t('When breadcrumb is empty'),
          'always' => t('Always'),
        ),
        '#default_value' => $default_value['set_breadcrumb'],
      ),
      'term_path' => array(
        '#type' => 'select',
        '#title' => t('Term path'),
        '#options' => $term_path_options,
        '#default_value' => $default_value['term_path'],
      ),
      'term_path_patterns' => array(
        '#type' => 'textarea',
        '#title' => t('Term path patterns'),
        '#description' => t("Enter one pattern per line. Only [tid] placeholder is available. For complex path patterns use API instead. Default pattern taxonomy/term/[tid] is not included by default."),
        '#default_value' => implode("\n", $default_value['term_path_patterns']),
        '#states' => array(
          'visible' => array(
            'select[name="taxonomy_menu_trails[term_path]"]' => array(
              'value' => 'custom',
            ),
          ),
        ),
      ),
      'paths_ui' => array(
        '#title' => t('Additional path patterns for @entity detection', array(
          '@entity' => \Drupal\Component\Utility\Unicode::strtolower($bundle_label),
        )),
        '#type' => 'textarea',
        '#description' => t('Enter one pattern per line. By default module is trying to detect @entity at !default_paths. Here you can specify additional paths to detect @entity and set menu trails for it. The "*" character matches any non-empty string, the "%" character matches non-empty string without "/" character. Available placeholders are:<ul><li>[@id] - @entity ID</li><li>[@title] - @entity title</li></ul>Each additional pattern will be tested on each page load, so use it only if you really need this feature.', array(
          '@entity' => \Drupal\Component\Utility\Unicode::strtolower($bundle_label),
          '!default_paths' => $default_paths,
          '@id' => 'node',
          '@title' => 'title',
        )),
      ),
    );
    array_unshift($form['#submit'], 'taxonomy_menu_trails_bundle_form_submit');
  }
  else {
    $subform = array(
      'notice' => array(
        '#markup' => '<p>' . t('Add some "Term reference" fields to use Taxonomy Menu Trails.') . '</p>',
      ),
    );
  }
  $form['taxonomy_menu_trails'] = array(
    '#tree' => TRUE,
    '#type' => 'details',
    '#title' => t('Taxonomy menu trails'),
  ) + $subform;
  $form['#entity_builders'][] = 'taxonomy_menu_trails_form_node_type_form_builder';
}

/**
 * Entity builder for the node type form with menu options.
 */
function taxonomy_menu_trails_form_node_type_form_builder($entity_type, NodeTypeInterface $type, &$form, FormStateInterface $form_state) {
  $type
    ->setThirdPartySetting('taxonomy_menu_trails', 'taxonomy_menu_trails', array_filter($form_state
    ->getValue('taxonomy_menu_trails')));
}

Functions

Namesort descending Description
taxonomy_menu_trails_form_alter Implements hook_form_alter().
taxonomy_menu_trails_form_node_type_form_builder Entity builder for the node type form with menu options.
_taxonomy_menu_trails_alter_bundle_form Add Taxonomy Menu Trails settings to bundle form.