Issue #2981915 by amateescu, timmillwood: Update MenuLinkContent to use EntityPublishedInterface
							parent
							
								
									bc2d4bfeb6
								
							
						
					
					
						commit
						e2651fce3d
					
				| 
						 | 
					@ -16,3 +16,29 @@ function menu_link_content_install() {
 | 
				
			||||||
  //   https://www.drupal.org/node/1965074
 | 
					  //   https://www.drupal.org/node/1965074
 | 
				
			||||||
  module_set_weight('menu_link_content', 1);
 | 
					  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 @@ namespace Drupal\menu_link_content\Entity;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use Drupal\Core\Entity\ContentEntityBase;
 | 
					use Drupal\Core\Entity\ContentEntityBase;
 | 
				
			||||||
use Drupal\Core\Entity\EntityChangedTrait;
 | 
					use Drupal\Core\Entity\EntityChangedTrait;
 | 
				
			||||||
 | 
					use Drupal\Core\Entity\EntityPublishedTrait;
 | 
				
			||||||
use Drupal\Core\Entity\EntityStorageInterface;
 | 
					use Drupal\Core\Entity\EntityStorageInterface;
 | 
				
			||||||
use Drupal\Core\Entity\EntityTypeInterface;
 | 
					use Drupal\Core\Entity\EntityTypeInterface;
 | 
				
			||||||
use Drupal\Core\Field\BaseFieldDefinition;
 | 
					use Drupal\Core\Field\BaseFieldDefinition;
 | 
				
			||||||
| 
						 | 
					@ -44,7 +45,8 @@ use Drupal\menu_link_content\MenuLinkContentInterface;
 | 
				
			||||||
 *     "label" = "title",
 | 
					 *     "label" = "title",
 | 
				
			||||||
 *     "langcode" = "langcode",
 | 
					 *     "langcode" = "langcode",
 | 
				
			||||||
 *     "uuid" = "uuid",
 | 
					 *     "uuid" = "uuid",
 | 
				
			||||||
 *     "bundle" = "bundle"
 | 
					 *     "bundle" = "bundle",
 | 
				
			||||||
 | 
					 *     "published" = "enabled",
 | 
				
			||||||
 *   },
 | 
					 *   },
 | 
				
			||||||
 *   links = {
 | 
					 *   links = {
 | 
				
			||||||
 *     "canonical" = "/admin/structure/menu/item/{menu_link_content}/edit",
 | 
					 *     "canonical" = "/admin/structure/menu/item/{menu_link_content}/edit",
 | 
				
			||||||
| 
						 | 
					@ -56,6 +58,7 @@ use Drupal\menu_link_content\MenuLinkContentInterface;
 | 
				
			||||||
class MenuLinkContent extends ContentEntityBase implements MenuLinkContentInterface {
 | 
					class MenuLinkContent extends ContentEntityBase implements MenuLinkContentInterface {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  use EntityChangedTrait;
 | 
					  use EntityChangedTrait;
 | 
				
			||||||
 | 
					  use EntityPublishedTrait;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * A flag for whether this entity is wrapped in a plugin instance.
 | 
					   * A flag for whether this entity is wrapped in a plugin instance.
 | 
				
			||||||
| 
						 | 
					@ -254,6 +257,9 @@ class MenuLinkContent extends ContentEntityBase implements MenuLinkContentInterf
 | 
				
			||||||
    /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
 | 
					    /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
 | 
				
			||||||
    $fields = parent::baseFieldDefinitions($entity_type);
 | 
					    $fields = parent::baseFieldDefinitions($entity_type);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Add the publishing status field.
 | 
				
			||||||
 | 
					    $fields += static::publishedBaseFieldDefinitions($entity_type);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    $fields['id']->setLabel(t('Entity ID'))
 | 
					    $fields['id']->setLabel(t('Entity ID'))
 | 
				
			||||||
      ->setDescription(t('The entity ID for this menu link content entity.'));
 | 
					      ->setDescription(t('The entity ID for this menu link content entity.'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -354,19 +360,21 @@ class MenuLinkContent extends ContentEntityBase implements MenuLinkContentInterf
 | 
				
			||||||
        'weight' => 0,
 | 
					        'weight' => 0,
 | 
				
			||||||
      ]);
 | 
					      ]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    $fields['enabled'] = BaseFieldDefinition::create('boolean')
 | 
					    // Override some properties of the published field added by
 | 
				
			||||||
      ->setLabel(t('Enabled'))
 | 
					    // \Drupal\Core\Entity\EntityPublishedTrait::publishedBaseFieldDefinitions().
 | 
				
			||||||
      ->setDescription(t('A flag for whether the link should be enabled in menus or hidden.'))
 | 
					    $fields['enabled']->setLabel(t('Enabled'));
 | 
				
			||||||
      ->setDefaultValue(TRUE)
 | 
					    $fields['enabled']->setDescription(t('A flag for whether the link should be enabled in menus or hidden.'));
 | 
				
			||||||
      ->setDisplayOptions('view', [
 | 
					    $fields['enabled']->setTranslatable(FALSE);
 | 
				
			||||||
        'label' => 'hidden',
 | 
					    $fields['enabled']->setRevisionable(FALSE);
 | 
				
			||||||
        'type' => 'boolean',
 | 
					    $fields['enabled']->setDisplayOptions('view', [
 | 
				
			||||||
        'weight' => 0,
 | 
					      'label' => 'hidden',
 | 
				
			||||||
      ])
 | 
					      'type' => 'boolean',
 | 
				
			||||||
      ->setDisplayOptions('form', [
 | 
					      'weight' => 0,
 | 
				
			||||||
        'settings' => ['display_label' => TRUE],
 | 
					    ]);
 | 
				
			||||||
        'weight' => -1,
 | 
					    $fields['enabled']->setDisplayOptions('form', [
 | 
				
			||||||
      ]);
 | 
					      'settings' => ['display_label' => TRUE],
 | 
				
			||||||
 | 
					      'weight' => -1,
 | 
				
			||||||
 | 
					    ]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    $fields['parent'] = BaseFieldDefinition::create('string')
 | 
					    $fields['parent'] = BaseFieldDefinition::create('string')
 | 
				
			||||||
      ->setLabel(t('Parent plugin ID'))
 | 
					      ->setLabel(t('Parent plugin ID'))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,11 +4,12 @@ namespace Drupal\menu_link_content;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use Drupal\Core\Entity\EntityChangedInterface;
 | 
					use Drupal\Core\Entity\EntityChangedInterface;
 | 
				
			||||||
use Drupal\Core\Entity\ContentEntityInterface;
 | 
					use Drupal\Core\Entity\ContentEntityInterface;
 | 
				
			||||||
 | 
					use Drupal\Core\Entity\EntityPublishedInterface;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Defines an interface for custom menu links.
 | 
					 * 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.
 | 
					   * Flags this instance as being wrapped in a menu link plugin instance.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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.
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue