class MenuLinkItem in Menu Link (Field) 8
Same name and namespace in other branches
- 2.0.x src/Plugin/Field/FieldType/MenuLinkItem.php \Drupal\menu_link\Plugin\Field\FieldType\MenuLinkItem
Defines a menu link field type which stores the link, parent and menu.
Plugin annotation
@FieldType(
id = "menu_link",
label = @Translation("Menu link"),
description = @Translation("Stores a title, menu and parent to insert a link to the current entity."),
default_widget = "menu_link_default",
list_class = "\Drupal\menu_link\Plugin\Field\MenuLinkItemList",
default_formatter = "menu_link",
column_groups = {
"title" = {
"label" = @Translation("Title"),
"translatable" = TRUE
},
"description" = {
"label" = @Translation("Description"),
"translatable" = TRUE
},
"menu_name" = {
"label" = @Translation("Menu name"),
"translatable" = TRUE
},
"parent" = {
"label" = @Translation("Parent"),
"translatable" = TRUE
},
},
)
Hierarchy
- class \Drupal\Core\TypedData\TypedData implements PluginInspectionInterface, TypedDataInterface uses DependencySerializationTrait, StringTranslationTrait, TypedDataTrait
- class \Drupal\Core\TypedData\Plugin\DataType\Map implements \Drupal\Core\TypedData\Plugin\DataType\IteratorAggregate, ComplexDataInterface
- class \Drupal\Core\Field\FieldItemBase implements FieldItemInterface
- class \Drupal\menu_link\Plugin\Field\FieldType\MenuLinkItem
- class \Drupal\Core\Field\FieldItemBase implements FieldItemInterface
- class \Drupal\Core\TypedData\Plugin\DataType\Map implements \Drupal\Core\TypedData\Plugin\DataType\IteratorAggregate, ComplexDataInterface
Expanded class hierarchy of MenuLinkItem
File
- src/
Plugin/ Field/ FieldType/ MenuLinkItem.php, line 47
Namespace
Drupal\menu_link\Plugin\Field\FieldTypeView source
class MenuLinkItem extends FieldItemBase {
/**
* The menu plugin manager.
*
* @var \Drupal\Core\Menu\MenuLinkManagerInterface
*/
protected $menuPluginManager;
/**
* The menu parent form selector.
*
* @var \Drupal\Core\Menu\MenuParentFormSelectorInterface
*/
protected $menuParentFormSelector;
/**
* {@inheritdoc}
*/
public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
parent::__construct($definition, $name, $parent);
$this->menuPluginManager = \Drupal::service('plugin.manager.menu.link');
$this->menuParentFormSelector = \Drupal::service('menu.parent_form_selector');
}
/**
* {@inheritdoc}
*/
public static function defaultStorageSettings() {
return [
'menu_link_per_translation' => FALSE,
] + parent::defaultStorageSettings();
}
/**
* {@inheritdoc}
*/
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element['menu_link_per_translation'] = [
'#type' => 'checkbox',
'#title' => t('Expose a menu link per translation'),
'#default_value' => $this
->getSetting('menu_link_per_translation'),
];
return $element;
}
/**
* {@inheritdoc}
*/
public static function defaultFieldSettings() {
$settings = parent::defaultFieldSettings();
$settings['available_menus'] = [
'main',
];
$settings['default_menu_parent'] = 'main:';
return $settings;
}
/**
* {@inheritdoc}
*/
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$form = parent::fieldSettingsForm($form, $form_state);
$menu_options = $this
->getMenuNames();
$form['available_menus'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Available menus'),
'#default_value' => $this
->getSetting('available_menus'),
'#options' => $menu_options,
'#description' => $this
->t('The menus available to place links in for this kind of entity.'),
'#required' => TRUE,
];
$parent_options = [];
// Make sure the setting is normalized to an associative array.
$available_menus = array_filter($this
->getSetting('available_menus'));
$available_menus = array_combine($available_menus, $available_menus);
foreach ($available_menus as $name) {
if (isset($menu_options[$name])) {
$parent_options["{$name}:"] = $menu_options[$name];
}
}
$form['default_menu_parent'] = [
'#type' => 'select',
'#title' => $this
->t('Default menu for new links'),
'#default_value' => $this
->getSetting('default_menu_parent'),
'#options' => $parent_options,
];
return $form;
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$definitions = [];
$definitions['menu_name'] = DataDefinition::create('string')
->setLabel(t('Menu'));
$definitions['title'] = DataDefinition::create('string')
->setLabel(t('Menu link title'));
$definitions['description'] = DataDefinition::create('string')
->setLabel(t('Menu link description'));
$definitions['parent'] = DataDefinition::create('string')
->setLabel(t('Menu link parent'))
->setSetting('default', '');
$definitions['weight'] = DataDefinition::create('integer')
->setLabel(t('Menu link weight'));
return $definitions;
}
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema = [];
$schema['columns']['menu_name'] = [
'description' => 'The menu of the link',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
];
$schema['columns']['title'] = [
'description' => 'The menu link text.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
];
$schema['columns']['description'] = [
'description' => 'The description of the menu link.',
'type' => 'blob',
'size' => 'big',
'not null' => FALSE,
];
$schema['columns']['parent'] = [
'description' => 'The parent of the menu link',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
];
$schema['columns']['weight'] = [
'description' => 'The weight of the menu link',
'type' => 'int',
];
return $schema;
}
/**
* {@inheritdoc}
*/
public function postSave($update) {
$this
->doSave();
}
/**
* {@inheritdoc}
*/
public function delete() {
parent::delete();
$plugin_id = $this
->getMenuPluginId();
if ($this->menuPluginManager
->hasDefinition($plugin_id)) {
$this->menuPluginManager
->removeDefinition($plugin_id, FALSE);
}
}
/**
* Saves the plugin definition.
*/
protected function doSave() {
$menu_link_per_translation = $this
->getSetting('menu_link_per_translation');
// We only update the menu link definition when working with the original
// language value of the field, otherwise, we can never properly update or
// remove the menu link.
// @todo - use the FieldTranslationSynchronizer
// https://www.drupal.org/node/2403455
if (!$menu_link_per_translation && $this
->getLangcode() != $this
->getEntity()
->getUntranslated()
->language()
->getId()) {
return;
}
$langcode = $menu_link_per_translation ? $this
->getLangcode() : LanguageInterface::LANGCODE_NOT_SPECIFIED;
$plugin_id = $this
->getMenuPluginId($langcode);
// When the entity is saved via a plugin instance, we should not call the
// menu tree manager to update the definition a second time.
if ($menu_plugin_definition = $this
->getMenuPluginDefinition($langcode)) {
if (!$this->menuPluginManager
->hasDefinition($plugin_id)) {
$this->menuPluginManager
->addDefinition($plugin_id, $menu_plugin_definition);
}
else {
$this->menuPluginManager
->updateDefinition($plugin_id, $menu_plugin_definition, FALSE);
}
}
else {
$this->menuPluginManager
->removeDefinition($plugin_id, FALSE);
}
}
/**
* Generates the plugin ID for the associated menu link.
*
* @param string $langcode
* (optional) The langcode to take into account.
*
* @return string
* The Plugin ID.
*/
public function getMenuPluginId($langcode = NULL) {
if ($langcode === NULL) {
$menu_link_per_translation = $this
->getSetting('menu_link_per_translation');
$langcode = $menu_link_per_translation ? $this
->getLangcode() : LanguageInterface::LANGCODE_NOT_SPECIFIED;
}
$field_name = $this->definition
->getFieldDefinition()
->getName();
$entity_type_id = $this
->getEntity()
->getEntityTypeId();
return 'menu_link_field:' . "{$entity_type_id}_{$field_name}_{$this->getEntity()->uuid()}_{$langcode}";
}
/**
* Generates the plugin definition of the associated menu link.
*
* @return array|false
* The menu plugin definition, otherwise FALSE.
*/
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;
}
/**
* Returns available menu names.
*
* @return string[]
* Returns menu labels, keyed by menu ID.
*/
protected function getMenuNames() {
if ($custom_menus = Menu::loadMultiple()) {
foreach ($custom_menus as $menu_name => $menu) {
$custom_menus[$menu_name] = $menu
->label();
}
asort($custom_menus);
}
return $custom_menus;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FieldItemBase:: |
public static | function |
Calculates dependencies for field items. Overrides FieldItemInterface:: |
2 |
FieldItemBase:: |
public static | function |
Calculates dependencies for field items on the storage level. Overrides FieldItemInterface:: |
1 |
FieldItemBase:: |
public | function |
Defines custom revision delete behavior for field values. Overrides FieldItemInterface:: |
|
FieldItemBase:: |
public static | function |
Returns a settings array in the field type's canonical representation. Overrides FieldItemInterface:: |
1 |
FieldItemBase:: |
public static | function |
Returns a settings array that can be stored as a configuration value. Overrides FieldItemInterface:: |
1 |
FieldItemBase:: |
public static | function |
Generates placeholder field values. Overrides FieldItemInterface:: |
18 |
FieldItemBase:: |
public | function |
Gets the entity that field belongs to. Overrides FieldItemInterface:: |
|
FieldItemBase:: |
public | function |
Gets the field definition. Overrides FieldItemInterface:: |
|
FieldItemBase:: |
public | function |
Gets the langcode of the field values held in the object. Overrides FieldItemInterface:: |
|
FieldItemBase:: |
protected | function | Returns the value of a field setting. | |
FieldItemBase:: |
protected | function | Returns the array of field settings. | |
FieldItemBase:: |
public static | function |
Returns the name of the main property, if any. Overrides FieldItemInterface:: |
8 |
FieldItemBase:: |
public static | function |
Informs the plugin that a dependency of the field will be deleted. Overrides FieldItemInterface:: |
1 |
FieldItemBase:: |
public | function |
Defines custom presave behavior for field values. Overrides FieldItemInterface:: |
7 |
FieldItemBase:: |
public | function |
Sets the data value. Overrides Map:: |
4 |
FieldItemBase:: |
public static | function |
Returns a settings array in the field type's canonical representation. Overrides FieldItemInterface:: |
2 |
FieldItemBase:: |
public static | function |
Returns a settings array that can be stored as a configuration value. Overrides FieldItemInterface:: |
2 |
FieldItemBase:: |
public | function |
Returns a renderable array for a single field item. Overrides FieldItemInterface:: |
|
FieldItemBase:: |
protected | function |
Different to the parent Map class, we avoid creating property objects as
far as possible in order to optimize performance. Thus we just update
$this->values if no property object has been created yet. Overrides Map:: |
|
FieldItemBase:: |
public | function |
Magic method: Gets a property value. Overrides FieldItemInterface:: |
2 |
FieldItemBase:: |
public | function |
Magic method: Determines whether a property is set. Overrides FieldItemInterface:: |
|
FieldItemBase:: |
public | function |
Magic method: Sets a property value. Overrides FieldItemInterface:: |
1 |
FieldItemBase:: |
public | function |
Magic method: Unsets a property. Overrides FieldItemInterface:: |
|
Map:: |
protected | property |
The data definition. Overrides TypedData:: |
|
Map:: |
protected | property | The array of properties. | |
Map:: |
protected | property | An array of values for the contained properties. | |
Map:: |
public | function |
Applies the default value. Overrides TypedData:: |
4 |
Map:: |
public | function |
Gets a property object. Overrides ComplexDataInterface:: |
|
Map:: |
public | function | ||
Map:: |
public | function |
Gets an array of property objects. Overrides ComplexDataInterface:: |
|
Map:: |
public | function |
Returns a string representation of the data. Overrides TypedData:: |
|
Map:: |
public | function |
Gets the data value. Overrides TypedData:: |
1 |
Map:: |
public | function |
Determines whether the data structure is empty. Overrides ComplexDataInterface:: |
17 |
Map:: |
public | function |
Overrides TraversableTypedDataInterface:: |
4 |
Map:: |
public | function |
Sets a property value. Overrides ComplexDataInterface:: |
|
Map:: |
public | function |
Returns an array of all property values. Overrides ComplexDataInterface:: |
1 |
Map:: |
public | function | Magic method: Implements a deep clone. | |
MenuLinkItem:: |
protected | property | The menu parent form selector. | |
MenuLinkItem:: |
protected | property | The menu plugin manager. | |
MenuLinkItem:: |
public static | function |
Defines the field-level settings for this plugin. Overrides FieldItemBase:: |
|
MenuLinkItem:: |
public static | function |
Defines the storage-level settings for this plugin. Overrides FieldItemBase:: |
|
MenuLinkItem:: |
public | function |
Defines custom delete behavior for field values. Overrides FieldItemBase:: |
|
MenuLinkItem:: |
protected | function | Saves the plugin definition. | |
MenuLinkItem:: |
public | function |
Returns a form for the field-level settings. Overrides FieldItemBase:: |
|
MenuLinkItem:: |
protected | function | Returns available menu names. | |
MenuLinkItem:: |
protected | function | Generates the plugin definition of the associated menu link. | |
MenuLinkItem:: |
public | function | Generates the plugin ID for the associated menu link. | |
MenuLinkItem:: |
public | function |
Defines custom post-save behavior for field values. Overrides FieldItemBase:: |
|
MenuLinkItem:: |
public static | function |
Defines field item properties. Overrides FieldItemInterface:: |
|
MenuLinkItem:: |
public static | function |
Returns the schema for the field. Overrides FieldItemInterface:: |
|
MenuLinkItem:: |
public | function |
Returns a form for the storage-level settings. Overrides FieldItemBase:: |
|
MenuLinkItem:: |
public | function |
Constructs a TypedData object given its definition and context. Overrides FieldItemBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TypedData:: |
protected | property | The property name. | |
TypedData:: |
protected | property | The parent typed data object. | |
TypedData:: |
public static | function |
Constructs a TypedData object given its definition and context. Overrides TypedDataInterface:: |
|
TypedData:: |
public | function |
Gets a list of validation constraints. Overrides TypedDataInterface:: |
9 |
TypedData:: |
public | function |
Gets the data definition. Overrides TypedDataInterface:: |
|
TypedData:: |
public | function |
Returns the name of a property or item. Overrides TypedDataInterface:: |
|
TypedData:: |
public | function |
Returns the parent data structure; i.e. either complex data or a list. Overrides TypedDataInterface:: |
|
TypedData:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
|
TypedData:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
TypedData:: |
public | function |
Returns the property path of the data. Overrides TypedDataInterface:: |
|
TypedData:: |
public | function |
Returns the root of the typed data tree. Overrides TypedDataInterface:: |
|
TypedData:: |
public | function |
Sets the context of a property or item via a context aware parent. Overrides TypedDataInterface:: |
|
TypedData:: |
public | function |
Validates the currently set data value. Overrides TypedDataInterface:: |
|
TypedDataTrait:: |
protected | property | The typed data manager used for creating the data types. | |
TypedDataTrait:: |
public | function | Gets the typed data manager. | 2 |
TypedDataTrait:: |
public | function | Sets the typed data manager. | 2 |