Loading entity_comparison.info.yml +0 −2 Changes for entity_comparison.info.yml: 0 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -5,5 +5,3 @@ core: 8.x core_version_requirement: ^8 || ^9 package: Entity configure: entity.entity_comparison.collection dependencies: - drupal:field_ui src/Entity/EntityComparison.php +12 −2 Changes for src/Entity/EntityComparison.php: 12 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -7,7 +7,6 @@ use Drupal\Core\Entity\Entity\EntityViewMode; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Link; use Drupal\Core\Url; use Drupal\field_ui\FieldUI; /** * Defines the Entity comparison entity. Loading Loading @@ -279,6 +278,8 @@ class EntityComparison extends ConfigEntityBase implements EntityComparisonInter $display->save(); } // Change success message if Field UI is enabled. if (\Drupal::service('module_handler')->moduleExists('field_ui')) { // Get url to the view mode page. $url = $this->getOverviewUrl($display_id); Loading @@ -287,6 +288,13 @@ class EntityComparison extends ConfigEntityBase implements EntityComparisonInter '%display_mode' => $this->label(), ':url' => $url->toString(), ])); } else { // Show success message. \Drupal::messenger()->addMessage(t('The %display_mode mode now uses custom display settings. To configure them, enable Field UI module.', [ '%display_mode' => $this->label(), ])); } // Enable the created view mode on the target bundle's manage display page. $display->set('status', TRUE); Loading @@ -298,9 +306,11 @@ class EntityComparison extends ConfigEntityBase implements EntityComparisonInter */ protected function getOverviewUrl($mode) { $entity_type = \Drupal::entityTypeManager()->getDefinition($this->getTargetEntityType()); $bundle_parameter_key = $entity_type->getBundleEntityType() ?: 'bundle'; return Url::fromRoute('entity.entity_view_display.' . $this->getTargetEntityType() . '.view_mode', [ 'view_mode_name' => $mode, ] + FieldUI::getRouteBundleParameter($entity_type, $this->getTargetBundleType())); $bundle_parameter_key => $this->getTargetBundleType(), ]); } } src/Tests/EntityComparisonTest.php +62 −47 Changes for src/Tests/EntityComparisonTest.php: 62 added lines, 47 removed lines. Original line number Diff line number Diff line Loading @@ -26,7 +26,7 @@ class EntityComparisonTest extends BrowserTestBase { * * @var string[] */ protected static $modules = ['entity_comparison', 'field_ui', 'node']; protected static $modules = ['entity_comparison', 'node']; /** * Don't check for or validate config schema. Loading @@ -52,11 +52,7 @@ class EntityComparisonTest extends BrowserTestBase { 'administer entity comparison', 'bypass node access', 'administer content types', 'administer node fields', 'administer node form display', 'administer node display', 'access content', 'administer display modes', ]; /** Loading Loading @@ -92,6 +88,7 @@ class EntityComparisonTest extends BrowserTestBase { // Create a store administrator user account. $this->adminUser = $this->drupalCreateUser($adminPermissions); // Admin login. $this->drupalLogin($this->adminUser); // Create product content type. Loading @@ -108,7 +105,6 @@ class EntityComparisonTest extends BrowserTestBase { 'widget_type' => 'string_textfield', ], ]; $this->addFieldsToEntity('node', 'product', $fields); // Create product contents. Loading @@ -125,24 +121,44 @@ class EntityComparisonTest extends BrowserTestBase { 'bundle_type' => 'product', ])->save(); // Update custom view mode. $this->updateViewMode(); // Logout. $this->drupalLogout(); } /** * Update view mode. * Add fields to product content type. */ protected function updateViewMode() { $manage_display = '/admin/structure/types/manage/product/display/product_product_comparison'; $edit = [ 'fields[price][region]' => 'content', 'fields[sku][region]' => 'content', 'fields[link_for_entity_comparison_product_comparison][region]' => 'hidden', ]; $this->drupalGet($manage_display); $this->submitForm($edit, t('Save')); protected function addFieldsToEntity($entity_type, $bundle_type, $fields) { foreach ($fields as $field_name => $field) { // Create a field. $field_storage = FieldStorageConfig::create([ 'field_name' => $field_name, 'entity_type' => $entity_type, 'type' => $field['type'], ]); $field_storage->save(); FieldConfig::create([ 'field_storage' => $field_storage, 'bundle' => $bundle_type, 'label' => $field_name, ])->save(); $entity_type_id = $entity_type . '.' . $bundle_type . '.' . 'default'; \Drupal::entityTypeManager()->getStorage('entity_form_display') ->load($entity_type_id) ->setComponent($field_name, [ 'type' => $field['widget_type'], 'settings' => [ 'placeholder' => 'A placeholder on ' . $field['type'], ], ]) ->save(); \Drupal::service('entity_display.repository')->getViewDisplay($entity_type, $bundle_type) ->setComponent($field_name) ->save(); } } /** Loading Loading @@ -176,39 +192,35 @@ class EntityComparisonTest extends BrowserTestBase { } /** * Add fields to product content type. * Use Field UI module to re-order fields. */ protected function addFieldsToEntity($entity_type, $bundle_type, $fields) { foreach ($fields as $field_name => $field) { protected function useFieldUi() { // Login as admin. $this->drupalLogin($this->adminUser); // Create a field. $field_storage = FieldStorageConfig::create([ 'field_name' => $field_name, 'entity_type' => $entity_type, 'type' => $field['type'], // Enable Field UI module. \Drupal::service('module_installer')->install(['field_ui']); // Grant Field UI permissions. $this->grantPermissions($this->adminUser->roles[0]->entity, [ 'administer node fields', 'administer node form display', 'administer node display', 'administer display modes', ]); $field_storage->save(); FieldConfig::create([ 'field_storage' => $field_storage, 'bundle' => $bundle_type, 'label' => $field_name, ])->save(); // Change custom view mode. $manage_display = '/admin/structure/types/manage/product/display/product_product_comparison'; $edit = [ 'fields[price][region]' => 'content', 'fields[sku][region]' => 'content', 'fields[link_for_entity_comparison_product_comparison][region]' => 'hidden', ]; $this->drupalGet($manage_display); $this->submitForm($edit, t('Save')); $entity_type_id = $entity_type . '.' . $bundle_type . '.' . 'default'; \Drupal::entityTypeManager()->getStorage('entity_form_display') ->load($entity_type_id) ->setComponent($field_name, [ 'type' => $field['widget_type'], 'settings' => [ 'placeholder' => 'A placeholder on ' . $field['type'], ], ]) ->save(); \Drupal::service('entity_display.repository')->getViewDisplay($entity_type, $bundle_type) ->setComponent($field_name) ->save(); } // Logout. $this->drupalLogout(); } /** Loading @@ -224,6 +236,9 @@ class EntityComparisonTest extends BrowserTestBase { * Check that the corresponding view mode is created and enabled successfully. */ public function testViewmodeIsEnabled() { // Use Field UI module. $this->useFieldUi(); $this->drupalLogin($this->adminUser); $this->drupalGet('/admin/structure/types/manage/product/display/product_product_comparison'); $this->assertSession()->responseContains('product_comparison'); Loading Loading
entity_comparison.info.yml +0 −2 Changes for entity_comparison.info.yml: 0 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -5,5 +5,3 @@ core: 8.x core_version_requirement: ^8 || ^9 package: Entity configure: entity.entity_comparison.collection dependencies: - drupal:field_ui
src/Entity/EntityComparison.php +12 −2 Changes for src/Entity/EntityComparison.php: 12 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -7,7 +7,6 @@ use Drupal\Core\Entity\Entity\EntityViewMode; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Link; use Drupal\Core\Url; use Drupal\field_ui\FieldUI; /** * Defines the Entity comparison entity. Loading Loading @@ -279,6 +278,8 @@ class EntityComparison extends ConfigEntityBase implements EntityComparisonInter $display->save(); } // Change success message if Field UI is enabled. if (\Drupal::service('module_handler')->moduleExists('field_ui')) { // Get url to the view mode page. $url = $this->getOverviewUrl($display_id); Loading @@ -287,6 +288,13 @@ class EntityComparison extends ConfigEntityBase implements EntityComparisonInter '%display_mode' => $this->label(), ':url' => $url->toString(), ])); } else { // Show success message. \Drupal::messenger()->addMessage(t('The %display_mode mode now uses custom display settings. To configure them, enable Field UI module.', [ '%display_mode' => $this->label(), ])); } // Enable the created view mode on the target bundle's manage display page. $display->set('status', TRUE); Loading @@ -298,9 +306,11 @@ class EntityComparison extends ConfigEntityBase implements EntityComparisonInter */ protected function getOverviewUrl($mode) { $entity_type = \Drupal::entityTypeManager()->getDefinition($this->getTargetEntityType()); $bundle_parameter_key = $entity_type->getBundleEntityType() ?: 'bundle'; return Url::fromRoute('entity.entity_view_display.' . $this->getTargetEntityType() . '.view_mode', [ 'view_mode_name' => $mode, ] + FieldUI::getRouteBundleParameter($entity_type, $this->getTargetBundleType())); $bundle_parameter_key => $this->getTargetBundleType(), ]); } }
src/Tests/EntityComparisonTest.php +62 −47 Changes for src/Tests/EntityComparisonTest.php: 62 added lines, 47 removed lines. Original line number Diff line number Diff line Loading @@ -26,7 +26,7 @@ class EntityComparisonTest extends BrowserTestBase { * * @var string[] */ protected static $modules = ['entity_comparison', 'field_ui', 'node']; protected static $modules = ['entity_comparison', 'node']; /** * Don't check for or validate config schema. Loading @@ -52,11 +52,7 @@ class EntityComparisonTest extends BrowserTestBase { 'administer entity comparison', 'bypass node access', 'administer content types', 'administer node fields', 'administer node form display', 'administer node display', 'access content', 'administer display modes', ]; /** Loading Loading @@ -92,6 +88,7 @@ class EntityComparisonTest extends BrowserTestBase { // Create a store administrator user account. $this->adminUser = $this->drupalCreateUser($adminPermissions); // Admin login. $this->drupalLogin($this->adminUser); // Create product content type. Loading @@ -108,7 +105,6 @@ class EntityComparisonTest extends BrowserTestBase { 'widget_type' => 'string_textfield', ], ]; $this->addFieldsToEntity('node', 'product', $fields); // Create product contents. Loading @@ -125,24 +121,44 @@ class EntityComparisonTest extends BrowserTestBase { 'bundle_type' => 'product', ])->save(); // Update custom view mode. $this->updateViewMode(); // Logout. $this->drupalLogout(); } /** * Update view mode. * Add fields to product content type. */ protected function updateViewMode() { $manage_display = '/admin/structure/types/manage/product/display/product_product_comparison'; $edit = [ 'fields[price][region]' => 'content', 'fields[sku][region]' => 'content', 'fields[link_for_entity_comparison_product_comparison][region]' => 'hidden', ]; $this->drupalGet($manage_display); $this->submitForm($edit, t('Save')); protected function addFieldsToEntity($entity_type, $bundle_type, $fields) { foreach ($fields as $field_name => $field) { // Create a field. $field_storage = FieldStorageConfig::create([ 'field_name' => $field_name, 'entity_type' => $entity_type, 'type' => $field['type'], ]); $field_storage->save(); FieldConfig::create([ 'field_storage' => $field_storage, 'bundle' => $bundle_type, 'label' => $field_name, ])->save(); $entity_type_id = $entity_type . '.' . $bundle_type . '.' . 'default'; \Drupal::entityTypeManager()->getStorage('entity_form_display') ->load($entity_type_id) ->setComponent($field_name, [ 'type' => $field['widget_type'], 'settings' => [ 'placeholder' => 'A placeholder on ' . $field['type'], ], ]) ->save(); \Drupal::service('entity_display.repository')->getViewDisplay($entity_type, $bundle_type) ->setComponent($field_name) ->save(); } } /** Loading Loading @@ -176,39 +192,35 @@ class EntityComparisonTest extends BrowserTestBase { } /** * Add fields to product content type. * Use Field UI module to re-order fields. */ protected function addFieldsToEntity($entity_type, $bundle_type, $fields) { foreach ($fields as $field_name => $field) { protected function useFieldUi() { // Login as admin. $this->drupalLogin($this->adminUser); // Create a field. $field_storage = FieldStorageConfig::create([ 'field_name' => $field_name, 'entity_type' => $entity_type, 'type' => $field['type'], // Enable Field UI module. \Drupal::service('module_installer')->install(['field_ui']); // Grant Field UI permissions. $this->grantPermissions($this->adminUser->roles[0]->entity, [ 'administer node fields', 'administer node form display', 'administer node display', 'administer display modes', ]); $field_storage->save(); FieldConfig::create([ 'field_storage' => $field_storage, 'bundle' => $bundle_type, 'label' => $field_name, ])->save(); // Change custom view mode. $manage_display = '/admin/structure/types/manage/product/display/product_product_comparison'; $edit = [ 'fields[price][region]' => 'content', 'fields[sku][region]' => 'content', 'fields[link_for_entity_comparison_product_comparison][region]' => 'hidden', ]; $this->drupalGet($manage_display); $this->submitForm($edit, t('Save')); $entity_type_id = $entity_type . '.' . $bundle_type . '.' . 'default'; \Drupal::entityTypeManager()->getStorage('entity_form_display') ->load($entity_type_id) ->setComponent($field_name, [ 'type' => $field['widget_type'], 'settings' => [ 'placeholder' => 'A placeholder on ' . $field['type'], ], ]) ->save(); \Drupal::service('entity_display.repository')->getViewDisplay($entity_type, $bundle_type) ->setComponent($field_name) ->save(); } // Logout. $this->drupalLogout(); } /** Loading @@ -224,6 +236,9 @@ class EntityComparisonTest extends BrowserTestBase { * Check that the corresponding view mode is created and enabled successfully. */ public function testViewmodeIsEnabled() { // Use Field UI module. $this->useFieldUi(); $this->drupalLogin($this->adminUser); $this->drupalGet('/admin/structure/types/manage/product/display/product_product_comparison'); $this->assertSession()->responseContains('product_comparison'); Loading