diff --git a/core/modules/menu_link_content/menu_link_content.install b/core/modules/menu_link_content/menu_link_content.install index 43c75ec0fe84c461eba595de2209ad6d0ae29835..8a04d028599ef379103757b76252f651c53ca59a 100644 --- a/core/modules/menu_link_content/menu_link_content.install +++ b/core/modules/menu_link_content/menu_link_content.install @@ -16,3 +16,29 @@ function menu_link_content_install() { // https://www.drupal.org/node/1965074 module_set_weight('menu_link_content', 1); } + +/** + * Add the publishing status entity key to custom menu links. + */ +function menu_link_content_update_8601() { + $definition_update_manager = \Drupal::entityDefinitionUpdateManager(); + $entity_type = $definition_update_manager->getEntityType('menu_link_content'); + + // Add the published entity key to the menu_link_content entity type. + $entity_keys = $entity_type->getKeys(); + $entity_keys['published'] = 'enabled'; + $entity_type->set('entity_keys', $entity_keys); + $definition_update_manager->updateEntityType($entity_type); + + // @todo The above should be enough, since that is the only definition that + // changed. But \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema varies + // field schema by whether a field is an entity key, so invoke + // EntityDefinitionUpdateManagerInterface::updateFieldStorageDefinition() + // with an unmodified field storage definition to trigger the necessary + // changes. SqlContentEntityStorageSchema::onEntityTypeUpdate() should be + // fixed to automatically handle this. + // @see https://www.drupal.org/node/2554245 + $definition_update_manager->updateFieldStorageDefinition($definition_update_manager->getFieldStorageDefinition('enabled', 'menu_link_content')); + + return t('The publishing status entity key has been added to custom menu links.'); +} diff --git a/core/modules/menu_link_content/src/Entity/MenuLinkContent.php b/core/modules/menu_link_content/src/Entity/MenuLinkContent.php index 0b8e7ccae0e9946142ffc5777b7888b4a3933760..0dfe7cb435e49c7420dac08dad7dba0889988770 100644 --- a/core/modules/menu_link_content/src/Entity/MenuLinkContent.php +++ b/core/modules/menu_link_content/src/Entity/MenuLinkContent.php @@ -4,6 +4,7 @@ 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; @@ -44,7 +45,8 @@ * "label" = "title", * "langcode" = "langcode", * "uuid" = "uuid", - * "bundle" = "bundle" + * "bundle" = "bundle", + * "published" = "enabled", * }, * links = { * "canonical" = "/admin/structure/menu/item/{menu_link_content}/edit", @@ -56,6 +58,7 @@ class MenuLinkContent extends ContentEntityBase implements MenuLinkContentInterface { use EntityChangedTrait; + use EntityPublishedTrait; /** * A flag for whether this entity is wrapped in a plugin instance. @@ -254,6 +257,9 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { /** @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.')); @@ -354,19 +360,21 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { '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')) diff --git a/core/modules/menu_link_content/src/MenuLinkContentInterface.php b/core/modules/menu_link_content/src/MenuLinkContentInterface.php index 5ce61f386759588c8f7a8a4e203e84ddecdae083..1d01522dca335e67627fc57a09aa6d7607995d1f 100644 --- a/core/modules/menu_link_content/src/MenuLinkContentInterface.php +++ b/core/modules/menu_link_content/src/MenuLinkContentInterface.php @@ -4,11 +4,12 @@ use Drupal\Core\Entity\EntityChangedInterface; use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\EntityPublishedInterface; /** * Defines an interface for custom menu links. */ -interface MenuLinkContentInterface extends ContentEntityInterface, EntityChangedInterface { +interface MenuLinkContentInterface extends ContentEntityInterface, EntityChangedInterface, EntityPublishedInterface { /** * Flags this instance as being wrapped in a menu link plugin instance. diff --git a/core/modules/menu_link_content/tests/src/Functional/Update/MenuLinkContentUpdateTest.php b/core/modules/menu_link_content/tests/src/Functional/Update/MenuLinkContentUpdateTest.php new file mode 100644 index 0000000000000000000000000000000000000000..84d770a277f8dde1fb561a57c650fe45d81c90ef --- /dev/null +++ b/core/modules/menu_link_content/tests/src/Functional/Update/MenuLinkContentUpdateTest.php @@ -0,0 +1,70 @@ +<?php + +namespace Drupal\Tests\menu_link_content\Functional\Update; + +use Drupal\FunctionalTests\Update\UpdatePathTestBase; +use Drupal\user\Entity\User; + +/** + * Tests the upgrade path for custom menu links. + * + * @group menu_link_content + * @group Update + */ +class MenuLinkContentUpdateTest extends UpdatePathTestBase { + + /** + * {@inheritdoc} + */ + protected function setDatabaseDumpFiles() { + $this->databaseDumpFiles = [ + __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.filled.standard.php.gz', + ]; + } + + /** + * Tests the addition of the publishing status entity key. + * + * @see menu_link_content_update_8601() + * + * @group failing + */ + public function testPublishedEntityKeyAdditon() { + $this->runUpdates(); + + // Log in as user 1. + $account = User::load(1); + $account->passRaw = 'drupal'; + $this->drupalLogin($account); + + // Make sure our custom menu link exists. + $assert_session = $this->assertSession(); + $this->drupalGet('admin/structure/menu/item/1/edit'); + $assert_session->checkboxChecked('edit-enabled-value'); + + // Check that custom menu links can be created, saved and then loaded. + $storage = \Drupal::entityTypeManager()->getStorage('menu_link_content'); + /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $menu_link */ + $menu_link = $storage->create([ + 'menu_name' => 'main', + 'link' => 'route:user.page', + 'title' => 'Pineapple', + ]); + $menu_link->save(); + + $menu_link = $storage->loadUnchanged($menu_link->id()); + + $this->assertEquals('main', $menu_link->getMenuName()); + $this->assertEquals('Pineapple', $menu_link->label()); + $this->assertEquals('route:user.page', $menu_link->link->uri); + $this->assertTrue($menu_link->isPublished()); + } + + /** + * {@inheritdoc} + */ + protected function replaceUser1() { + // Do not replace the user from our dump. + } + +}