diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFieldFormatter.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFieldFormatter.php index 0e43086..92bf745 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFieldFormatter.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFieldFormatter.php @@ -3,7 +3,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldFormatter; use Drupal\Core\Entity\EntityDisplayBase; -use Drupal\Core\Entity\EntityFieldManager; +use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\BaseFieldDefinition; @@ -60,14 +60,18 @@ class EntityReferenceFieldFormatter extends EntityReferenceFormatterBase impleme /** * The list of supported fields, prepared as options (machine_name => label). * - * @var array + * @var string[] */ protected $availableFieldOptions; /** * The list of available formatters, keyed by field type. * - * @var array + * Each element of this array is an associative array, where keys are + * formatter machine names, and the values are formatter human-readable + * labels. + * + * @var array[] */ protected $availableFormatterOptions; @@ -94,7 +98,7 @@ public static function create(ContainerInterface $container, array $configuratio * Constructs a EntityReferenceReferencedEntityFieldFormatter object. * * @param string $plugin_id - * The plugin_id for the formatter. + * The formatter's plugin ID. * @param mixed $plugin_definition * The plugin implementation definition. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition @@ -107,7 +111,7 @@ public static function create(ContainerInterface $container, array $configuratio * The view mode. * @param array $third_party_settings * Any third party settings. - * @param \Drupal\Core\Entity\EntityFieldManager $entity_field_manager + * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager * The entity field manager. * @param \Drupal\Core\Field\FormatterPluginManager $formatter_plugin_manager * The formatter plugin manager. @@ -116,7 +120,7 @@ public static function create(ContainerInterface $container, array $configuratio * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info * The entity type bundle info. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityFieldManager $entity_field_manager, FormatterPluginManager $formatter_plugin_manager, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) { + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityFieldManagerInterface $entity_field_manager, FormatterPluginManager $formatter_plugin_manager, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); $this->entityFieldManager = $entity_field_manager; $this->formatterPluginManager = $formatter_plugin_manager; @@ -193,7 +197,6 @@ protected function getAvailableFieldOptions() { foreach ($target_bundles as $bundle) { // Fetch all fields present on each target bundle. $bundle_field_names = []; - /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */ foreach ($this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle) as $field_machine_name => $field_definition) { // We use ::isDisplayConfigurable() to filter out the base fields that // should not be exposed to the user. The only exception to this is the @@ -243,14 +246,14 @@ protected function getAvailableFormatterOptions(FieldStorageDefinitionInterface /** * Ajax callback for field name change. */ - public static function onFieldNameChange(array $form, FormStateInterface $form_state) { + public static function onFieldNameChange(array &$form, FormStateInterface $form_state) { return $form['fields'][$form_state->get('plugin_settings_edit')]['plugin']['settings_edit_form']['settings']; } /** * Ajax callback for formatter type change. */ - public static function onFormatterTypeChange(array $form, FormStateInterface $form_state) { + public static function onFormatterTypeChange(array &$form, FormStateInterface $form_state) { return $form['fields'][$form_state->get('plugin_settings_edit')]['plugin']['settings_edit_form']['settings']['settings']; } @@ -330,7 +333,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) { $settings_form = $formatter->settingsForm([], $form_state); } else { - drupal_set_message('Could not instantiate the formatter plugin, please choose a different formatter.', 'error'); + drupal_set_message($this->t('Could not instantiate the formatter plugin, please choose a different formatter.'), 'error'); } $form['settings'] = $settings_form; $form['settings']['#prefix'] = '
'; @@ -348,7 +351,7 @@ public function settingsSummary() { $field_name = $this->getFieldName(); $formatter_type = $this->getFormatterType($field_name['machine_name']); - $summary[] = $this->t('Displaying the field "%field_name", using the formatter "%type".', [ + $summary[] = $this->t('Displaying the field %field_name, using the formatter %type.', [ '%field_name' => $field_name['label'], '%type' => $formatter_type['label'], ]); @@ -369,20 +372,18 @@ public function settingsSummary() { * The value of the setting, or NULL if absent. */ public function getSetting($key, FormStateInterface $form_state = NULL) { - if (!$form_state) { - return parent::getSetting($key); - } - - $field_name = $this->fieldDefinition->getName(); - $form_state_key = [ - 'fields', - $field_name, - 'settings_edit_form', - 'settings', - $key, - ]; - if ($form_state->hasValue($form_state_key)) { - return $form_state->getValue($form_state_key); + if ($form_state) { + $field_name = $this->fieldDefinition->getName(); + $form_state_key = [ + 'fields', + $field_name, + 'settings_edit_form', + 'settings', + $key, + ]; + if ($form_state->hasValue($form_state_key)) { + return $form_state->getValue($form_state_key); + } } return parent::getSetting($key); diff --git a/core/tests/Drupal/FunctionalJavascriptTests/EntityReference/EntityReferenceFieldFormatterTest.php b/core/tests/Drupal/FunctionalJavascriptTests/EntityReference/EntityReferenceFieldFormatterTest.php index 16058db..22c3713 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/EntityReference/EntityReferenceFieldFormatterTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/EntityReference/EntityReferenceFieldFormatterTest.php @@ -9,7 +9,7 @@ use Drupal\simpletest\NodeCreationTrait; /** - * Tests the output of entity reference autocomplete widgets. + * Tests the "entity_reference_field" formatter. * * @group entity_reference */ @@ -85,7 +85,7 @@ protected function setUp() { } /** - * Tests the Referenced Entity Field formatter configuration. + * Tests the formatter's configuration and output. */ public function testEntityReferenceFieldFormatter() { $page = $this->getSession()->getPage(); @@ -96,7 +96,7 @@ public function testEntityReferenceFieldFormatter() { $page->selectFieldOption('fields[field_related_content][type]', 'entity_reference_field'); $result = $assert_session->waitForElementVisible('css', '.field-plugin-summary-cell .ajax-new-content'); $this->assertNotEmpty($result); - $assert_session->pageTextContains('Displaying the field "Title", using the formatter "Plain text".'); + $assert_session->pageTextContains('Displaying the field Title, using the formatter Plain text.'); // Save without choosing anything and verify that the child title is shown // by default. @@ -132,7 +132,7 @@ public function testEntityReferenceFieldFormatter() { $page->pressButton('Update'); $result = $assert_session->waitForElementVisible('css', '.field-plugin-summary-cell .ajax-new-content'); $this->assertNotEmpty($result); - $assert_session->pageTextContains('Displaying the field "Body", using the formatter "Default".'); + $assert_session->pageTextContains('Displaying the field Body, using the formatter Default.'); $page->pressButton('Save'); $this->drupalGet('/node/' . $this->testParentNode->id());