You are here

menus_attribute.module in Menus attribute 8

Provides alters for menus_attribute.

File

menus_attribute.module
View source
<?php

/**
 * @file
 * Provides alters for menus_attribute.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\menus_attribute\StorageHelper;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function menus_attribute_form_alter(array &$form, FormStateInterface $form_state) {
  if ($form['#form_id'] == 'menu_link_content_menu_link_content_form' || $form['#form_id'] == 'menu_link_edit') {
    $instance = StorageHelper::instance();
    $data = NULL;
    $plugin_id = NULL;
    $get_path = explode("/", \Drupal::service('path.current')
      ->getPath());
    if ($get_path[6] == 'edit') {
      if ($form['#form_id'] == 'menu_link_content_menu_link_content_form') {
        $id = $get_path[5];
        $plugin_id = \Drupal::entityTypeManager()
          ->getStorage('menu_link_content')
          ->load($id)
          ->getPluginId();
      }
      if ($form['#form_id'] == 'menu_link_edit') {
        $plugin_id = $get_path[5];
      }
      if ($instance
        ->exists($plugin_id)) {
        $data = $instance
          ->getData($plugin_id);
      }
    }
    $form['menu_link_attributes'] = [
      '#type' => 'details',
      '#title' => t('Menu Link Attributes'),
    ];
    $form['menu_link_attributes']['menu_link_id'] = [
      '#type' => 'textfield',
      '#title' => t('ID'),
      '#description' => t('Specifies a unique ID for the link.'),
      '#default_value' => $data ? $data->link_id : '',
    ];
    $form['menu_link_attributes']['menu_link_name'] = [
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#default_value' => $data ? $data->link_name : '',
    ];
    $form['menu_link_attributes']['menu_link_title'] = [
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#default_value' => $data ? $data->link_title : '',
    ];
    $form['menu_link_attributes']['menu_link_rel'] = [
      '#type' => 'textfield',
      '#title' => t('Relationship'),
      '#description' => t("Specifies the relationship between the current page and the link. Enter 'nofollow' here to nofollow this link."),
      '#default_value' => $data ? $data->link_rel : '',
    ];
    $form['menu_link_attributes']['menu_link_class'] = [
      '#type' => 'textfield',
      '#title' => t('Classes'),
      '#description' => t('Enter additional classes to be added to the link.'),
      '#default_value' => $data ? $data->link_classes : '',
    ];
    $form['menu_link_attributes']['menu_link_style'] = [
      '#type' => 'textfield',
      '#title' => t('Style'),
      '#description' => t('Enter additional styles to be applied to the link.'),
      '#default_value' => $data ? $data->link_style : '',
    ];
    $form['menu_link_attributes']['menu_link_target'] = [
      '#type' => 'select',
      '#title' => t('Target'),
      '#description' => t('Specifies where to open the link. Using this attribute breaks XHTML validation.'),
      '#options' => [
        '' => 'None(ie.same window)',
        '_blank' => 'New window (_blank)',
        '_top' => 'Top window (_top)',
        '_self' => 'Same window (_self)',
        '_parent' => 'Parent window (_parent)',
      ],
      '#default_value' => $data ? $data->link_target : '',
    ];
    $form['menu_link_attributes']['menu_link_access_key'] = [
      '#type' => 'textfield',
      '#title' => t('Access Key'),
      '#size' => 1,
      '#maxlength' => 1,
      '#description' => t('Specifies a keyboard shortcut to access this link.'),
      '#default_value' => $data ? $data->link_accesskey : '',
    ];
    $form['menu_item_attributes'] = [
      '#type' => 'details',
      '#title' => t('Menu Item Attributes'),
    ];
    $form['menu_item_attributes']['menu_item_id'] = [
      '#type' => 'textfield',
      '#title' => t('ID'),
      '#description' => t('Specifies a unique ID for the link.'),
      '#default_value' => $data ? $data->item_id : '',
    ];
    $form['menu_item_attributes']['menu_item_class'] = [
      '#type' => 'textfield',
      '#title' => t('Classes'),
      '#description' => t('Enter additional classes to be added to the link.'),
      '#default_value' => $data ? $data->item_classes : '',
    ];
    $form['menu_item_attributes']['menu_item_style'] = [
      '#type' => 'textfield',
      '#title' => t('Style'),
      '#description' => t('Enter additional styles to be applied to the link.'),
      '#default_value' => $data ? $data->item_style : '',
    ];
    $form['actions']['submit']['#submit'][] = '_menus_attribute_form_submit_handler';
    return $form;
  }
}

/**
 * Submit handler.
 */
function _menus_attribute_form_submit_handler(&$form, &$form_state) {
  $instance = StorageHelper::instance();
  $get_path = explode("/", \Drupal::service('path.current')
    ->getPath());
  if ($get_path[6] == 'edit') {
    if ($form['#form_id'] == 'menu_link_content_menu_link_content_form') {
      $id = $get_path[5];
      $plugin_id = \Drupal::entityTypeManager()
        ->getStorage('menu_link_content')
        ->load($id)
        ->getPluginId();
    }
    if ($form['#form_id'] == 'menu_link_edit') {
      $plugin_id = $get_path[5];
    }
  }
  else {
    $plugin_id = $form_state
      ->getBuildInfo()['callback_object']
      ->getEntity()
      ->getPluginId();
  }
  foreach ($form_state
    ->getValues() as $key => $value) {
    $temp[$key] = $value;
  }
  $instance
    ->exists($plugin_id) ? $instance
    ->update($temp, $plugin_id) : $instance
    ->add($temp, $plugin_id);
}

/**
 * Implements hook_theme_registry_alter().
 */
function menus_attribute_theme_registry_alter(&$theme_registry) {
  $theme_registry['menu']['path'] = drupal_get_path('module', 'menus_attribute') . '/templates';
}