Unverified Commit da94d7fc authored by larowlan's avatar larowlan

Issue #2367933 by phenaproxima, mikelutz, Berdir, voleger, amateescu,...

Issue #2367933 by phenaproxima, mikelutz, Berdir, voleger, amateescu, chr.fritsch, johndevman, Jo Fitzgerald, andypost, larowlan, alexpott: Move entity_get_(form_)display() to the entity display repository
parent 870b5646
......@@ -6,8 +6,6 @@
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
/**
* Clears the entity render cache for all entity types.
......@@ -440,49 +438,15 @@ function entity_view_multiple(array $entities, $view_mode, $langcode = NULL, $re
* @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface
* The entity view display associated with the view mode.
*
* @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0.
* If the display is available in configuration use:
* @code
* \Drupal::entityTypeManager()
* ->getStorage('entity_view_display')
* ->load($entity_type . '.' . $bundle . '.' . $view_mode);
* @endcode
* When the display is not available in configuration, you can create a new
* EntityViewDisplay object using:
* @code
* $values = array(
* 'targetEntityType' => $entity_type,
* 'bundle' => $bundle,
* 'mode' => $view_mode,
* 'status' => TRUE,
* );
* \Drupal::entityTypeManager()
* ->getStorage('entity_view_display')
* ->create($values);
* @endcode
* @deprecated in drupal:8.8.0 and will be removed from drupal:9.0.0. Use
* EntityDisplayRepositoryInterface::getViewDisplay() instead.
*
* @see \Drupal\Core\Entity\EntityStorageInterface::create()
* @see \Drupal\Core\Entity\EntityStorageInterface::load()
* @see https://www.drupal.org/node/2835616
*/
function entity_get_display($entity_type, $bundle, $view_mode) {
// Try loading the display from configuration.
$display = EntityViewDisplay::load($entity_type . '.' . $bundle . '.' . $view_mode);
// If not found, create a fresh display object. We do not preemptively create
// new entity_view_display configuration entries for each existing entity type
// and bundle whenever a new view mode becomes available. Instead,
// configuration entries are only created when a display object is explicitly
// configured and saved.
if (!$display) {
$display = EntityViewDisplay::create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $view_mode,
'status' => TRUE,
]);
}
return $display;
@trigger_error('entity_get_display() is deprecated in drupal:8.8.0. It will be removed before drupal:9.0.0. Use \Drupal::service(\'entity_display.repository\')->getViewDisplay() instead. See https://www.drupal.org/node/2835616', E_USER_DEPRECATED);
return \Drupal::service('entity_display.repository')
->getViewDisplay($entity_type, $bundle, $view_mode);
}
/**
......@@ -520,47 +484,15 @@ function entity_get_display($entity_type, $bundle, $view_mode) {
* @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface
* The entity form display associated with the given form mode.
*
* @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0.
* If the entity form display is available in configuration use:
* @code
* \Drupal::entityTypeManager()
* ->getStorage('entity_form_display')
* ->load($entity_type . '.' . $bundle . '.' . $form_mode);
* @endcode
* When the entity form display is not available in configuration, you can
* create a new EntityFormDisplay object using:
* @code
* $values = array(
* 'targetEntityType' => $entity_type,
* 'bundle' => $bundle,
* 'mode' => $form_mode,
* 'status' => TRUE,
* );
* \Drupal::entityTypeManager()
* ->getStorage('entity_form_display')
* ->create($values);
* @endcode
* @deprecated in drupal:8.8.0 and will be removed from drupal:9.0.0. Use
* EntityDisplayRepositoryInterface::getFormDisplay() instead.
*
* @see https://www.drupal.org/node/2835616
* @see \Drupal\Core\Entity\EntityStorageInterface::create()
* @see \Drupal\Core\Entity\EntityStorageInterface::load()
*/
function entity_get_form_display($entity_type, $bundle, $form_mode) {
// Try loading the entity from configuration.
$entity_form_display = EntityFormDisplay::load($entity_type . '.' . $bundle . '.' . $form_mode);
// If not found, create a fresh entity object. We do not preemptively create
// new entity form display configuration entries for each existing entity type
// and bundle whenever a new form mode becomes available. Instead,
// configuration entries are only created when an entity form display is
// explicitly configured and saved.
if (!$entity_form_display) {
$entity_form_display = EntityFormDisplay::create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $form_mode,
'status' => TRUE,
]);
}
return $entity_form_display;
@trigger_error('entity_get_form_display() is deprecated in drupal:8.8.0. It will be removed before drupal:9.0.0. Use \Drupal::service(\'entity_display.repository\')->getFormDisplay() instead. See https://www.drupal.org/node/2835616', E_USER_DEPRECATED);
return \Drupal::service('entity_display.repository')
->getFormDisplay($entity_type, $bundle, $form_mode);
}
......@@ -52,10 +52,11 @@ class EntityFormDisplay extends EntityDisplayBase implements EntityFormDisplayIn
*
* This method should only be used internally when rendering an entity form.
* When assigning suggested display options for a component in a given form
* mode, entity_get_form_display() should be used instead, in order to avoid
* inadvertently modifying the output of other form modes that might happen to
* use the 'default' display too. Those options will then be effectively
* applied only if the form mode is configured to use them.
* mode, EntityDisplayRepositoryInterface::getFormDisplay() should be used
* instead, in order to avoid inadvertently modifying the output of other form
* modes that might happen to use the 'default' display too. Those options
* will then be effectively applied only if the form mode is configured to use
* them.
*
* hook_entity_form_display_alter() is invoked on each display, allowing 3rd
* party code to alter the display options held in the display before they are
......@@ -73,7 +74,7 @@ class EntityFormDisplay extends EntityDisplayBase implements EntityFormDisplayIn
* @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface
* The display object that should be used to build the entity form.
*
* @see entity_get_form_display()
* @see \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getFormDisplay()
* @see hook_entity_form_display_alter()
*/
public static function collectRenderDisplay(FieldableEntityInterface $entity, $form_mode, $default_fallback = TRUE) {
......
......@@ -51,10 +51,10 @@ class EntityViewDisplay extends EntityDisplayBase implements EntityViewDisplayIn
*
* This method should only be used internally when rendering an entity. When
* assigning suggested display options for a component in a given view mode,
* entity_get_display() should be used instead, in order to avoid
* inadvertently modifying the output of other view modes that might happen to
* use the 'default' display too. Those options will then be effectively
* applied only if the view mode is configured to use them.
* EntityDisplayRepositoryInterface::getViewDisplay() should be used instead,
* in order to avoid inadvertently modifying the output of other view modes
* that might happen to use the 'default' display too. Those options will then
* be effectively applied only if the view mode is configured to use them.
*
* hook_entity_view_display_alter() is invoked on each display, allowing 3rd
* party code to alter the display options held in the display before they are
......@@ -69,7 +69,7 @@ class EntityViewDisplay extends EntityDisplayBase implements EntityViewDisplayIn
* The display objects to use to render the entities, keyed by entity
* bundle.
*
* @see entity_get_display()
* @see \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getViewDisplay()
* @see hook_entity_view_display_alter()
*/
public static function collectRenderDisplays($entities, $view_mode) {
......
......@@ -243,4 +243,50 @@ public function clearDisplayModeInfo() {
return $this;
}
/**
* {@inheritdoc}
*/
public function getViewDisplay($entity_type, $bundle, $view_mode = self::DEFAULT_DISPLAY_MODE) {
$storage = $this->entityTypeManager->getStorage('entity_view_display');
// Try loading the display from configuration; if not found, create a fresh
// display object. We do not preemptively create new entity_view_display
// configuration entries for each existing entity type and bundle whenever a
// new view mode becomes available. Instead, configuration entries are only
// created when a display object is explicitly configured and saved.
$entity_view_display = $storage->load($entity_type . '.' . $bundle . '.' . $view_mode);
if (!$entity_view_display) {
$entity_view_display = $storage->create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $view_mode,
'status' => TRUE,
]);
}
return $entity_view_display;
}
/**
* {@inheritdoc}
*/
public function getFormDisplay($entity_type, $bundle, $form_mode = self::DEFAULT_DISPLAY_MODE) {
$storage = $this->entityTypeManager->getStorage('entity_form_display');
// Try loading the entity from configuration; if not found, create a fresh
// entity object. We do not preemptively create new entity form display
// configuration entries for each existing entity type and bundle whenever a
// new form mode becomes available. Instead, configuration entries are only
// created when an entity form display is explicitly configured and saved.
$entity_form_display = $storage->load($entity_type . '.' . $bundle . '.' . $form_mode);
if (!$entity_form_display) {
$entity_form_display = $storage->create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $form_mode,
'status' => TRUE,
]);
}
return $entity_form_display;
}
}
......@@ -7,6 +7,13 @@
*/
interface EntityDisplayRepositoryInterface {
/**
* The default display mode ID.
*
* @var string
*/
const DEFAULT_DISPLAY_MODE = 'default';
/**
* Gets the entity view mode info for all entity types.
*
......@@ -100,4 +107,86 @@ public function getFormModeOptionsByBundle($entity_type_id, $bundle);
*/
public function clearDisplayModeInfo();
/**
* Returns the entity view display associated with a bundle and view mode.
*
* Use this function when assigning suggested display options for a component
* in a given view mode. Note that they will only be actually used at render
* time if the view mode itself is configured to use dedicated display
* settings for the bundle; if not, the 'default' display is used instead.
*
* The function reads the entity view display from the current configuration,
* or returns a ready-to-use empty one if configuration entry exists yet for
* this bundle and view mode. This streamlines manipulation of display objects
* by always returning a consistent object that reflects the current state of
* the configuration.
*
* Example usage:
* - Set the 'body' field to be displayed and the 'field_image' field to be
* hidden on article nodes in the 'default' display.
* @code
* \Drupal::service('entity_display.repository')
* ->getViewDisplay('node', 'article', 'default')
* ->setComponent('body', array(
* 'type' => 'text_summary_or_trimmed',
* 'settings' => array('trim_length' => '200')
* 'weight' => 1,
* ))
* ->removeComponent('field_image')
* ->save();
* @endcode
*
* @param string $entity_type
* The entity type.
* @param string $bundle
* The bundle.
* @param string $view_mode
* (optional) The view mode. Defaults to self::DEFAULT_DISPLAY_MODE.
*
* @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface
* The entity view display associated with the view mode.
*/
public function getViewDisplay($entity_type, $bundle, $view_mode = self::DEFAULT_DISPLAY_MODE);
/**
* Returns the entity form display associated with a bundle and form mode.
*
* The function reads the entity form display object from the current
* configuration, or returns a ready-to-use empty one if no configuration
* entry exists yet for this bundle and form mode. This streamlines
* manipulation of entity form displays by always returning a consistent
* object that reflects the current state of the configuration.
*
* Example usage:
* - Set the 'body' field to be displayed with the
* 'text_textarea_with_summary' widget and the 'field_image' field to be
* hidden on article nodes in the 'default' form mode.
* @code
* \Drupal::service('entity_display.repository')
* ->getFormDisplay('node', 'article', 'default')
* ->setComponent('body', array(
* 'type' => 'text_textarea_with_summary',
* 'weight' => 1,
* ))
* ->setComponent('field_image', array(
* 'region' => 'hidden',
* ))
* ->save();
* @endcode
*
* @param string $entity_type
* The entity type.
* @param string $bundle
* The bundle.
* @param string $form_mode
* (optional) The form mode. Defaults to self::DEFAULT_DISPLAY_MODE.
*
* @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface
* The entity form display associated with the given form mode.
*
* @see \Drupal\Core\Entity\EntityStorageInterface::create()
* @see \Drupal\Core\Entity\EntityStorageInterface::load()
*/
public function getFormDisplay($entity_type, $bundle, $form_mode = self::DEFAULT_DISPLAY_MODE);
}
......@@ -851,4 +851,28 @@ public function getInstance(array $options) {
return $this->container->get('entity_type.manager')->getInstance($options);
}
/**
* {@inheritdoc}
*
* @deprecated in drupal:8.8.0, will be removed before drupal:9.0.0.
* Use \Drupal\Core\Entity\EntityTypeManagerInterface::getViewDisplay()
* instead.
*/
public function getViewDisplay($entity_type, $bundle, $view_mode = self::DEFAULT_DISPLAY_MODE) {
@trigger_error('EntityManager::getViewDisplay() is deprecated in drupal:8.8.0 and will be removed before Drupal 9.0.0. Use \Drupal::service(\'entity_display.repository\')->getViewDisplay() instead.', E_USER_DEPRECATED);
return $this->container->get('entity_display.repository')->getViewDisplay($entity_type, $bundle, $view_mode);
}
/**
* {@inheritdoc}
*
* @deprecated in drupal:8.8.0, will be removed before drupal:9.0.0.
* Use \Drupal\Core\Entity\EntityTypeManagerInterface::getFormwDisplay()
* instead.
*/
public function getFormDisplay($entity_type, $bundle, $form_mode = self::DEFAULT_DISPLAY_MODE) {
@trigger_error('EntityManager::getFormDisplay() is deprecated in drupal:8.8.0 and will be removed before Drupal 9.0.0. Use \Drupal::service(\'entity_display.repository\')->getFormDisplay() instead.', E_USER_DEPRECATED);
return $this->container->get('entity_display.repository')->getFormDisplay($entity_type, $bundle, $form_mode);
}
}
......@@ -351,7 +351,8 @@ protected function defaultValueWidget(FormStateInterface $form_state) {
// Use the widget currently configured for the 'default' form mode, or
// fallback to the default widget for the field type.
$entity_form_display = entity_get_form_display($entity->getEntityTypeId(), $entity->bundle(), 'default');
$entity_form_display = \Drupal::service('entity_display.repository')
->getFormDisplay($entity->getEntityTypeId(), $entity->bundle());
$widget = $entity_form_display->getRenderer($this->getFieldDefinition()->getName());
if (!$widget) {
$widget = \Drupal::service('plugin.manager.field.widget')->getInstance(['field_definition' => $this->getFieldDefinition()]);
......
......@@ -91,15 +91,18 @@ function block_content_add_body_field($block_type_id, $label = 'Body') {
]);
$field->save();
// Assign widget settings for the 'default' form mode.
entity_get_form_display('block_content', $block_type_id, 'default')
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
// Assign widget settings for the default form mode.
$display_repository->getFormDisplay('block_content', $block_type_id)
->setComponent('body', [
'type' => 'text_textarea_with_summary',
])
->save();
// Assign display settings for 'default' view mode.
entity_get_display('block_content', $block_type_id, 'default')
// Assign display settings for default view mode.
$display_repository->getViewDisplay('block_content', $block_type_id)
->setComponent('body', [
'label' => 'hidden',
'type' => 'text_default',
......
......@@ -7,7 +7,7 @@ services:
comment.manager:
class: Drupal\comment\CommentManager
arguments: ['@entity_type.manager', '@config.factory', '@string_translation', '@module_handler', '@current_user', '@entity_field.manager']
arguments: ['@entity_type.manager', '@config.factory', '@string_translation', '@module_handler', '@current_user', '@entity_field.manager', '@entity_display.repository']
comment.statistics:
class: Drupal\comment\CommentStatistics
......
......@@ -5,6 +5,7 @@
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
......@@ -38,6 +39,13 @@ class CommentManager implements CommentManagerInterface {
*/
protected $entityFieldManager;
/**
* The entity display repository.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
/**
* The entity type manager.
*
......@@ -82,14 +90,16 @@ class CommentManager implements CommentManagerInterface {
* The config factory.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager service.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, AccountInterface $current_user, EntityFieldManagerInterface $entity_field_manager = NULL) {
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, AccountInterface $current_user, EntityFieldManagerInterface $entity_field_manager = NULL, EntityDisplayRepositoryInterface $entity_display_repository = NULL) {
$this->entityTypeManager = $entity_type_manager;
$this->userConfig = $config_factory->get('user.settings');
$this->stringTranslation = $string_translation;
......@@ -100,6 +110,12 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager, Con
$entity_field_manager = \Drupal::service('entity_field.manager');
}
$this->entityFieldManager = $entity_field_manager;
if (!$entity_display_repository) {
@trigger_error('The entity_display.repository service must be passed to CommentManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2835616.', E_USER_DEPRECATED);
$entity_display_repository = \Drupal::service('entity_display.repository');
}
$this->entityDisplayRepository = $entity_display_repository;
}
/**
......@@ -129,15 +145,15 @@ public function addBodyField($comment_type_id) {
]);
$field->save();
// Assign widget settings for the 'default' form mode.
entity_get_form_display('comment', $comment_type_id, 'default')
// Assign widget settings for the default form mode.
$this->entityDisplayRepository->getFormDisplay('comment', $comment_type_id)
->setComponent('comment_body', [
'type' => 'text_textarea',
])
->save();
// Assign display settings for the 'default' view mode.
entity_get_display('comment', $comment_type_id, 'default')
// Assign display settings for the default view mode.
$this->entityDisplayRepository->getViewDisplay('comment', $comment_type_id)
->setComponent('comment_body', [
'label' => 'hidden',
'type' => 'text_default',
......
......@@ -131,7 +131,7 @@ public function postComment($entity, $comment, $subject = '', $contact = NULL, $
}
// Determine the visibility of subject form field.
if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) {
if (\Drupal::service('entity_display.repository')->getFormDisplay('comment', 'comment')->getComponent('subject')) {
// Subject input allowed.
$edit['subject[0][value]'] = $subject;
}
......@@ -234,7 +234,8 @@ public function deleteComment(CommentInterface $comment) {
* Boolean specifying whether the subject field should be enabled.
*/
public function setCommentSubject($enabled) {
$form_display = entity_get_form_display('comment', 'comment', 'default');
$form_display = \Drupal::service('entity_display.repository')
->getFormDisplay('comment', 'comment');
if ($enabled) {
$form_display->setComponent('subject', [
'type' => 'string_textfield',
......
......@@ -87,25 +87,25 @@ public function addDefaultCommentField($entity_type, $bundle, $field_name = 'com
],
])->save();
// Entity form displays: assign widget settings for the 'default' form
// Entity form displays: assign widget settings for the default form
// mode, and hide the field in all other form modes.
entity_get_form_display($entity_type, $bundle, 'default')
$entity_display_repository->getFormDisplay($entity_type, $bundle)
->setComponent($field_name, [
'type' => 'comment_default',
'weight' => 20,
])
->save();
foreach ($entity_display_repository->getFormModes($entity_type) as $id => $form_mode) {
$display = entity_get_form_display($entity_type, $bundle, $id);
$display = $entity_display_repository->getFormDisplay($entity_type, $bundle, $id);
// Only update existing displays.
if ($display && !$display->isNew()) {
$display->removeComponent($field_name)->save();
}
}
// Entity view displays: assign widget settings for the 'default' view
// Entity view displays: assign widget settings for the default view
// mode, and hide the field in all other view modes.
entity_get_display($entity_type, $bundle, 'default')
$entity_display_repository->getViewDisplay($entity_type, $bundle)
->setComponent($field_name, [
'label' => 'above',
'type' => 'comment_default',
......@@ -114,7 +114,7 @@ public function addDefaultCommentField($entity_type, $bundle, $field_name = 'com
])
->save();
foreach ($entity_display_repository->getViewModes($entity_type) as $id => $view_mode) {
$display = entity_get_display($entity_type, $bundle, $id);
$display = $entity_display_repository->getViewDisplay($entity_type, $bundle, $id);
// Only update existing displays.
if ($display && !$display->isNew()) {
$display->removeComponent($field_name)->save();
......
......@@ -101,8 +101,10 @@ public function testCommentLinks() {
$this->assertLink('Add new comment');
}
$display_repository = $this->container->get('entity_display.repository');
// Change weight to make links go before comment body.
entity_get_display('comment', 'comment', 'default')
$display_repository->getViewDisplay('comment', 'comment')
->setComponent('links', ['weight' => -100])
->save();
$this->drupalGet($this->node->toUrl());
......@@ -112,7 +114,7 @@ public function testCommentLinks() {
$this->assertIdentical($element->getTagName(), 'div', 'Last element is comment body.');
// Change weight to make links go after comment body.
entity_get_display('comment', 'comment', 'default')
$display_repository->getViewDisplay('comment', 'comment')
->setComponent('links', ['weight' => 100])
->save();
$this->drupalGet($this->node->toUrl());
......@@ -122,7 +124,7 @@ public function testCommentLinks() {
$this->assertNotEmpty($element->find('css', 'ul.links'), 'Last element is comment links.');
// Make sure we can hide node links.
entity_get_display('node', $this->node->bundle(), 'default')
$display_repository->getViewDisplay('node', $this->node->bundle())
->removeComponent('links')
->save();
$this->drupalGet($this->node->toUrl());
......@@ -135,7 +137,7 @@ public function testCommentLinks() {
$this->assertLink('Reply');
// Make sure we can hide comment links.
entity_get_display('comment', 'comment', 'default')
$display_repository->getViewDisplay('comment', 'comment')
->removeComponent('links')
->save();
$this->drupalGet('node/' . $this->node->id());
......
......@@ -122,7 +122,8 @@ public function postComment(EntityInterface $entity, $comment, $subject = '', $c
}
// Determine the visibility of subject form field.
if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) {
$display_repository = $this->container->get('entity_display.repository');
if ($display_repository->getFormDisplay('comment', 'comment')->getComponent('subject')) {
// Subject input allowed.
$edit['subject[0][value]'] = $subject;
}
......
......@@ -321,7 +321,8 @@ public function testTwoPagers() {
// Add another field to article content-type.
$this->addDefaultCommentField('node', 'article', 'comment_2');
// Set default to display comment list with unique pager id.
entity_get_display('node', 'article', 'default')
\Drupal::service('entity_display.repository')
->getViewDisplay('node', 'article')
->setComponent('comment_2', [
'label' => 'hidden',
'type' => 'comment_default',
......
......@@ -125,7 +125,8 @@ public function postComment($entity, $comment, $subject = '', $contact = NULL, $
}
// Determine the visibility of subject form field.
if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) {
$display_repository = $this->container->get('entity_display.repository');
if ($display_repository->getFormDisplay('comment', 'comment')->getComponent('subject')) {
// Subject input allowed.
$edit['subject[0][value]'] = $subject;
}
......@@ -228,7 +229,9 @@ public function deleteComment(CommentInterface $comment) {
* Boolean specifying whether the subject field should be enabled.
*/
public function setCommentSubject($enabled) {
$form_display = entity_get_form_display('comment', 'comment', 'default');
$form_display = $this->container->get('entity_display.repository')
->getFormDisplay('comment', 'comment');
if ($enabled) {
$form_display->setComponent('subject', [
'type' => 'string_textfield',
......
......@@ -3,6 +3,7 @@
namespace Drupal\Tests\comment\Unit;
use Drupal\comment\CommentManager;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
......@@ -54,7 +55,8 @@ public function testGetFields() {
$this->getMock('Drupal\Core\StringTranslation\TranslationInterface'),
$this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'),
$this->createMock(AccountInterface::class),
$entity_field_manager
$entity_field_manager,
$this->prophesize(EntityDisplayRepositoryInterface::class)->reveal()
);
$comment_fields = $comment_manager->getFields('node');
$this->assertArrayHasKey('field_foobar', $comment_fields);
......
......@@ -112,20 +112,21 @@ public function testExportImport() {
'field_storage' => $this->fieldStorage,
'bundle' => $this->contentType->id(),
])->save();
$display_repository = $this->container->get('entity_display.repository');
// Update the displays so that configuration does not change unexpectedly on
// import.
entity_get_form_display('node', $this->contentType->id(), 'default')
$display_repository->getFormDisplay('node', $this->contentType->id(), 'default')
->setComponent($this->fieldName, [
'type' => 'text_textfield',
])
->save();
entity_get_display('node', $this->contentType->id(), 'full')
$display_repository->getViewDisplay('node', $this->contentType->id(), 'full')