Commit e2651fce authored by catch's avatar catch

Issue #2981915 by amateescu, timmillwood: Update MenuLinkContent to use EntityPublishedInterface

parent bc2d4bfe
......@@ -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.');
}
......@@ -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'))
......
......@@ -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.
......
<?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.
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment