View source
<?php
function _content_menu_form_menu_overview_form_alter(&$form, &$form_state, $form_id) {
if (!empty($form['inline_actions']['add'])) {
unset($form['inline_actions']['add']);
}
$form['#theme'][0] = 'menu_overview_form_extended';
$filtering_active = TRUE;
$filtering_active = $filtering_active && module_exists('ctools');
if ($filtering_active) {
require_once 'filters/content_menu_domain_filter.php';
require_once 'filters/content_menu_language_filter.php';
$filters = array(
new content_menu_domain_filter(),
new content_menu_language_filter($form['#menu']['menu_name']),
);
foreach ($filters as $f) {
$f
->addFilterWidget($form, $form_state, $form_id);
}
}
$hidden = array();
foreach (element_children($form) as $mlid) {
$el =& $form[$mlid];
if ($filtering_active) {
foreach ($filters as $f) {
if ($f
->hideElement($el)) {
unset($form[$mlid]);
$hidden[$el['#item']['mlid']] = $el['#item']['mlid'];
continue;
}
}
}
if (isset($el['#item']['plid']) && isset($hidden[$el['#item']['plid']])) {
unset($form[$mlid]);
$hidden[$el['#item']['mlid']] = $el['#item']['mlid'];
continue;
}
$el['#inline_edit_title'] = TRUE;
if (isset($form[$mlid]['operations'])) {
drupal_alter('menu_item_element', $el);
uasort($el['operations'], 'drupal_sort_weight');
foreach ($el['operations'] as $el_op_key => $el_op) {
unset($el['operations'][$el_op_key]['weight']);
}
if (module_exists('ctools') && isset($el['operations'])) {
$other_operations = array();
$dropbutton_operations = array();
foreach ($form[$mlid]['operations'] as $op_key => $op_item) {
if (is_array($op_item) && isset($op_item['#type'])) {
if ($op_item['#type'] == 'link') {
$dropbutton_operations[$op_key] = array(
'title' => $op_item['#title'],
'href' => $op_item['#href'],
);
if (isset($op_item['#options'])) {
$dropbutton_operations[$op_key] += $op_item['#options'];
}
}
else {
$other_operations[$op_key] = $op_item;
}
}
}
$el['operations'] = array();
$el['operations']['dropbutton_operations'] = array(
'#theme' => 'links__ctools_dropbutton',
'#links' => $dropbutton_operations,
);
$el['operations'] += $other_operations;
}
$el['#operations_count'] = count($form[$mlid]['operations']);
}
if ($el['#inline_edit_title'] && isset($el['title'])) {
$title = $el['#item']['title'];
$el['title'] = array(
'#type' => 'textfield',
'#default_value' => $title,
'#size' => 30,
'#maxlength' => 255,
'#prefix' => '<span class="menu-overview-title-link">' . $el['title']['#markup'] . '</span>',
'#suffix' => '<a href="javascript:void();" title="Click to edit title" class="menu-overview-title-edit-trigger">' . t('edit title') . '</a>',
);
$form['#attached']['js'] = array(
drupal_get_path('module', 'content_menu') . '/content_menu.js',
);
}
}
content_menu_form_menu_overview_form_alter_additem($form);
drupal_add_css(drupal_get_path('module', 'content_menu') . "/css/content_menu.css", array(
'weight' => 2,
));
$form['#submit'] = array(
'content_menu_menu_overview_form_submit_extended',
);
unset($_GET['destination']);
if (!isset($form['actions'])) {
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
}
$form['actions']['submit']['#value'] = t('Save');
}
function _content_menu_menu_item_element_alter(&$el) {
$el_item =& $el['#item'];
$el_ops =& $el['operations'];
$target_content = '';
$link_path = $el_item['link_path'] == '<front>' ? config('system.site')
->get('page.front') : $el_item['link_path'];
$link_path = (array) explode('/', $link_path);
$link_path = array_pad($link_path, 2, '');
if (isset($el_ops['edit']) && $el_ops['edit']['#type'] == 'link') {
$el_ops['edit']['weight'] = 1;
$selectcontenturl = content_menu_variable_get_add_existing_content_url();
if ($selectcontenturl) {
$el_ops['selectcontent'] = array(
'#type' => 'link',
'weight' => 2,
'#title' => t('select content'),
'#href' => $selectcontenturl,
'#options' => array(
'query' => content_menu_assemble_query_string($el_item),
),
);
}
if ($link_path[0] == 'menu-dummy') {
$node_type_count = 0;
foreach (node_type_get_types() as $node_type) {
$type = $node_type->type;
if (user_access('create ' . $type . ' content')) {
$node_menu_options = variable_get('menu_options_' . $type, array(
'main-menu' => 'main-menu',
));
if (in_array($el_item['menu_name'], $node_menu_options)) {
$el_ops['new_' . $type] = array(
'#type' => 'link',
'weight' => 10 + $node_type_count,
'#title' => t('New @type', array(
'@type' => t($node_type->name),
)),
'#href' => 'node/add/' . str_replace("_", "-", $type),
'#options' => array(
'query' => content_menu_assemble_query_string($el_item),
),
);
}
}
}
}
$el_ops['configure'] = $el_ops['edit'];
$el_ops['configure']['#title'] = t('configure item');
$el_ops['configure']['weight'] = 3;
if ($link_path[0] == 'menu-dummy') {
$target_content = t('Placeholder');
}
else {
if ($link_path[0] == 'node' && is_numeric($link_path[1])) {
$node = node_load($link_path[1]);
if (is_object($node)) {
if (node_access('update', $node)) {
$el_ops['edit']['#href'] = 'node/' . $link_path[1] . '/edit';
}
$node_type = node_type_get_type($node);
$target_content = t($node_type->name);
if (!$node->status) {
$target_content = '<span class="menu-item-content-target-unpublished">' . $target_content . '</span>';
}
}
}
else {
if ($el_item['page_callback'] == 'page_manager_page_execute') {
if (user_access('use page manager')) {
$page_arguments = unserialize($el_item['page_arguments']);
$el_ops['edit']['#href'] = 'admin/structure/pages/edit/page-' . implode('/', $page_arguments);
}
$target_content = t('Panel');
}
else {
if ($el_item['page_callback'] == 'views_page') {
if (user_access('administer views')) {
$page_arguments = unserialize($el_item['page_arguments']);
$el_ops['edit']['#href'] = 'admin/structure/views/view/' . $page_arguments[0] . '/edit/' . $page_arguments[1];
}
$el_ops['selectcontent']['#href'] = 'admin/structure/menu/manage/add_existing_view';
$el_ops['selectcontent']['#title'] = t('select view');
$target_content = t('View');
}
else {
if ($link_path[0] == 'menu-position') {
if (!user_access('administer menu positions')) {
$el_ops['configure'] = $el_ops['edit'] = array();
}
$target_content = t('Menu rule');
$el['#inline_edit_title'] = FALSE;
}
else {
if (url_is_external($el_item['link_path'])) {
$target_content = t('URL');
}
else {
$target_content = t('Path');
}
}
}
}
}
}
if (isset($el_ops['edit']['#href'])) {
if ($el_ops['edit']['#href'] == $el_ops['configure']['#href']) {
$el_ops['edit'] = array();
$el_ops['selectcontent']['weight'] = 4;
}
else {
$el_ops['edit']['#title'] = t('edit content');
$el_ops['edit']['#options']['query']['destination'] = current_path();
}
}
}
else {
$el_ops['edit'] = array();
}
if (!isset($el_ops['delete']) && $link_path[0] == 'menu-position' && (is_numeric($link_path[1]) && user_access('administer menu positions'))) {
$el_ops['delete'] = array(
'#type' => 'link',
'#title' => t('delete'),
'#href' => 'admin/structure/menu-position/delete/' . intval($link_path[1]),
'#options' => array(
'query' => array(
'destination' => current_path(),
),
),
);
}
if (isset($el_ops['delete'])) {
$el_ops['delete']['#title'] = t('delete item');
$el_ops['delete']['weight'] = 100;
}
$el['target_content'] = array(
'#type' => 'markup',
'#markup' => $target_content,
);
}
function content_menu_form_menu_overview_form_alter_additem(&$form) {
$form['additem'] = array(
'#attributes' => array(
'class' => array(
'menu-additem',
),
),
'hidden' => array(
'#type' => 'checkbox',
'#title' => t('Enable the new menu link'),
'#title_display' => 'invisible',
'#default_value' => true,
),
'weight' => array(
'#type' => 'weight',
'#delta' => 50,
'#default_value' => CONTENT_MENU_ADD_ITEM_WEIGHT,
'#title_display' => 'invisible',
'#title' => 'Weight',
),
'mlid' => array(
'#type' => 'hidden',
'#value' => '0',
),
'plid' => array(
'#type' => 'hidden',
'#default_value' => '0',
),
'operations' => array(),
'#operations_count' => 0,
'title' => array(
'#type' => 'textfield',
'#title' => t('New menu item'),
'#default_value' => '',
'#size' => 30,
'#maxlength' => 255,
'#required' => FALSE,
),
);
$target_types = content_menu_get_target_types($form['#menu']['menu_name']);
foreach ($target_types as $op_key => $op_info) {
$target_types[$op_key] = $op_info['label'];
}
$form['additem']['target_content'] = array(
'#type' => 'select',
'#title' => ' ',
'#options' => $target_types,
'#default_value' => config('content_menu.settings')
->get('default_add_operation'),
);
}
function theme_menu_overview_form_extended($variables) {
$form = $variables['form'];
drupal_add_tabledrag('menu-overview', 'match', 'parent', 'menu-plid', 'menu-plid', 'menu-mlid', TRUE, MENU_MAX_DEPTH - 1);
drupal_add_tabledrag('menu-overview', 'order', 'sibling', 'menu-weight');
$operations_count_max = 1;
foreach (element_children($form) as $mlid) {
if (isset($form[$mlid]['#operations_count'])) {
$operations_count_max = max($operations_count_max, $form[$mlid]['#operations_count']);
}
}
$header = array(
t('Menu link'),
array(
'data' => t('Enabled'),
'class' => array(
'checkbox',
),
),
t('Weight'),
t('Target'),
array(
'data' => t('Operations'),
'colspan' => $operations_count_max,
),
);
$rows = array();
foreach (element_children($form) as $mlid) {
if (isset($form[$mlid]['hidden'])) {
$element =& $form[$mlid];
$operations = array();
foreach (element_children($element['operations']) as $op) {
$operations[] = array(
'data' => drupal_render($element['operations'][$op]),
'class' => array(
'menu-operations',
),
);
}
while (count($operations) < $operations_count_max) {
$operations[] = '';
}
$element['plid']['#attributes']['class'] = array(
'menu-plid',
);
$element['mlid']['#attributes']['class'] = array(
'menu-mlid',
);
$element['weight']['#attributes']['class'] = array(
'menu-weight',
);
$element['plid']['#type'] = 'hidden';
$depth = isset($element['#item']) ? $element['#item']['depth'] : 1;
$row = array();
$row[] = theme('indentation', array(
'size' => $depth - 1,
)) . drupal_render($element['title']);
$row[] = array(
'data' => drupal_render($element['hidden']),
'class' => array(
'checkbox',
'menu-enabled',
),
);
$row[] = drupal_render($element['weight']) . drupal_render($element['plid']) . drupal_render($element['mlid']);
$row[] = array(
'data' => drupal_render($element['target_content']),
'class' => array(
'menu-target-content',
),
);
$row = array_merge($row, $operations);
$row = array_merge(array(
'data' => $row,
), $element['#attributes']);
$row['class'][] = 'draggable';
if (isset($element['#item']['mlid']) && isset($_SESSION['content_menu_inserted_links'][$element['#item']['mlid']])) {
if (time() - $_SESSION['content_menu_inserted_links'][$element['#item']['mlid']]['created'] <= 60) {
$row['class'][] = 'ok';
}
unset($_SESSION['content_menu_inserted_links'][$element['#item']['mlid']]);
}
$rows[] = $row;
}
}
$output = '';
if (empty($rows)) {
$rows[] = array(
array(
'data' => $form['#empty_text'],
'colspan' => '7',
),
);
}
if (isset($form['#content_menu_filter_widget'])) {
foreach ($form['#content_menu_filter_widget'] as $widgetname) {
$output .= drupal_render($form[$widgetname]);
}
}
$output .= theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array(
'id' => 'menu-overview',
),
));
$output .= drupal_render_children($form);
return $output;
}
function content_menu_menu_overview_form_submit_extended($form, &$form_state) {
$max_weight = 0;
foreach (element_children($form) as $mlid) {
$el =& $form[$mlid];
if (isset($el['#item']['weight']) && $el['#item']['weight'] > $max_weight) {
$max_weight = $el['#item']['weight'];
}
if (isset($el['#item']) && isset($el['title']['#value'])) {
if ($el['title']['#value'] != $el['title']['#default_value']) {
$title = $form_state['input'][$mlid]['title'];
$el['#item']['title'] = $el['#item']['link_title'] = $title;
$el['weight']['#default_value'] += 1;
}
}
}
menu_overview_form_submit($form, $form_state);
$item = $form_state['input']['additem'];
if (trim($item['title'] != '')) {
$item['link_title'] = $item['title'];
$item['name'] = $form['#menu']['menu_name'];
$item['hidden'] = $item['hidden'] ? 0 : 1;
$item['customized'] = 1;
if ($item['weight'] == CONTENT_MENU_ADD_ITEM_WEIGHT) {
$item['weight'] = $max_weight + 1;
}
$operations = content_menu_get_target_types($item['name']);
$op = $operations[$item['target_content']];
$args = array(
'form' => $form,
'form_state' => $form_state,
'item' => $item,
);
if (isset($op['arguments']) && is_array($op['arguments'])) {
$args += $op['arguments'];
}
if (function_exists($op['handler'])) {
if ($op['clean_menu_save_message']) {
foreach ($_SESSION['messages']['status'] as $msg_key => $msg_text) {
if ($_SESSION['messages']['status'][$msg_key] == t('Your configuration has been saved.')) {
unset($_SESSION['messages']['status'][$msg_key]);
}
}
}
$result = call_user_func_array($op['handler'], $args);
}
}
}
function content_menu_menu_form_handler_dummy($form, $form_state, $item) {
drupal_set_message(t('A new menu item has been created.'));
$item['link_path'] = 'menu-dummy';
content_menu_link_save($item);
}
function content_menu_menu_form_handler_node($form, $form_state, $item, $type) {
$types = node_type_get_types();
drupal_set_message(t('You`re about to create a new menu item with new %type content.', array(
'%type' => $types[$type]->name,
)));
drupal_set_message(t('Complete the process by filling out and saving the form below.'));
$type = str_replace("_", "-", $type);
drupal_goto('node/add/' . $type, array(
'query' => content_menu_assemble_query_string($item),
));
}
function content_menu_menu_form_handler_url($form, $form_state, $item) {
drupal_set_message(t('You`re about to create a new generic menu item linking to a URL path.'));
drupal_set_message(t('Complete the process by filling out and saving the form below.'));
drupal_goto('admin/structure/menu/manage/' . $item['name'] . '/add', array(
'query' => content_menu_assemble_query_string($item),
));
}
function content_menu_menu_form_handler_menu_position_rule($form, $form_state, $item) {
drupal_set_message(t('You`re about to create a new generic menu position rule.'));
drupal_set_message(t('Complete the process by filling out and saving the form below.'));
drupal_goto('admin/structure/menu-position/add', array(
'query' => content_menu_assemble_query_string($item),
));
}
function content_menu_menu_form_handler_menu_position_existing($form, $form_state, $item) {
$url = content_menu_variable_get_add_existing_content_url();
if (empty($url)) {
content_menu_menu_form_handler_url($form, $form_state, $item);
}
else {
drupal_set_message(t('You`re about to create a new menu item for an existing content.'));
drupal_set_message(t('Complete the process by finding and selecting an existing content to add to the menu.'));
drupal_goto($url, array(
'query' => content_menu_assemble_query_string($item),
));
}
}
function content_menu_menu_form_handler_existing_view($form, $form_state, $item) {
drupal_set_message(t('You`re about to create a new menu item for an existing view.'));
drupal_set_message(t('Complete the process by finding and selecting the view to add to the menu.'));
drupal_goto('admin/structure/menu/manage/add_existing_view', array(
'query' => content_menu_assemble_query_string($item),
));
}
function content_menu_add_exiting_view($form, $form_state) {
if (!module_exists('views')) {
return;
}
$form = array();
$item = content_menu_get_menu_item_from_querystring();
$form['#menu_item'] = $item;
$all_views = views_get_all_views();
foreach ($all_views as $view) {
foreach ($view->display as $display) {
if ($display->display_plugin == 'page') {
if (!property_exists($view, 'disabled') || property_exists($view, 'disabled') && !$view->disabled) {
$view_path = $display->display_options['path'];
$view_title = check_plain($view->name . ' - ' . $display->display_title . ' (' . $display->display_options['path'] . ')');
if (drupal_valid_path($view_path)) {
$options[$view_path] = $view_title;
}
}
}
}
}
if (!empty($options)) {
asort($options);
$form['existing_view'] = array(
'#type' => 'radios',
'#title' => t('Link to this view'),
'#default_value' => isset($item['link_path']) ? $item['link_path'] : NULL,
'#required' => TRUE,
'#options' => $options,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#submit' => array(
'content_menu_add_exiting_view_submit',
),
);
}
else {
$no_views_notice = t('There are no available views.');
if (module_exists('views_ui') && user_access('administer views')) {
$no_views_notice .= ' ' . t('You can create views in the <a href="@url">Views UI</a>', array(
'@url' => url('admin/structure/views'),
));
}
$form['view_info'] = array(
'#title' => t('Use these views'),
'#type' => 'item',
'#markup' => $no_views_notice,
);
}
return $form;
}
function content_menu_add_exiting_view_submit($form, $form_state) {
$menu_item = $form['#menu_item'];
$menu_item['link_path'] = $form_state['values']['existing_view'];
content_menu_link_save($menu_item);
}
function content_menu_assemble_query_string($item) {
if (empty($item['name']) && isset($item['menu_name'])) {
$item['name'] = $item['menu_name'];
}
$query = array(
'menu_mlid' => isset($item['mlid']) ? $item['mlid'] : NULL,
'menu_title' => $item['title'],
'menu_plid' => $item['plid'],
'menu_name' => $item['name'],
'menu_hidden' => $item['hidden'],
'menu_weight' => $item['weight'],
'menu_link_path' => isset($item['link_path']) ? $item['link_path'] : NULL,
'destination' => isset($item['destination']) ? $item['destination'] : 'admin/structure/menu/manage/' . $item['name'],
'msg_buffer' => isset($item['msg_buffer']) ? $item['msg_buffer'] : NULL,
);
return $query;
}
function content_menu_get_target_types($menu_name) {
$target_types = array();
$context = array(
'menu_name' => $menu_name,
);
drupal_alter('menu_item_target_types', $target_types, $context);
return $target_types;
}
function _content_menu_menu_item_target_types_alter(&$target_types, &$context) {
$target_types['url'] = array(
'label' => t('URL'),
'handler' => 'content_menu_menu_form_handler_url',
'clean_menu_save_message' => TRUE,
);
$target_types['dummy'] = array(
'label' => t('Placeholder'),
'handler' => 'content_menu_menu_form_handler_dummy',
'clean_menu_save_message' => FALSE,
);
foreach (node_type_get_types() as $node_type) {
if (user_access('create ' . $node_type->type . ' content') || user_access('administer nodes')) {
$node_menu_options = variable_get('menu_options_' . $node_type->type, array(
'main-menu' => 'main-menu',
));
if (in_array($context['menu_name'], $node_menu_options)) {
$target_types['node_' . $node_type->type] = array(
'label' => t('New @type', array(
'@type' => t($node_type->name),
)),
'handler' => 'content_menu_menu_form_handler_node',
'arguments' => array(
'type' => $node_type->type,
),
'clean_menu_save_message' => TRUE,
);
}
}
}
$target_types['existing'] = array(
'label' => t('Existing content'),
'handler' => 'content_menu_menu_form_handler_menu_position_existing',
'clean_menu_save_message' => TRUE,
);
if (module_exists('views')) {
$target_types['existing_view'] = array(
'label' => t('Existing view'),
'handler' => 'content_menu_menu_form_handler_existing_view',
'clean_menu_save_message' => TRUE,
);
}
if (module_exists('menu_position') && user_access('administer menu positions')) {
$target_types['menu_position_rule'] = array(
'label' => t('Menu position rule'),
'handler' => 'content_menu_menu_form_handler_menu_position_rule',
'clean_menu_save_message' => TRUE,
);
}
}