View source
<?php
include_once 'PowerMenuHandler.interface.php';
class PowerMenuTaxonomyHandler implements PowerMenuHandlerInterface {
public function configurationForm() {
$form = array();
$form['note'] = array(
'#markup' => '<p><strong>' . t('Choose the vocabulary which is going to reflect the structure of your menu.') . '</strong></p><p>' . t('You
can associate each taxonomy therm from the selected vocabulary to a menu link. Has an entity a taxonomy term from this vocabulary associated,
the plugin sets the active trail to the menu link associated with this taxonomy term.') . '</p>',
);
$taxonomies = taxonomy_get_vocabularies();
$options = array();
foreach ($taxonomies as $value) {
$options[$value->vid] = $value->name;
}
$vocabulary = variable_get('power_menu_taxonomy_vocabulary', array(
'vid' => NULL,
'machine_name' => NULL,
));
$form['power_menu_taxonomy']['vocabulary'] = array(
'#type' => 'select',
'#options' => $options,
'#default_value' => $vocabulary['vid'],
);
$form['power_menu_taxonomy']['default_enable_create_term'] = array(
'#title' => t('Enable creating a new term when making a new menu link, otherwise it must be enabled first'),
'#description' => t('Always enable the term creation when creating a new menu link.'),
'#type' => 'checkbox',
'#default_value' => variable_get('power_menu_taxonomy_default_enable_create_term', TRUE),
);
$form['power_menu_taxonomy']['create_term_always'] = array(
'#title' => t('Create always new terms'),
'#description' => t('Do always create a new term, also when a term with the name of the menu-item exists in the selected vocabulary.'),
'#type' => 'checkbox',
'#default_value' => variable_get('power_menu_taxonomy_create_term_always', FALSE),
);
$form['power_menu_taxonomy']['force_term_selection'] = array(
'#title' => t('Force term selection'),
'#description' => t('Always force the user to select a term or create a new one, on menu item creation or update.'),
'#type' => 'checkbox',
'#default_value' => variable_get('power_menu_taxonomy_force_term_selection', TRUE),
);
$form['power_menu_taxonomy']['show_menu_link_info'] = array(
'#title' => t('Show associated menu-link information'),
'#description' => t('Show the associated menu-link information on the term overview page.'),
'#type' => 'checkbox',
'#default_value' => variable_get('power_menu_taxonomy_show_menu_link_info', FALSE),
);
return $form;
}
public function configurationFormSubmit(array $form, array &$form_state) {
$vocabulary = taxonomy_vocabulary_load($form_state['values']['vocabulary']);
$vocabulary = array(
'vid' => $vocabulary->vid,
'machine_name' => $vocabulary->machine_name,
);
variable_set('power_menu_taxonomy_vocabulary', $vocabulary);
variable_set('power_menu_taxonomy_default_enable_create_term', $form_state['values']['default_enable_create_term']);
variable_set('power_menu_taxonomy_create_term_always', $form_state['values']['create_term_always']);
variable_set('power_menu_taxonomy_force_term_selection', $form_state['values']['force_term_selection']);
variable_set('power_menu_taxonomy_show_menu_link_info', $form_state['values']['show_menu_link_info']);
}
public function configurationFormValidate(array &$elements, array &$form_state, $form_id = NULL) {
}
public function getMenuPathToActivate($entity, $type, array $router_item, $alias) {
$path = NULL;
$mlid = NULL;
$terms = variable_get('power_menu_taxonomy_terms', array());
$entity_terms = PowerMenuTaxonomyHandler::getTaxonomyTermsFromEntity($entity, $type);
foreach ($entity_terms as $value) {
if (array_key_exists($value['tid'], $terms)) {
$mlid = $terms[$value['tid']];
}
}
if ($mlid != NULL) {
$menu_link = menu_link_load($mlid);
if ($menu_link) {
$path = $menu_link['link_path'];
}
}
return $path;
}
public function menuFormAlter(&$menu_item_form, &$form_state) {
$form = array();
$vocabulary = variable_get('power_menu_taxonomy_vocabulary', array(
'vid' => NULL,
'machine_name' => NULL,
));
if ($vocabulary['vid'] !== NULL) {
$vocabulary = taxonomy_vocabulary_load($vocabulary['vid']);
$vocabulary_name = $vocabulary->name;
$terms = taxonomy_get_tree($vocabulary->vid);
$mlid = arg(4);
$used_terms = variable_get('power_menu_taxonomy_terms', array());
foreach ($used_terms as $key => $value) {
if ($mlid != $value) {
unset($used_terms[$key]);
}
}
$used_terms = array_keys($used_terms);
if ($terms) {
foreach ($terms as $term) {
$choice = new stdClass();
$choice->option = array(
$term->tid => str_repeat('-', $term->depth) . ' ' . $term->name,
);
$options[] = $choice;
}
}
$form['power_menu_taxonomy_create'] = array(
'#type' => 'checkbox',
'#title' => t('Create Taxonomy term'),
'#default_value' => $menu_item_form['mlid']['#value'] == 0 && variable_get('power_menu_taxonomy_default_enable_create_term', TRUE) ? TRUE : FALSE,
'#description' => t('The name of the taxonomy term is going to be the title of the menu link. When it\'s possible, the same hierarchy as the menu-item is used.', array(
'%vocabulary' => $vocabulary_name,
)),
);
$form['power_menu_taxonomy_terms'] = array(
'#type' => 'select',
'#title' => t('Link to existing term'),
'#multiple' => TRUE,
'#options' => $options,
'#default_value' => $used_terms,
'#description' => t('Choose a taxonomy term. When displaying a node that has the selected taxonomy term, this menu item will set to active. A term can only belong to one menu item and is a term not selectable it is already asigned to other menu items.'),
'#post_render' => array(
'power_menu_taxonomy_terms_post_render',
),
);
}
else {
$form['power_menu_taxonomy'] = array(
'#markup' => t('No menu vocabulary selected. !config_page', array(
'!config_page' => l(t('Go to the plugin configuration page.'), 'admin/config/search/power_menu/handler/edit/taxonomy'),
)),
);
}
return $form;
}
public function menuFormValidate(array &$elements, array &$form_state, $form_id = NULL) {
if (variable_get('power_menu_taxonomy_force_term_selection', TRUE)) {
if (empty($form_state['values']['power_menu_taxonomy_create']) && empty($form_state['values']['power_menu_taxonomy_terms'])) {
form_set_error('power_menu_taxonomy_create', t('Select \'Create Taxonomy term\' or select one from the existing terms.'));
form_set_error('power_menu_taxonomy_terms');
}
}
}
public function menuFormSubmit(array $form, array &$form_state) {
$terms = variable_get('power_menu_taxonomy_terms', array());
$vocabulary = variable_get('power_menu_taxonomy_vocabulary', array(
'vid' => NULL,
'machine_name' => NULL,
));
if ($vocabulary['vid'] !== NULL && !empty($form_state['values']['power_menu_taxonomy_create'])) {
$term_name = $form_state['values']['link_title'];
$term = db_select('taxonomy_term_data', 't')
->fields('t', array(
'tid',
))
->condition('t.name', $term_name)
->condition('t.vid', $vocabulary['vid'])
->execute()
->fetchField();
if (!$term || variable_get('power_menu_taxonomy_create_term_always', FALSE)) {
$term = new stdClass();
$term->vid = $vocabulary['vid'];
$term->name = $term_name;
if (!empty($form_state['values']['parent'])) {
$mlid = explode(':', $form_state['values']['parent']);
$parent_term = array_search($mlid[1], $terms);
if ($parent_term) {
$term->parent = $parent_term;
}
}
taxonomy_term_save($term);
$form_state['values']['power_menu_taxonomy_terms'][] = $term->tid;
}
}
if (isset($form_state['values']['power_menu_taxonomy_terms'])) {
$selected_terms = $form_state['values']['power_menu_taxonomy_terms'];
$mlid = $form_state['values']['mlid'];
foreach ($terms as $key => $value) {
if ($value == $mlid) {
unset($terms[$key]);
}
}
foreach ($selected_terms as $value) {
$terms[$value] = $mlid;
}
}
variable_set('power_menu_taxonomy_terms', $terms);
}
public function menuLinkDelete(array $link) {
$terms = variable_get('power_menu_taxonomy_terms', array());
$terms_to_save = array();
foreach ($terms as $tid => $mlid) {
if ($mlid != $link['mlid']) {
$terms_to_save[$tid] = $mlid;
}
}
variable_set('power_menu_taxonomy_terms', $terms_to_save);
}
public static function getTaxonomyFieldsFromEntity($type = FALSE) {
$vocabulary = variable_get('power_menu_taxonomy_vocabulary', array(
'vid' => NULL,
'machine_name' => NULL,
));
$fileds = field_info_fields();
foreach ($fileds as $field_name => $value) {
if ($value['type'] != 'taxonomy_term_reference' || $value['settings']['allowed_values'][0]['vocabulary'] != $vocabulary['machine_name'] || $type && empty($value['bundles'][$type])) {
unset($fileds[$field_name]);
}
}
return $fileds;
}
public static function getTaxonomyTermsFromEntity($entity, $type) {
$terms = array();
$fileds = PowerMenuTaxonomyHandler::getTaxonomyFieldsFromEntity($type);
foreach ($fileds as $field_name => $value) {
if (!empty($entity->{$field_name})) {
$items = field_get_items($type, $entity, $field_name);
$terms = array_merge($terms, $items);
}
}
return $terms;
}
}