View source
<?php
include_once 'includes/views.inc';
function power_menu_permission() {
return array(
'administer power menu' => array(
'title' => t('Administer power menu configuration'),
),
);
}
function power_menu_menu() {
$items['admin/config/search/power_menu'] = array(
'title' => 'Power Menu configuration',
'description' => 'Configure power menu plugins.',
'access arguments' => array(
'administer power menu',
),
'page arguments' => array(
'power_menu_configuration_form',
),
'page callback' => 'drupal_get_form',
'file' => 'power_menu.admin.inc',
);
$items['admin/config/search/power_menu/handler'] = array(
'title' => 'Menu handlers',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
);
$items['admin/config/search/power_menu/handler/edit/%'] = array(
'title' => 'Power Menu configuration',
'description' => 'Configure power menu plugin.',
'access arguments' => array(
'administer power menu',
),
'page arguments' => array(
'power_menu_configuration_handler_form',
6,
),
'page callback' => 'drupal_get_form',
'type' => MENU_NORMAL_ITEM,
'file' => 'power_menu.admin.inc',
);
$items['admin/config/search/power_menu/fields'] = array(
'title' => 'Menu fields',
'description' => 'Configure power menu fields.',
'access arguments' => array(
'administer power menu',
),
'page arguments' => array(
'power_menu_fields_configuration_form',
),
'page callback' => 'drupal_get_form',
'type' => MENU_LOCAL_TASK,
'file' => 'power_menu.admin.inc',
'weight' => 0,
);
return $items;
}
function power_menu_menu_alter(&$items) {
$menus = variable_get('power_menu_fields_menus', array());
foreach ($menus as $key => $value) {
$value = power_menu_create_machine_name($value);
$items['admin/config/search/power_menu/fields/' . $value . '/fields']['type'] = MENU_CALLBACK;
$items['admin/config/search/power_menu/fields/' . $value . '/display']['type'] = MENU_CALLBACK;
}
}
function power_menu_help($path, $arg) {
switch ($path) {
case 'admin/config/search/power_menu':
$output = '<p>' . t('The power menu allows you to set active menu items based on several plugins. This is mainly necessary when nodes not "connected" with a menu item. The power menu also provides fields for every menu entry.') . '</p>';
$output .= '<p>' . t('On this settings page, you can define in which order the menu handler should process.') . '</p>';
return $output;
case 'admin/config/search/power_menu/fields':
$output = '<p>' . t('The power menu allows you to set active menu items based on several plugins. This is mainly necessary when nodes not "connected" with a menu item. The power menu also provides fields for every menu entry.') . '</p>';
$output .= '<p>' . t('On this settings page, you can define which menu should have fields.') . '</p>';
return $output;
}
}
function power_menu_theme() {
return array(
'power_menu_plugins_order' => array(
'render element' => 'element',
'file' => 'power_menu.admin.inc',
),
);
}
function power_menu_ctools_plugin_type() {
return array(
'menu_handlers' => array(
'classes' => array(
'handler',
),
),
);
}
function power_menu_handlers_weight_compare($a, $b) {
if ($a['weight'] == $b['weight']) {
return 0;
}
return $a['weight'] < $b['weight'] ? -1 : 1;
}
function power_menu_get_menu_handlers($only_enabled = FALSE) {
ctools_include('plugins');
$handlers = ctools_get_plugins('power_menu', 'menu_handlers');
$ordered_handlers = array();
$settings = variable_get('power_menu_handlers_settings', array());
uasort($settings, 'power_menu_handlers_weight_compare');
foreach ($settings as $key => $value) {
if ((!$only_enabled || $value['enabled']) && !empty($handlers[$key])) {
$ordered_handlers[$key] = $handlers[$key];
}
unset($handlers[$key]);
}
if ($only_enabled) {
return $ordered_handlers;
}
else {
return array_merge($ordered_handlers, $handlers);
}
}
function power_menu_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'power_menu') {
return 'plugins/' . $plugin_type;
}
}
function power_menu_entity_view($entity, $type, $view_mode, $langcode) {
$defined_view_modes = variable_get('power_menu_view_mode', 'full');
if (is_array($defined_view_modes)) {
$set_path = in_array($view_mode, $defined_view_modes) ? TRUE : FALSE;
}
else {
$set_path = $view_mode == $defined_view_modes ? TRUE : FALSE;
}
if ($set_path && variable_get('power_menu_handlers_enabled', FALSE)) {
power_menu_set_path($entity, $type, $langcode);
}
}
function power_menu_set_path($entity, $type, $langcode) {
$path = power_menu_get_path($entity, $type);
if ($path == '<front>') {
$path = variable_get('site_frontpage', 'node');
menu_set_active_item($path);
}
elseif (preg_match('/^<[A-Za-z0-9]*>$/', $path)) {
}
else {
foreach (variable_get('power_menu_handlers_menus', array()) as $menu_name) {
menu_tree_set_path($menu_name, $path);
}
if (variable_get('power_menu_handlers_breadcrumb', TRUE) && isset($path)) {
$breadcrumbs = power_menu_get_breadcrumbs($path);
if (variable_get('power_menu_handlers_breadcrumb_title', FALSE) && !empty($entity->title)) {
$breadcrumbs[] = $entity->title;
}
drupal_set_breadcrumb($breadcrumbs);
}
}
return $path;
}
function power_menu_get_path($entity, $type, $defined_path = NULL, $use_cache = TRUE) {
$uri = entity_uri($type, $entity);
global $language;
$cache_key = "handler:{$language->language}:{$uri['path']}";
$cached_path = cache_get($cache_key, 'cache_power_menu');
$path = $use_cache && $cached_path ? $cached_path->data : FALSE;
if (!$path) {
$router_item = menu_get_item($defined_path);
if (!$router_item) {
return $path;
}
$alias = drupal_get_path_alias($router_item['href']);
foreach (power_menu_get_menu_handlers(TRUE) as $handler) {
$instance = power_menu_plugin_get_handler_instance($handler);
if ($instance) {
$path = $instance
->getMenuPathToActivate($entity, $type, $router_item, $alias);
if (!empty($path)) {
break;
}
}
}
cache_set($cache_key, $path, 'cache_power_menu');
return $path;
}
return $path;
}
function power_menu_plugin_get_handler_instance($handler) {
$class_name = ctools_plugin_get_class($handler, 'handler');
if ($class_name) {
$instance = new $class_name();
if ($instance instanceof PowerMenuHandlerInterface) {
return $instance;
}
}
return FALSE;
}
function power_menu_plugin_get_handler_instance_by_name($handler_name) {
$handlers = power_menu_get_menu_handlers();
if (!empty($handlers[$handler_name])) {
$handler = $handlers[$handler_name];
return power_menu_plugin_get_handler_instance($handler);
}
return FALSE;
}
function power_menu_get_breadcrumbs($path) {
$crumbs = array(
l(t('Home'), '<front>'),
);
$menu = db_select('menu_links', 'ml')
->fields('ml', array(
'menu_name',
))
->condition('ml.link_path', $path, '=')
->execute()
->fetchField();
if ($menu) {
$tree = menu_tree_page_data($menu);
if (function_exists('i18n_menu_localize_tree')) {
$tree = i18n_menu_localize_tree($tree);
}
_power_menu_recurse_crumbs($tree, $path, $crumbs);
}
return $crumbs;
}
function _power_menu_recurse_crumbs($tree, $path, &$crumbs, $above = array()) {
foreach ($tree as $menu_item) {
if (!$menu_item['link']['in_active_trail']) {
continue;
}
if ($menu_item['link']['link_path'] == $path) {
foreach ($above as $trail_item) {
$crumbs[] = l($trail_item['link']['title'], $trail_item['link']['link_path']);
}
$crumbs[] = l($menu_item['link']['title'], $menu_item['link']['link_path']);
break;
}
if (is_array($menu_item['below'])) {
_power_menu_recurse_crumbs($menu_item['below'], $path, $crumbs, array_merge($above, array(
$menu_item,
)));
}
}
}
function power_menu_form_menu_edit_item_alter(&$form, &$form_state) {
$mlid = $form['mlid']['#value'];
if ($mlid) {
$menu_link = menu_link_load($mlid);
$menu_name = $menu_link['menu_name'];
}
else {
$menu_name = $form['original_item']['#value']['menu_name'];
$mlid = NULL;
}
$menus = variable_get('power_menu_handlers_menus', array());
if (in_array($menu_name, $menus)) {
$handlers = power_menu_get_menu_handlers(TRUE);
if ($handlers) {
foreach ($handlers as $handler) {
$instance = power_menu_plugin_get_handler_instance($handler);
if ($instance) {
$handler_form = $instance
->menuFormAlter($form, $form_state);
if (is_array($handler_form)) {
$form['power_menu']['handlers'][$handler['name']] = array(
'#type' => 'fieldset',
'#title' => t($handler['title']),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['power_menu']['handlers'][$handler['name']]['sa'] = $handler_form;
}
}
}
if (!empty($form['power_menu']['handlers'])) {
$form['#submit'][] = 'power_menu_form_menu_edit_item_handlers_submit';
$form['#validate'][] = 'power_menu_form_menu_edit_item_handlers_validate';
$form['power_menu']['handlers']['#type'] = 'fieldset';
$form['power_menu']['handlers']['#title'] = t('Power Menu Handlers');
$form['power_menu']['handlers']['#collapsible'] = TRUE;
$form['power_menu']['handlers']['#collapsed'] = FALSE;
}
}
}
power_menu_add_fields_entity_fields($menu_name, $mlid, $form, $form_state);
}
function power_menu_form_menu_overview_form_alter(&$form, &$form_state) {
power_menu_add_fields_entity_fields($form['#menu']['menu_name'], 0, $form, $form_state);
}
function power_menu_menu_link_delete($link) {
$args = array(
$link,
);
_power_menu_call_handler_function('menuLinkDelete', $args);
$id = power_menu_get_fields_entity_id_by_mlid($link['mlid']);
if ($id) {
entity_delete('power_menu_fields', $id);
}
}
function power_menu_menu_delete($menu) {
$id = power_menu_get_fields_entity_id_by_menu($menu['menu_name']);
if ($id) {
entity_delete('power_menu_fields', $id);
}
}
function power_menu_add_fields_entity_fields($menu_name, $mlid, &$form, &$form_state) {
$bundle_name = power_menu_create_machine_name($menu_name);
$selected_menus = variable_get('power_menu_fields_menus', array());
if (in_array($menu_name, $selected_menus)) {
$form['#submit'][] = 'power_menu_form_menu_edit_fields_submit';
$form['#validate'][] = 'power_menu_form_menu_edit_fields_validate';
$form['power_menu']['fields'] = array(
'#type' => 'fieldset',
'#title' => t('Power Menu fields'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#tree' => TRUE,
'#parents' => array(
'power_menu',
'fields',
),
);
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'power_menu_fields')
->propertyCondition('mlid', $mlid)
->propertyCondition('menu_name', $bundle_name);
$result = $query
->execute();
if (empty($result)) {
$entity = entity_create('power_menu_fields', array(
'menu_name' => $bundle_name,
'mlid' => $mlid,
));
}
else {
$entity_nid = array_keys($result['power_menu_fields']);
$entity = entity_load('power_menu_fields', $entity_nid);
$entity = reset($entity);
}
$form['#parents'] = array(
'power_menu',
'fields',
);
field_attach_form('power_menu_fields', $entity, $form['power_menu']['fields'], $form_state);
$form['power_menu']['fields']['entity'] = array(
'#type' => 'value',
'#value' => $entity,
);
}
}
function power_menu_form_menu_edit_item_handlers_validate(&$form, &$form_state, $form_id = NULL) {
$args = array(
&$form,
&$form_state,
);
_power_menu_call_handler_function('menuFormValidate', $args, TRUE);
}
function power_menu_form_menu_edit_item_handlers_submit($form, &$form_state) {
$args = array(
$form,
&$form_state,
);
_power_menu_call_handler_function('menuFormSubmit', $args, TRUE);
}
function power_menu_form_menu_edit_fields_validate(&$form, &$form_state, $form_id = NULL) {
}
function power_menu_form_menu_edit_fields_submit($form, &$form_state) {
if (isset($form_state['values']['power_menu']['fields']['entity'])) {
$entity = $form_state['values']['power_menu']['fields']['entity'];
unset($form_state['values']['menu_name']);
entity_form_submit_build_entity('power_menu_fields', $entity, $form, $form_state);
$entity
->save();
if (isset($form_state['values']['link_path'])) {
cache_clear_all('fields:' . $form_state['values']['link_path'], 'cache_power_menu');
}
elseif ($entity->mlid == 0) {
cache_clear_all('fields:/', 'cache_power_menu');
}
if (isset($form_state['values']['mlid'])) {
cache_clear_all('fields:mlid:' . $form_state['values']['mlid'], 'cache_power_menu');
}
cache_clear_all('power_menu:power_menu_field_display', 'cache_block', TRUE);
}
}
function _power_menu_call_handler_function($function, &$args, $only_enabled = FALSE) {
$handlers = power_menu_get_menu_handlers($only_enabled);
foreach ($handlers as $handler) {
$instance = power_menu_plugin_get_handler_instance($handler);
if ($instance) {
call_user_func_array(array(
&$instance,
$function,
), $args);
}
}
}
function power_menu_get_entities_and_bundles() {
$entity_infos = entity_get_info();
$bundles = array();
foreach ($entity_infos as $entity_key => $entity) {
if (empty($entity['uri callback'])) {
continue;
}
foreach ($entity['bundles'] as $bundle_key => $bundle) {
$bundles[$entity_key . '|' . $bundle_key] = $entity['label'] . ' : ' . $bundle['label'];
}
}
return $bundles;
}
function power_menu_flush_caches() {
return array(
'cache_power_menu',
);
}
function power_menu_entity_info() {
$menus = variable_get('power_menu_fields_menus', array());
$bundles = array();
foreach ($menus as $key => $value) {
$value = power_menu_create_machine_name($value);
$bundles[$value] = array(
'admin' => array(
'path' => 'admin/config/search/power_menu/fields/' . $value,
'access arguments' => array(
'administer power menu',
),
),
'label' => $value,
);
}
$info['power_menu_fields'] = array(
'label' => t('Power Menu fields'),
'controller class' => 'EntityAPIController',
'base table' => 'power_menu_fields',
'uri callback' => 'entity_class_uri',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'id',
'bundle' => 'menu_name',
),
'bundle keys' => array(
'bundle' => 'menu_name',
),
'bundles' => $bundles,
'entity class' => 'Entity',
'module' => 'power_menu',
);
return $info;
}
function power_menu_get_fields_entity($path = NULL, $fallback = TRUE, $menu_name = 'main-menu') {
$menu_name_orig = $menu_name;
$menu_name = power_menu_create_machine_name($menu_name);
if (!isset($path)) {
$path = implode('/', arg());
}
$cache_path_key = 'path:' . $path;
$cached_path = cache_get($cache_path_key, 'cache_power_menu');
$lookup_path = $cached_path ? $cached_path->data : $path;
$cache_key = 'fields:' . $lookup_path;
$entity = cache_get($cache_key, 'cache_power_menu');
$entity = $entity ? $entity->data : NULL;
if (!$entity) {
if ($lookup_path == '/' && $fallback) {
$id = power_menu_get_fields_entity_id_by_menu($menu_name);
}
else {
if (variable_get('site_frontpage', 'node') == $lookup_path) {
$lookup_path = '<front>';
}
$query = db_select('power_menu_fields', 'pmf');
$query
->leftJoin('menu_links', 'ml', 'pmf.mlid = ml.mlid');
$id = $query
->fields('pmf', array(
'id',
))
->condition('ml.link_path', $lookup_path)
->condition('ml.menu_name', $menu_name_orig)
->execute()
->fetchField();
}
if (!$id && $fallback && $lookup_path != '/') {
$entity = power_menu_get_fields_entity('/', TRUE, $menu_name);
$lookup_path = '/';
}
elseif ($id) {
$entity = entity_load('power_menu_fields', array(
$id,
));
$entity = $entity ? array_pop($entity) : NULL;
}
cache_set($cache_path_key, $lookup_path, 'cache_power_menu');
cache_set('fields:' . $lookup_path, $entity, 'cache_power_menu');
}
return $entity;
}
function power_menu_block_info() {
$blocks = array();
$blocks['power_menu_field_display'] = array(
'info' => t('Power Menu field display'),
'cache' => DRUPAL_CACHE_PER_PAGE,
);
return $blocks;
}
function power_menu_block_configure($delta) {
$form = array();
switch ($delta) {
case 'power_menu_field_display':
$menus = menu_get_menus();
$entity_info = entity_get_info('power_menu_fields');
$view_modes = array(
'default' => t('Default'),
);
foreach ($entity_info['view modes'] as $key => $view_mode) {
$view_modes[$key] = $view_mode['label'];
}
$form['power_menu_fields_menu'] = array(
'#type' => 'select',
'#title' => t('Display Menu'),
'#default_value' => variable_get('power_menu_field_display_block_menu', 'main-menu'),
'#options' => $menus,
'#description' => t('Select the menu from which the <a href="/admin/config/search/power_menu/fields">display</a> should be used.'),
);
$form['power_menu_fields_view_mode'] = array(
'#type' => 'select',
'#title' => t('Display View mode'),
'#default_value' => variable_get('power_menu_field_display_block_view_mode', 'default'),
'#options' => $view_modes,
'#description' => t('Select the view mode which should be used to render the Power Menu field entity.'),
);
}
return $form;
}
function power_menu_block_save($delta, $edit) {
switch ($delta) {
case 'power_menu_field_display':
variable_set('power_menu_field_display_block_menu', $edit['power_menu_fields_menu']);
variable_set('power_menu_field_display_block_view_mode', $edit['power_menu_fields_view_mode']);
}
}
function power_menu_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'power_menu_field_display':
$content = '';
$field_entity = power_menu_get_fields_entity(NULL, TRUE, variable_get('power_menu_field_display_block_menu', 'main-menu'));
if ($field_entity) {
$content = entity_view('power_menu_fields', array(
$field_entity,
), variable_get('power_menu_field_display_block_view_mode', 'default'), NULL, TRUE);
}
$block['content'] = $content;
break;
}
return $block;
}
function power_menu_get_fields_entity_by_mlid($mlid) {
$cache_key = 'fields:mlid:' . $mlid;
$entity = cache_get($cache_key, 'cache_power_menu');
$entity = $entity ? $entity->data : NULL;
if (!$entity) {
$id = power_menu_get_fields_entity_id_by_mlid($mlid);
if ($id) {
$entity = entity_load('power_menu_fields', array(
$id,
));
$entity = $entity ? array_pop($entity) : NULL;
}
cache_set($cache_key, $entity, 'cache_power_menu');
}
return $entity;
}
function power_menu_get_fields_entity_id_by_mlid($mlid) {
$query = db_select('power_menu_fields', 'pmf');
$id = $query
->fields('pmf', array(
'id',
))
->condition('pmf.mlid', $mlid)
->execute()
->fetchField();
return $id;
}
function power_menu_get_fields_entity_id_by_menu($menu_name) {
$bundle_name = power_menu_create_machine_name($menu_name);
$id = db_select('power_menu_fields', 'pmf')
->fields('pmf', array(
'id',
))
->condition('pmf.mlid', 0)
->condition('pmf.menu_name', $bundle_name)
->execute()
->fetchField();
return $id;
}
function power_menu_features_pipe_field_alter(&$pipe, $data, $export) {
foreach ($data as $identifier) {
if (strpos($identifier, 'power_menu_fields') === 0) {
$pipe['dependencies'][] = 'power_menu';
break;
}
}
}
function power_menu_create_machine_name($value) {
return $value = str_replace('-', '_', $value);
}
function power_menu_form_taxonomy_overview_terms_alter(&$form, &$form_state) {
$show_info = variable_get('power_menu_taxonomy_show_menu_link_info', FALSE);
$vocabulary = variable_get('power_menu_taxonomy_vocabulary', array(
'vid' => NULL,
'machine_name' => NULL,
));
if ($show_info && isset($form['#vocabulary']->vid) && $form['#vocabulary']->vid == $vocabulary['vid']) {
$terms = variable_get('power_menu_taxonomy_terms', array());
foreach ($form as &$value) {
if (is_array($value) && isset($value['#term']['name'])) {
$tid = $value['#term']['tid'];
if (array_key_exists($tid, $terms)) {
$value['view']['link'] = $value['view'];
unset($value['view']['#type']);
$menu_link = menu_link_load($terms[$tid]);
$title = l($menu_link['link_title'], $menu_link['link_path']);
$title = strlen($title) != 0 ? $title : $menu_link['title'];
$options = array(
'!title' => $title,
'!edit' => l(t('edit'), 'admin/structure/menu/item/' . $menu_link['mlid'] . '/edit'),
);
$value['view']['menu_information']['#type'] = 'markup';
$value['view']['menu_information']['#markup'] = ' - ' . t('Associated menu link: !title (!edit)', $options);
}
}
}
}
}