function taxonomy_menu_ui_get_menu_link_defaults in Taxonomy Menu UI 8
Returns the definition for a menu link for the given term.
Parameters
\Drupal\taxonomy\TermInterface $term: The term entity.
Return value
array An array that contains default values for the menu link form.
1 call to taxonomy_menu_ui_get_menu_link_defaults()
File
- ./
taxonomy_menu_ui.module, line 62 - Add ability to create menu links for taxonomy terms.
Code
function taxonomy_menu_ui_get_menu_link_defaults(TermInterface $term) {
// Prepare the definition for the edit form.
/** @var \Drupal\taxonomy\VocabularyInterface $vocabulary */
$vid = $term
->bundle();
$vocabulary = Vocabulary::load($vid);
$menu_name = strtok($vocabulary
->getThirdPartySetting('menu_ui', 'parent', 'main:'), ':');
$defaults = FALSE;
if ($term
->id()) {
$id = FALSE;
// Give priority to the default menu.
$vocabulary_menus = $vocabulary
->getThirdPartySetting('menu_ui', 'available_menus', [
'main',
]);
if (in_array($menu_name, $vocabulary_menus)) {
$query = \Drupal::entityQuery('menu_link_content')
->condition('link.uri', 'taxonomy/term/' . $term
->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($vocabulary_menus)) {
$query = \Drupal::entityQuery('menu_link_content')
->condition('link.uri', 'internal:/taxonomy/term/' . $term
->id())
->condition('menu_name', array_values($vocabulary_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 = [
'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::service('entity_field.manager')
->getBaseFieldDefinitions('menu_link_content');
$max_length = $field_definitions['title']
->getSetting('max_length');
$defaults = [
'entity_id' => 0,
'id' => '',
'title' => '',
'title_max_length' => $max_length,
'description' => '',
'menu_name' => $menu_name,
'parent' => '',
'weight' => 0,
];
}
return $defaults;
}