You are here

function menu_badges_form_alter in Menu Badges 8

Implements hook_form_alter().

File

./menu_badges.module, line 35
Menu Badges module file.

Code

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';
  }
}