You are here

menu_badges.module in Menu Badges 8

Same filename and directory in other branches
  1. 7 menu_badges.module

Menu Badges module file.

File

menu_badges.module
View source
<?php

/**
 * @file
 * Menu Badges module file.
 */

/**
 * Implements hook_permission().
 */
function menu_badges_permission() {
  return array(
    'administer menu badges' => array(
      'title' => t('Administer Menu Badges'),
      'description' => t('Select link badges to place on menu links or menu tabs (local tasks)'),
    ),
  );
}

/**
 * Implements hook_theme().
 */
function menu_badges_theme($existing, $type, $theme, $path) {
  return array(
    'menu_badges_tabs_admin_form' => array(
      'render element' => 'form',
    ),
  );
}

/**
 * Implements hook_form_alter().
 */
function menu_badges_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'menu_link_edit') {
    $manager = \Drupal::service('plugin.manager.link_badge');
    $definitions = $manager
      ->getDefinitions();
    $menu_badge_options = array(
      '' => t('None'),
    );
    foreach ($definitions as $id => $info) {
      $menu_badge_options[$id] = $info['label'];
    }
    $options = $form_state
      ->getBuildInfo()['args'][0]
      ->getOptions();
    $form['menu_badges_id'] = array(
      '#type' => 'select',
      '#title' => t('Display menu badge'),
      '#options' => $menu_badge_options,
      '#description' => t('Display the value from a menu badge function on this menu item.'),
      '#default_value' => isset($options['link_badge']['id']) ? $options['link_badge']['id'] : '',
    );
    $form['#submit'][] = 'menu_badges_menu_edit_item_submit';
  }
  elseif ($form_id == 'menu_link_content_menu_link_content_form') {
    $manager = \Drupal::service('plugin.manager.link_badge');
    $definitions = $manager
      ->getDefinitions();
    $menu_badge_options = array(
      '' => t('None'),
    );
    foreach ($definitions as $id => $info) {
      $menu_badge_options[$id] = $info['label'];
    }
    $uuid = $form_state
      ->getBuildInfo()['callback_object']
      ->getEntity()
      ->uuid();
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    $defintion = $menu_link_manager
      ->getDefinition('menu_link_content:' . $uuid);
    $form['menu_badges_id'] = array(
      '#type' => 'select',
      '#title' => t('Display menu badge'),
      '#options' => $menu_badge_options,
      '#description' => t('Display the value from a menu badge function on this menu item.'),
      '#default_value' => isset($defintion['options']['link_badge']['id']) ? $defintion['options']['link_badge']['id'] : '',
    );
    $form['actions']['submit']['#submit'][] = 'menu_badges_menu_link_content_menu_link_content_form_submit';
  }
}

/**
 * Submit handler for saving menu badge options.
 */
function menu_badges_menu_edit_item_submit(&$form, &$form_state) {
  $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
  $menu_badge_id = $form_state
    ->getValue('menu_badges_id');
  if (empty($menu_badge_id)) {
    $menu_link_manager
      ->updateDefinition($form_state
      ->getValue('menu_link_id'), [
      'options' => [],
    ]);
  }
  else {
    $menu_link_manager
      ->updateDefinition($form_state
      ->getValue('menu_link_id'), [
      'options' => [
        'link_badge' => [
          'id' => $menu_badge_id,
        ],
      ],
    ]);
  }
}
function menu_badges_menu_link_content_menu_link_content_form_submit(&$form, &$form_state) {
  $menu_badge_id = $form_state
    ->getValue('menu_badges_id');
  $menu_link = $form_state
    ->getBuildInfo()['callback_object']
    ->getEntity();
  $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
  if (empty($menu_badge_id)) {
    $menu_link_manager
      ->updateDefinition('menu_link_content:' . $menu_link
      ->uuid(), [
      'options' => [],
    ]);
  }
  else {
    $menu_link_manager
      ->updateDefinition('menu_link_content:' . $menu_link
      ->uuid(), [
      'options' => [
        'link_badge' => [
          'id' => $menu_badge_id,
        ],
      ],
    ]);
  }
}

/**
 * Form theming for tab admin form.
 * @param $variables
 */
