use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
+use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
* @ContentEntityType(
* id = "menu_link_content",
* label = @Translation("Custom menu link"),
+ * label_collection = @Translation("Custom menu links"),
+ * label_singular = @Translation("custom menu link"),
+ * label_plural = @Translation("custom menu links"),
+ * label_count = @PluralTranslation(
+ * singular = "@count custom menu link",
+ * plural = "@count custom menu links",
+ * ),
* handlers = {
* "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage",
* "storage_schema" = "Drupal\menu_link_content\MenuLinkContentStorageSchema",
* "label" = "title",
* "langcode" = "langcode",
* "uuid" = "uuid",
- * "bundle" = "bundle"
+ * "bundle" = "bundle",
+ * "published" = "enabled",
* },
* links = {
* "canonical" = "/admin/structure/menu/item/{menu_link_content}/edit",
class MenuLinkContent extends ContentEntityBase implements MenuLinkContentInterface {
use EntityChangedTrait;
+ use EntityPublishedTrait;
/**
* A flag for whether this entity is wrapped in a plugin instance.
$this->setRequiresRediscovery(FALSE);
}
}
+
/**
* {@inheritdoc}
*/
foreach ($entities as $menu_link) {
/** @var \Drupal\menu_link_content\Entity\MenuLinkContent $menu_link */
$menu_link_manager->removeDefinition($menu_link->getPluginId(), FALSE);
+
+ // Children get re-attached to the menu link's parent.
+ $parent_plugin_id = $menu_link->getParentId();
+ $children = $storage->loadByProperties(['parent' => $menu_link->getPluginId()]);
+ foreach ($children as $child) {
+ /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $child */
+ $child->set('parent', $parent_plugin_id)->save();
+ }
}
}
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
$fields = parent::baseFieldDefinitions($entity_type);
+ // Add the publishing status field.
+ $fields += static::publishedBaseFieldDefinitions($entity_type);
+
$fields['id']->setLabel(t('Entity ID'))
->setDescription(t('The entity ID for this menu link content entity.'));
'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', [
- 'label' => 'hidden',
- 'type' => 'boolean',
- 'weight' => 0,
- ])
- ->setDisplayOptions('form', [
- 'settings' => ['display_label' => TRUE],
- 'weight' => -1,
- ]);
+ // Override some properties of the published field added by
+ // \Drupal\Core\Entity\EntityPublishedTrait::publishedBaseFieldDefinitions().
+ $fields['enabled']->setLabel(t('Enabled'));
+ $fields['enabled']->setDescription(t('A flag for whether the link should be enabled in menus or hidden.'));
+ $fields['enabled']->setTranslatable(FALSE);
+ $fields['enabled']->setRevisionable(FALSE);
+ $fields['enabled']->setDisplayOptions('view', [
+ 'label' => 'hidden',
+ 'type' => 'boolean',
+ 'weight' => 0,
+ ]);
+ $fields['enabled']->setDisplayOptions('form', [
+ 'settings' => ['display_label' => TRUE],
+ 'weight' => -1,
+ ]);
$fields['parent'] = BaseFieldDefinition::create('string')
->setLabel(t('Parent plugin ID'))