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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 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 ; } |
Please login to continue.