taxonomy_menu_custom_paths.module in Taxonomy menu 8
Same filename and directory in other branches
Enables custom paths to Taxonomy Menu.
File
taxonomy_menu_custom_paths/taxonomy_menu_custom_paths.moduleView source
<?php
/**
* @file
* Enables custom paths to Taxonomy Menu.
*/
/**
* Implements hook_taxonomy_menu_vocabulary_settings().
*/
function taxonomy_menu_custom_paths_taxonomy_menu_vocabulary_settings() {
return array(
'custom_path_base' => '',
'custom_path_depth' => '',
);
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Adds Taxonomy menu custom path settings on a per-vocabulary basis.
*
* @see taxonomy_menu_custom_paths_vocabulary_validate()
*/
function taxonomy_menu_custom_paths_form_taxonomy_vocabulary_form_alter(&$form, &$form_state) {
// Get the vocabulary ID.
$vid = $form_state['controller']
->getEntity()->vid ?: 0;
$form['taxonomy_menu']['options_custom_path'] = array(
'#type' => 'details',
'#title' => t('Custom path options'),
'#collapsed' => TRUE,
'#description' => t("<strong>Warning:</strong> These settings uses custom paths that are not registered in Drupal by default. You need to register them <strong>before</strong> you create the taxonomy menu. For example, you could use a view in order to do that."),
'#states' => array(
'visible' => array(
':input[name="taxonomy_menu[path]"]' => array(
'value' => 'taxonomy_menu_path_custom',
),
),
),
);
$form['taxonomy_menu']['options_custom_path']['custom_path_base'] = array(
'#title' => t('Base path'),
'#type' => 'textfield',
'#default_value' => taxonomy_menu_variable_get('custom_path_base', $vid, ''),
'#description' => t("Path 'base_path/%' must be available, where the argument is a Term ID (tid)."),
);
$form['taxonomy_menu']['options_custom_path']['custom_path_depth'] = array(
'#title' => t('Depth modifier'),
'#description' => t("Path 'base_path/%/%' must be available, where the arguments are respectively a Term ID (tid) and a Depth Modifier."),
'#default_value' => taxonomy_menu_variable_get('custom_path_depth', $vid, ''),
'#type' => 'textfield',
);
// Add a custom validation function.
array_unshift($form['#validate'], 'taxonomy_menu_custom_paths_vocabulary_validate');
}
/**
* Validation handler for settings of custom path in the taxonomy vocabulary.
*
* We make sure that a base path is provided and paths are registered.
*
* @see taxonomy_form_vocabulary()
*/
function taxonomy_menu_custom_paths_vocabulary_validate($form, $form_state) {
$options = $form_state['values']['taxonomy_menu'];
$base = $options['options_custom_path']['custom_path_base'];
$depth = $options['options_custom_path']['custom_path_depth'];
// Don't allow base path value to be empty when a custom path is selected.
// Check that the required path are registered in Drupal before processing.
if ($options['path'] == 'taxonomy_menu_path_custom') {
if (empty($base)) {
\Drupal::formBuilder()
->setErrorByName('custom_path_base', $form_state, 'A base path must be provided when using a custom path.');
}
elseif (empty($depth)) {
$path = \Drupal::service('path.alias_manager.cached')
->getSystemPath($base . '/%');
if (!drupal_valid_path($path)) {
\Drupal::formBuilder()
->setErrorByName('custom_path_base', $form_state, 'The path ' . $path . ' is not available in Drupal. This is required to use custom paths.');
}
}
else {
$path = \Drupal::service('path.alias_manager.cached')
->getSystemPath($base . '/%/%');
if (!drupal_valid_path($path)) {
\Drupal::formBuilder()
->setErrorByName('custom_path_depth', $form_state, 'The path ' . $path . ' is not available in Drupal. This is required to use custom paths.');
}
}
}
}
/**
* Implements hook_taxonomy_menu_path().
*/
function taxonomy_menu_custom_paths_taxonomy_menu_path() {
return array(
'taxonomy_menu_path_custom' => t('Custom (<base_path>/%)'),
);
}
/**
* Callback for hook_taxonomy_menu_path.
*/
function taxonomy_menu_path_custom($term) {
$path = '';
$tids = array();
$base_path = taxonomy_menu_variable_get('custom_path_base', $term
->bundle(), '');
$depth = taxonomy_menu_variable_get('custom_path_depth', $term
->bundle(), '');
// When tid equals 0, we are dealing with a vocabulary item. We want the path
// to be a mulitple term path.
if ($term
->id() == 0) {
$tids = _taxonomy_menu_get_tids($term
->bundle());
}
else {
$tids[] = $term
->id();
$terms = taxonomy_get_tree($term
->bundle(), $term
->id());
foreach ($terms as $term) {
$tids[] = $term->tid;
}
}
// Build the path.
if ($tids) {
$path = $base_path . '/' . implode('+', $tids);
}
else {
$path = $base_path . '/' . $term
->id();
}
return $depth != '' ? $path . '/' . $depth : $path;
}
Functions
Name | Description |
---|---|
taxonomy_menu_custom_paths_form_taxonomy_vocabulary_form_alter | Implements hook_form_FORM_ID_alter(). |
taxonomy_menu_custom_paths_taxonomy_menu_path | Implements hook_taxonomy_menu_path(). |
taxonomy_menu_custom_paths_taxonomy_menu_vocabulary_settings | Implements hook_taxonomy_menu_vocabulary_settings(). |
taxonomy_menu_custom_paths_vocabulary_validate | Validation handler for settings of custom path in the taxonomy vocabulary. |
taxonomy_menu_path_custom | Callback for hook_taxonomy_menu_path. |