diff --git a/core/lib/Drupal/Core/Field/PluginSettingsBase.php b/core/lib/Drupal/Core/Field/PluginSettingsBase.php index d57434f..b5aeff7 100644 --- a/core/lib/Drupal/Core/Field/PluginSettingsBase.php +++ b/core/lib/Drupal/Core/Field/PluginSettingsBase.php @@ -52,8 +52,8 @@ public function getSettings() { * {@inheritdoc} */ public function getSetting($key) { - // Merge defaults if that has not been done before. - if (!$this->defaultSettingsMerged) { + // Merge defaults if we have no value for the key. + if (!$this->defaultSettingsMerged && !array_key_exists($key, $this->settings)) { $this->mergeDefaults(); } return isset($this->settings[$key]) ? $this->settings[$key] : NULL; @@ -64,7 +64,7 @@ public function getSetting($key) { */ protected function mergeDefaults() { // Use only settings defined by plugin. - $this->settings = array_intersect_key($this->settings, static::defaultSettings()) + static::defaultSettings(); + $this->settings += static::defaultSettings(); $this->defaultSettingsMerged = TRUE; } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/ConfigurableEntityReferenceItem.php b/core/modules/entity_reference/lib/Drupal/entity_reference/ConfigurableEntityReferenceItem.php index 8c43040..91092f3 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/ConfigurableEntityReferenceItem.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/ConfigurableEntityReferenceItem.php @@ -33,7 +33,8 @@ class ConfigurableEntityReferenceItem extends EntityReferenceItem implements All */ public static function defaultSettings() { $settings = parent::defaultSettings(); - // The instance settings 'handler_settings.target_bundles' used for this. + // The target bundle is handled by the 'target_bundles' property in the + // 'handler_settings' instance setting. unset($settings['target_bundle']); return $settings; } diff --git a/core/modules/field/lib/Drupal/field/Entity/FieldConfig.php b/core/modules/field/lib/Drupal/field/Entity/FieldConfig.php index eed3000..d3b74cc 100644 --- a/core/modules/field/lib/Drupal/field/Entity/FieldConfig.php +++ b/core/modules/field/lib/Drupal/field/Entity/FieldConfig.php @@ -295,6 +295,7 @@ public function preSave(EntityStorageControllerInterface $storage_controller) { */ protected function preSaveNew(EntityStorageControllerInterface $storage_controller) { $entity_manager = \Drupal::entityManager(); + $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); // Assign the ID. $this->id = $this->id(); @@ -318,7 +319,7 @@ protected function preSaveNew(EntityStorageControllerInterface $storage_controll } // Check that the field type is known. - $field_type = \Drupal::service('plugin.manager.field.field_type')->getDefinition($this->type); + $field_type = $field_type_manager->getDefinition($this->type); if (!$field_type) { throw new FieldException(format_string('Attempt to create a field of unknown type %type.', array('%type' => $this->type))); } @@ -326,9 +327,7 @@ protected function preSaveNew(EntityStorageControllerInterface $storage_controll // Make sure all settings are present, so that a complete field // definition is passed to the various hooks and written to config. - if (isset($field_type['class'])) { - $this->settings += $field_type['class']::defaultSettings(); - } + $this->settings += $field_type_manager->getDefaultSettings($this->type); // Notify the entity storage controller. $entity_manager->getStorageController($this->entity_type)->onFieldCreate($this); @@ -526,14 +525,10 @@ public function getSettings() { // some CPU and memory profiling to see if it's worth statically caching // $field_type_info, or the default field and instance settings, within // $this. - $field_type_info = \Drupal::service('plugin.manager.field.field_type')->getDefinition($this->type); + $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); - $settings = array(); - $instance_settings = array(); - if (isset($field_type_info['class'])) { - $settings = $field_type_info['class']::defaultSettings(); - $instance_settings = $field_type_info['class']::defaultInstanceSettings(); - } + $settings = $field_type_manager->getDefaultSettings($this->type); + $instance_settings = $field_type_manager->getDefaultInstanceSettings($this->type); return $this->settings + $settings + $instance_settings; } @@ -542,8 +537,8 @@ public function getSettings() { */ public function getSetting($setting_name) { // @todo See getSettings() about potentially statically caching this. - // We assume here that consecutive array_key_exists() is more efficient than - // calling getSettings() when all we need is a single setting. + // We assume here that one call to array_key_exists() is more efficient + // than calling getSettings() when all we need is a single setting. if (array_key_exists($setting_name, $this->settings)) { return $this->settings[$setting_name]; } diff --git a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php index 257d20b..36a2770 100644 --- a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php @@ -63,8 +63,8 @@ function testCreateField() { $this->assertEqual($field_config['cardinality'], 1, 'Cardinality defaults to 1.'); // Ensure that default settings are present. - $field_type = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field_definition['type']); - $this->assertEqual($field_config['settings'], $field_type['class']::defaultSettings(), 'Default field settings have been written.'); + $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); + $this->assertEqual($field_config['settings'], $field_type_manager->getDefaultSettings($field_definition['type']), 'Default field settings have been written.'); // Guarantee that the name is unique. try { diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php index 20ec766..3f2e116 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php @@ -133,8 +133,8 @@ function testFieldPrepare() { $field = field_info_field('entity_test', $field_definition['name']); // Check that all expected settings are in place. - $field_type = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field_definition['type']); - $this->assertEqual($field->settings, $field_type['class']::defaultSettings(), 'All expected default field settings are present.'); + $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); + $this->assertEqual($field->settings, $field_type_manager->getDefaultSettings($field_definition['type']), 'All expected default field settings are present.'); } /** @@ -167,8 +167,8 @@ function testInstancePrepare() { $instance = field_info_instance($instance_definition['entity_type'], $instance_definition['field_name'], $instance_definition['bundle']); // Check that all expected instance settings are in place. - $field_type = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field_definition['type']); - $this->assertEqual($instance->settings, $field_type['class']::defaultInstanceSettings() , 'All expected instance settings are present.'); + $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); + $this->assertEqual($instance->settings, $field_type_manager->getDefaultInstanceSettings($field_definition['type']) , 'All expected instance settings are present.'); } /** diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php index 9f5ff65..19851fb 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php @@ -75,8 +75,7 @@ function testCreateFieldInstance() { // the loaded ConfigEntity, to be sure we check that the defaults are // applied on write. $config = \Drupal::config('field.instance.' . $instance->id())->get(); - - $field_type = \Drupal::service('plugin.manager.field.field_type')->getDefinition($this->field_definition['type']); + $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); // Check that default values are set. $this->assertEqual($config['required'], FALSE, 'Required defaults to false.'); @@ -84,7 +83,7 @@ function testCreateFieldInstance() { $this->assertIdentical($config['description'], '', 'Description defaults to empty string.'); // Check that default settings are set. - $this->assertEqual($config['settings'], $field_type['class']::defaultInstanceSettings() , 'Default instance settings have been written.'); + $this->assertEqual($config['settings'], $field_type_manager->getDefaultInstanceSettings($this->field_definition['type']) , 'Default instance settings have been written.'); // Guarantee that the field/bundle combination is unique. try { diff --git a/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverviewBase.php b/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverviewBase.php index b0e66e3..2deca15 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverviewBase.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverviewBase.php @@ -399,7 +399,7 @@ protected function buildFieldRow(FieldDefinitionInterface $field_definition, Ent } // Check selected plugin settings to display edit link or not. - if ($plugin::defaultSettings()) { + if ($this->pluginManager->getDefaultSettings($display_options['type'])) { $field_row['settings_edit'] = $base_button + array( '#type' => 'image_button', '#name' => $field_name . '_settings_edit', diff --git a/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageDisplayTest.php b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageDisplayTest.php index 5699ed9..1b8c16d 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageDisplayTest.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageDisplayTest.php @@ -225,8 +225,9 @@ function testViewModeCustom() { $node = $this->drupalCreateNode($settings); // Gather expected output values with the various formatters. - $field_test_default_settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings('field_test_default'); - $field_test_with_prepare_view_settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings('field_test_with_prepare_view'); + $formatter_plugin_manager = \Drupal::service('plugin.manager.field.formatter'); + $field_test_default_settings = $formatter_plugin_manager->getDefaultSettings('field_test_default'); + $field_test_with_prepare_view_settings = $formatter_plugin_manager->getDefaultSettings('field_test_with_prepare_view'); $output = array( 'field_test_default' => $field_test_default_settings['test_formatter_setting'] . '|' . $value, 'field_test_with_prepare_view' => $field_test_with_prepare_view_settings['test_formatter_setting_additional'] . '|' . $value. '|' . ($value + 1),