View source
<?php
namespace Drupal\entity_hierarchy_microsite\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\entity_hierarchy_microsite\EntityHooks;
class MicrositeMenuItemOverride extends ContentEntityBase implements MicrositeMenuItemOverrideInterface {
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$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)
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE);
$fields['weight'] = BaseFieldDefinition::create('integer')
->setRequired(TRUE)
->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', [
'label' => 'hidden',
'type' => 'number_integer',
'weight' => 0,
])
->setDisplayOptions('form', [
'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. This option may be overridden for the entire menu tree when placing a menu block.'))
->setDefaultValue(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'boolean',
'weight' => 0,
])
->setDisplayOptions('form', [
'settings' => [
'display_label' => TRUE,
],
'weight' => 0,
]);
$fields['enabled'] = BaseFieldDefinition::create('boolean')
->setLabel(new TranslatableMarkup('Enabled'))
->setDefaultValue(TRUE)
->setDescription(t('A flag for whether the link should be enabled in menus or hidden.'))
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'boolean',
'weight' => 0,
])
->setDisplayOptions('form', [
'settings' => [
'display_label' => TRUE,
],
'weight' => -1,
]);
$fields['parent'] = BaseFieldDefinition::create('string')
->setLabel(t('Parent plugin ID'))
->setRequired(TRUE)
->setDescription(t('The ID of the parent menu link plugin, or empty string when at the top level of the hierarchy.'));
$fields['target'] = BaseFieldDefinition::create('uuid')
->setRequired(TRUE)
->addConstraint('UniqueField')
->setLabel(new TranslatableMarkup('Target UUID of item to override'));
return $fields;
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
\Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityHooks::class)
->onMenuOverridePostSave($this, $update);
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
\Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityHooks::class)
->onMenuOverridePostDelete($entities);
}
public function getTarget() {
return $this
->get('target')->value;
}
public function getParent() {
return $this
->get('parent')->value;
}
public function isEnabled() {
return (bool) $this
->get('enabled')->value;
}
public function isExpanded() {
return (bool) $this
->get('expanded')->value;
}
public function getWeight() {
return (int) $this
->get('weight')->value;
}
}