menu_ui_get_menu_link_defaults

menu_ui_get_menu_link_defaults(NodeInterface $node)

Returns the definition for a menu link for the given node.

Parameters

\Drupal\node\NodeInterface $node: The node entity.

Return value

array An array that contains default values for the menu link form.

File

core/modules/menu_ui/menu_ui.module, line 193
Allows administrators to customize the site's navigation menus.

Code

function menu_ui_get_menu_link_defaults(NodeInterface $node) {
  // Prepare the definition for the edit form.
  /** @var \Drupal\node\NodeTypeInterface $node_type */
  $node_type = $node->type->entity;
  $menu_name = strtok($node_type->getThirdPartySetting('menu_ui', 'parent', 'main:'), ':');
  $defaults = FALSE;
  if ($node->id()) {
    $id = FALSE;
    // Give priority to the default menu
    $type_menus = $node_type->getThirdPartySetting('menu_ui', 'available_menus', array('main'));
    if (in_array($menu_name, $type_menus)) {
      $query = \Drupal::entityQuery('menu_link_content')
        ->condition('link.uri', 'node/' . $node->id())
        ->condition('menu_name', $menu_name)
        ->sort('id', 'ASC')
        ->range(0, 1);
      $result = $query->execute();

      $id = (!empty($result)) ? reset($result) : FALSE;
    }
    // Check all allowed menus if a link does not exist in the default menu.
    if (!$id && !empty($type_menus)) {
      $query = \Drupal::entityQuery('menu_link_content')
        ->condition('link.uri', 'entity:node/' . $node->id())
        ->condition('menu_name', array_values($type_menus), 'IN')
        ->sort('id', 'ASC')
        ->range(0, 1);
      $result = $query->execute();

      $id = (!empty($result)) ? reset($result) : FALSE;
    }
    if ($id) {
      $menu_link = MenuLinkContent::load($id);
      $menu_link = \Drupal::service('entity.repository')->getTranslationFromContext($menu_link);
      $defaults = array(
        'entity_id' => $menu_link->id(),
        'id' => $menu_link->getPluginId(),
        'title' => $menu_link->getTitle(),
        'title_max_length' => $menu_link->getFieldDefinitions()['title']->getSetting('max_length'),
        'description' => $menu_link->getDescription(),
        'menu_name' => $menu_link->getMenuName(),
        'parent' => $menu_link->getParentId(),
        'weight' => $menu_link->getWeight(),
      );
    }
  }

  if (!$defaults) {
    // Get the default max_length of a menu link title from the base field
    // definition.
    $field_definitions = \Drupal::entityManager()->getBaseFieldDefinitions('menu_link_content');
    $max_length = $field_definitions['title']->getSetting('max_length');
    $defaults = array(
      'entity_id' => 0,
      'id' => '',
      'title' => '',
      'title_max_length' => $max_length,
      'description' => '',
      'menu_name' => $menu_name,
      'parent' => '',
      'weight' => 0,
    );
  }
  return $defaults;
}
doc_Drupal
2016-10-29 09:27:51
Comments
Leave a Comment

Please login to continue.