You are here

protected function MenuLinkItem::getMenuPluginDefinition in Menu Link (Field) 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldType/MenuLinkItem.php \Drupal\menu_link\Plugin\Field\FieldType\MenuLinkItem::getMenuPluginDefinition()

Generates the plugin definition of the associated menu link.

Return value

array|false The menu plugin definition, otherwise FALSE.

1 call to MenuLinkItem::getMenuPluginDefinition()
MenuLinkItem::doSave in src/Plugin/Field/FieldType/MenuLinkItem.php
Saves the plugin definition.

File

src/Plugin/Field/FieldType/MenuLinkItem.php, line 282

Class

MenuLinkItem
Defines a menu link field type which stores the link, parent and menu.

Namespace

Drupal\menu_link\Plugin\Field\FieldType

Code

protected function getMenuPluginDefinition($langcode) {
  $menu_definition = [];

  // If there is no menu name selected, you don't have a valid menu plugin.
  if (!$this->values['menu_name']) {
    return FALSE;
  }
  $entity = $this
    ->getEntity();
  $menu_definition['id'] = $this
    ->getMenuPluginId($langcode);
  if ($entity instanceof TranslatableInterface && $entity
    ->hasTranslation($langcode)) {
    $entity = $entity
      ->getTranslation($langcode);
  }
  $menu_definition['title'] = $this->values['title'] ?: $entity
    ->label();
  $menu_definition['description'] = isset($this->values['description']) ? $this->values['description'] : '';
  $menu_definition['title_max_length'] = $this
    ->getFieldDefinition()
    ->getSetting('max_length');
  $menu_definition['menu_name'] = $this->values['menu_name'];
  $menu_definition['parent'] = isset($this->values['parent']) ? $this->values['parent'] : '';
  $menu_definition['weight'] = isset($this->values['weight']) ? $this->values['weight'] : 0;
  $menu_definition['class'] = MenuLinkField::class;
  $menu_definition['form_class'] = MenuLinkFieldForm::class;
  $menu_definition['metadata']['entity_id'] = $entity
    ->id();
  $menu_definition['metadata']['entity_type_id'] = $entity
    ->getEntityTypeId();
  $menu_definition['metadata']['field_name'] = $this->definition
    ->getFieldDefinition()
    ->getName();
  $menu_definition['metadata']['langcode'] = $langcode;
  $menu_definition['metadata']['translatable'] = $entity
    ->getEntityType()
    ->isTranslatable();
  $url = $entity
    ->toUrl('canonical');
  $menu_definition['route_name'] = $url
    ->getRouteName();
  $menu_definition['route_parameters'] = $url
    ->getRouteParameters();
  return $menu_definition;
}