function template_preprocess_menu_badges_tabs_admin_form(&$variables) {
  $form =& $variables['form'];
  $variables['search_form'] = $variables['form']['search'];
  unset($variables['form']['search']);
  $rows = array();
  foreach ($form['results'] as $task_id => $task_info) {
    if (is_array($task_info) && !empty($task_info['type'])) {
      $row = array();
      $row[] = $task_info['title']['#value'];
      $row[] = $task_info['path']['#value'];
      $row[] = $task_info['type']['#value'];
      $row[] = drupal_render($form['results'][$task_id]['menu_badges_id']);
      $rows[] = $row;
    }
  }
  $variables['results_table'] = array(
    '#type' => 'table',
    '#header' => array(
      t('Title'),
      t('Menu Path'),
      t('Menu Type'),
      t('Menu Badge'),
    ),
    '#rows' => $rows,
  );
  $variables['pager'] = array(
    '#type' => 'pager',
  );
}
function menu_badges_local_tasks_alter(&$local_tasks) {
  $menu_badge_manager = \Drupal::service('menu_badges.manager');
  $local_badges = $menu_badge_manager
    ->getLocalBadges();
  if (!empty($local_badges[\Drupal\menu_badges\MenuBadgesManager::LOCAL_TASK])) {
    foreach ($local_badges[\Drupal\menu_badges\MenuBadgesManager::LOCAL_TASK] as $route_id => $badge) {
      $route_id = str_replace('|', '.', $route_id);
      if (!empty($local_tasks[$route_id])) {
        $local_tasks[$route_id]['options']['link_badge'] = $badge;
      }
      else {
        unset($local_tasks[$route_id]['options']['link_badge']);
      }
    }
  }
}

/**
 * Implements hook_menu_local_actions_alter().
 */
function menu_badges_menu_local_actions_alter(&$local_actions) {
  $menu_badge_manager = \Drupal::service('menu_badges.manager');
  $local_badges = $menu_badge_manager
    ->getLocalBadges();
  if (!empty($local_badges[\Drupal\menu_badges\MenuBadgesManager::LOCAL_ACTION])) {
    foreach ($local_badges[\Drupal\menu_badges\MenuBadgesManager::LOCAL_ACTION] as $route_id => $badge) {
      $route_id = str_replace('|', '.', $route_id);
      if (!empty($local_actions[$route_id])) {
        $local_actions[$route_id]['options']['link_badge'] = $badge;
      }
      else {
        unset($local_actions[$route_id]['options']['link_badge']);
      }
    }
  }
}

/*function template_preprocess_menu_local_tasks(&$variables) {
  $tab_callbacks = \Drupal::service('menu_badges.manager')->getLocalBadgesForRoutes(array(), \Drupal\menu_badges\MenuBadgesManager::LOCAL_TASK);
  foreach (array('primary', 'secondary') as $type) {
    if (!empty($variables[$type])) {
      foreach ($variables[$type] as $route_id => $route) {
        if (in_array($route_id, array_keys($tab_callbacks))) {
          $variables[$type][$route_id]['#link']['localized_options']['link_badge'] = array(
            'callback' => $tab_callbacks[$route_id]['callback'],
          );
          if (!empty($tab_callbacks[$route_id]['arguments'])) {
            $variables[$type][$route_id]['#link']['localized_options']['link_badge']['arguments'] = $tab_callbacks[$route_id]['arguments'];
          }
        }
      }
    }
  }
}

function template_preprocess_menu_local_action(&$variables) {
  $tab_callbacks = \Drupal::service('menu_badges.manager')->getLocalBadgesForRoutes(array(), \Drupal\menu_badges\MenuBadgesManager::LOCAL_ACTION);
  $route_id = $variables['element']['#link']['route_name'];
  if (in_array($route_id, array_keys($tab_callbacks))) {
    $variables['element']['#link']['localized_options']['link_badge'] = array(
      'callback' => $tab_callbacks[$route_id]['callback'],
    );
    if (!empty($tab_callbacks[$route_id]['arguments'])) {
      $variables['element'][$route_id]['#link']['localized_options']['link_badge']['arguments'] = $tab_callbacks[$route_id]['arguments'];
    }
  }
}*/