View source
<?php
namespace Drupal\menu_link_content\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\link\LinkItemInterface;
use Drupal\menu_link_content\MenuLinkContentInterface;
class MenuLinkContent extends ContentEntityBase implements MenuLinkContentInterface {
use EntityChangedTrait;
protected $insidePlugin = FALSE;
public function setInsidePlugin() {
$this->insidePlugin = TRUE;
}
public function getTitle() {
return $this
->get('title')->value;
}
public function getUrlObject() {
return $this->link
->first()
->getUrl();
}
public function getMenuName() {
return $this
->get('menu_name')->value;
}
public function getDescription() {
return $this
->get('description')->value;
}
public function getPluginId() {
return 'menu_link_content:' . $this
->uuid();
}
public function isEnabled() {
return (bool) $this
->get('enabled')->value;
}
public function isExpanded() {
return (bool) $this
->get('expanded')->value;
}
public function getParentId() {
return $this
->get('parent')->value;
}
public function getWeight() {
return (int) $this
->get('weight')->value;
}
public function getPluginDefinition() {
$definition = array();
$definition['class'] = 'Drupal\\menu_link_content\\Plugin\\Menu\\MenuLinkContent';
$definition['menu_name'] = $this
->getMenuName();
if ($url_object = $this
->getUrlObject()) {
$definition['url'] = NULL;
$definition['route_name'] = NULL;
$definition['route_parameters'] = [];
if (!$url_object
->isRouted()) {
$definition['url'] = $url_object
->getUri();
}
else {
$definition['route_name'] = $url_object
->getRouteName();
$definition['route_parameters'] = $url_object
->getRouteParameters();
}
$definition['options'] = $url_object
->getOptions();
}
$definition['title'] = $this
->getTitle();
$definition['description'] = $this
->getDescription();
$definition['weight'] = $this
->getWeight();
$definition['id'] = $this
->getPluginId();
$definition['metadata'] = array(
'entity_id' => $this
->id(),
);
$definition['form_class'] = '\\Drupal\\menu_link_content\\Form\\MenuLinkContentForm';
$definition['enabled'] = $this
->isEnabled() ? 1 : 0;
$definition['expanded'] = $this
->isExpanded() ? 1 : 0;
$definition['provider'] = 'menu_link_content';
$definition['discovered'] = 0;
$definition['parent'] = $this
->getParentId();
return $definition;
}
public static function preCreate(EntityStorageInterface $storage, array &$values) {
$values += [
'bundle' => 'menu_link_content',
];
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if (parse_url($this->link->uri, PHP_URL_SCHEME) === 'internal') {
$this
->setRequiresRediscovery(TRUE);
}
else {
$this
->setRequiresRediscovery(FALSE);
}
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
if ($update && $menu_link_manager
->getDefinition($this
->getPluginId())) {
if (!$this->insidePlugin) {
$menu_link_manager
->updateDefinition($this
->getPluginId(), $this
->getPluginDefinition(), FALSE);
}
}
else {
$menu_link_manager
->addDefinition($this
->getPluginId(), $this
->getPluginDefinition());
}
}
public static function preDelete(EntityStorageInterface $storage, array $entities) {
parent::preDelete($storage, $entities);
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
foreach ($entities as $menu_link) {
$menu_link_manager
->removeDefinition($menu_link
->getPluginId(), FALSE);
}
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Entity ID'))
->setDescription(t('The entity ID for this menu link content entity.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The content menu link UUID.'))
->setReadOnly(TRUE);
$fields['bundle'] = BaseFieldDefinition::create('string')
->setLabel(t('Bundle'))
->setDescription(t('The content menu link bundle.'))
->setSetting('max_length', EntityTypeInterface::BUNDLE_MAX_LENGTH)
->setSetting('is_ascii', TRUE)
->setReadOnly(TRUE);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Menu link title'))
->setDescription(t('The text to be used for this link in the menu.'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
))
->setDisplayOptions('form', array(
'type' => 'string_textfield',
'weight' => -5,
))
->setDisplayConfigurable('form', TRUE);
$fields['description'] = BaseFieldDefinition::create('string')
->setLabel(t('Description'))
->setDescription(t('Shown when hovering over the menu link.'))
->setTranslatable(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'string',
'weight' => 0,
))
->setDisplayOptions('form', array(
'type' => 'string_textfield',
'weight' => 0,
));
$fields['menu_name'] = BaseFieldDefinition::create('string')
->setLabel(t('Menu name'))
->setDescription(t('The menu name. All links with the same menu name (such as "tools") are part of the same menu.'))
->setDefaultValue('tools')
->setSetting('is_ascii', TRUE);
$fields['link'] = BaseFieldDefinition::create('link')
->setLabel(t('Link'))
->setDescription(t('The location this menu link points to.'))
->setRequired(TRUE)
->setSettings(array(
'link_type' => LinkItemInterface::LINK_GENERIC,
'title' => DRUPAL_DISABLED,
))
->setDisplayOptions('form', array(
'type' => 'link_default',
'weight' => -2,
));
$fields['external'] = BaseFieldDefinition::create('boolean')
->setLabel(t('External'))
->setDescription(t('A flag to indicate if the link points to a full URL starting with a protocol, like http:// (1 = external, 0 = internal).'))
->setDefaultValue(FALSE);
$fields['rediscover'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Indicates whether the menu link should be rediscovered'))
->setDefaultValue(FALSE);
$fields['weight'] = BaseFieldDefinition::create('integer')
->setLabel(t('Weight'))
->setDescription(t('Link weight among links in the same menu at the same depth. In the menu, the links with high weight will sink and links with a low weight will be positioned nearer the top.'))
->setDefaultValue(0)
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'integer',
'weight' => 0,
))
->setDisplayOptions('form', array(
'type' => 'number',
'weight' => 20,
));
$fields['expanded'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Show as expanded'))
->setDescription(t('If selected and this menu link has children, the menu will always appear expanded.'))
->setDefaultValue(FALSE)
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'boolean',
'weight' => 0,
))
->setDisplayOptions('form', array(
'settings' => array(
'display_label' => TRUE,
),
'weight' => 0,
));
$fields['enabled'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Enabled'))
->setDescription(t('A flag for whether the link should be enabled in menus or hidden.'))
->setDefaultValue(TRUE)
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'boolean',
'weight' => 0,
))
->setDisplayOptions('form', array(
'settings' => array(
'display_label' => TRUE,
),
'weight' => -1,
));
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language'))
->setDescription(t('The menu link language code.'))
->setTranslatable(TRUE)
->setDisplayOptions('view', array(
'type' => 'hidden',
))
->setDisplayOptions('form', array(
'type' => 'language_select',
'weight' => 2,
));
$fields['parent'] = BaseFieldDefinition::create('string')
->setLabel(t('Parent plugin ID'))
->setDescription(t('The ID of the parent menu link plugin, or empty string when at the top level of the hierarchy.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the menu link was last edited.'))
->setTranslatable(TRUE);
return $fields;
}
public function requiresRediscovery() {
return $this
->get('rediscover')->value;
}
public function setRequiresRediscovery($rediscovery) {
$this
->set('rediscover', $rediscovery);
return $this;
}
}