diff -u b/core/modules/entity/lib/Drupal/entity/EntityDisplayBase.php b/core/modules/entity/lib/Drupal/entity/EntityDisplayBase.php --- b/core/modules/entity/lib/Drupal/entity/EntityDisplayBase.php +++ b/core/modules/entity/lib/Drupal/entity/EntityDisplayBase.php @@ -48,15 +48,11 @@ public $bundle; /** - * A partial entity, created via _field_create_entity_from_ids() from - * $targetEntityType and $bundle. + * A list of field definitions eligible for configuration in this display. * - * @var \Drupal\Core\Entity\EntityInterface - * - * @todo Remove when getFieldDefinition() is fixed to not need it. - * https://drupal.org/node/2114707 + * @var \Drupal\Core\Field\FieldDefinitionInterface][ */ - private $targetEntity; + protected $fieldDefinitions; /** * View or form mode to be displayed. @@ -306,40 +302,37 @@ * Returns the field definition of a field. */ protected function getFieldDefinition($field_name) { - // @todo Replace this entire implementation with - // \Drupal::entityManager()->getFieldDefinition() when it can hand the - // $instance objects - https://drupal.org/node/2114707 - if (!isset($this->targetEntity)) { - $this->targetEntity = _field_create_entity_from_ids((object) array('entity_type' => $this->targetEntityType, 'bundle' => $this->bundle, 'entity_id' => NULL)); - } - if (($this->targetEntity instanceof ContentEntityInterface) && $this->targetEntity->hasField($field_name)) { - return $this->targetEntity->get($field_name)->getFieldDefinition(); - } + $definitions = $this->getFieldDefinitions(); + return isset($definitions[$field_name]) ? $definitions[$field_name] : NULL; } /** * Returns the definitions of the fields that are candidate for display. */ protected function getFieldDefinitions() { - // @todo Replace this entire implementation with - // \Drupal::entityManager()->getFieldDefinition() when it can hand the - // $instance objects - https://drupal.org/node/2114707 - if (!isset($this->targetEntity)) { - $this->targetEntity = _field_create_entity_from_ids((object) array('entity_type' => $this->targetEntityType, 'bundle' => $this->bundle, 'entity_id' => NULL)); - } - $field_definitions = array(); - if ($this->targetEntity instanceof ContentEntityInterface) { - foreach ($this->targetEntity as $field_name => $items) { - $field_definitions[$field_name] = $items->getFieldDefinition(); + if (!isset($this->fieldDefinitions)) { + // @todo Replace this with \Drupal::entityManager()->getFieldDefinition() + // when it can hand the $instance objects. + // https://drupal.org/node/2114707 + $entity_manager = \Drupal::entityManager(); + $entity_info = $entity_manager->getDefinition($this->targetEntityType); + $field_definitions = array(); + if (is_subclass_of($entity_info, '\Drupal\Core\Entity\ContentEntityInterface')) { + $entity = _field_create_entity_from_ids((object) array('entity_type' => $this->targetEntityType, 'bundle' => $this->bundle, 'entity_id' => NULL)); + foreach ($entity as $field_name => $items) { + $field_definitions[$field_name] = $items->getFieldDefinition(); + } } + + $context = $this->displayContext; + $this->fieldDefinitions = array_filter($field_definitions, function(FieldDefinitionInterface $field_definition) use ($context) { + // @todo getDisplayOptions() is not part of FieldDefinitionInterface, we + // should check for instanceof FieldDefinition + return $field_definition->isFieldConfigurable() || $field_definition->getDisplayOptions($context); + }); } - $context = $this->displayContext; - return array_filter($field_definitions, function(FieldDefinitionInterface $field_definition) use ($context) { - // @todo getDisplayOptions() is not part of FieldDefinitionInterface, we - // should check for instanceof FieldDefinition - return $field_definition->isFieldConfigurable() || $field_definition->getDisplayOptions($context); - }); + return $this->fieldDefinitions; } } diff -u b/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverviewBase.php b/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverviewBase.php --- b/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverviewBase.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverviewBase.php @@ -661,7 +661,7 @@ * An array of extra field info, as provided by field_info_extra_fields(). */ protected function getExtraFields() { - return field_info_extra_fields($this->entity_type, $this->bundle, $this->displayContext); + return field_info_extra_fields($this->entity_type, $this->bundle, ($this->displayContext == 'view' ? 'display' : $this->displayContext)); } /** only in patch2: unchanged: --- a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php +++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php @@ -109,9 +109,10 @@ public function testEntityGetDisplay() { * Tests the behavior of a field component within an EntityDisplay object. */ public function testExtraFieldComponent() { + entity_test_create_bundle('bundle_with_extra_fields'); $display = entity_create('entity_display', array( 'targetEntityType' => 'entity_test', - 'bundle' => 'entity_test', + 'bundle' => 'bundle_with_extra_fields', 'mode' => 'default', )); @@ -133,12 +134,6 @@ public function testExtraFieldComponent() { public function testFieldComponent() { $this->enableModules(array('field_test')); - $display = entity_create('entity_display', array( - 'targetEntityType' => 'entity_test', - 'bundle' => 'entity_test', - 'mode' => 'default', - )); - $field_name = 'test_field'; // Create a field and an instance. $field = entity_create('field_entity', array( @@ -154,6 +149,12 @@ public function testFieldComponent() { )); $instance->save(); + $display = entity_create('entity_display', array( + 'targetEntityType' => 'entity_test', + 'bundle' => 'entity_test', + 'mode' => 'default', + )); + // Check that providing no options results in default values being used. $display->setComponent($field_name); $field_type_info = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field->type); only in patch2: unchanged: --- a/core/modules/entity/lib/Drupal/entity/Tests/EntityFormDisplayTest.php +++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityFormDisplayTest.php @@ -55,27 +55,27 @@ public function testEntityGetFromDisplay() { public function testFieldComponent() { $this->enableModules(array('field_test')); + // Create a field and an instance. + $field_name = 'test_field'; + $field = entity_create('field_entity', array( + 'name' => $field_name, + 'entity_type' => 'entity_test', + 'type' => 'test_field' + )); + $field->save(); + $instance = entity_create('field_instance', array( + 'field_name' => $field_name, + 'entity_type' => 'entity_test', + 'bundle' => 'entity_test', + )); + $instance->save(); + $form_display = entity_create('entity_form_display', array( 'targetEntityType' => 'entity_test', 'bundle' => 'entity_test', 'mode' => 'default', )); - // Create a field and an instance. - $field_name = 'test_field'; - $field = entity_create('field_entity', array( - 'name' => $field_name, - 'entity_type' => 'entity_test', - 'type' => 'test_field' - )); - $field->save(); - $instance = entity_create('field_instance', array( - 'field_name' => $field_name, - 'entity_type' => 'entity_test', - 'bundle' => 'entity_test', - )); - $instance->save(); - // Check that providing no options results in default values being used. $form_display->setComponent($field_name); $field_type_info = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field->type); only in patch2: unchanged: --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -195,7 +195,7 @@ function entity_test_entity_form_mode_info_alter(&$form_modes) { * Implements hook_field_extra_fields(). */ function entity_test_field_extra_fields() { - $extra['entity_test']['entity_test'] = array( + $extra['entity_test']['bundle_with_extra_fields'] = array( 'display' => array( // Note: those extra fields do not currently display anything, they are // just used in \Drupal\entity\Tests\EntityDisplayTest to test the