Commit 7f616204 authored by alexpott's avatar alexpott

Issue #2136197 by andypost, jessebeach, swentel, longwave, Berdir: Move...

Issue #2136197 by andypost, jessebeach, swentel, longwave, Berdir: Move field/instance/widget/formatter settings out of annotation / plugin definition.
parent ed5d0556
......@@ -66,14 +66,6 @@ class FieldFormatter extends Plugin {
*/
public $field_types = array();
/**
* An array whose keys are the names of the settings available to the
* formatter type, and whose values are the default values for those settings.
*
* @var array
*/
public $settings = array();
/**
* An integer to determine the weight of this formatter relative to other
* formatter in the Field UI when selecting a formatter for a given field
......
......@@ -51,33 +51,6 @@ class FieldType extends DataType {
*/
public $description;
/**
* An array of field-level settings available for the field type.
*
* Keys are the names of the settings, and values are the default values for
* those settings.
*
* @var array
*/
public $settings;
/**
* An array of instance-level settings available for the field type.
*
* Keys are the names of the settings, and values are the default values for
* those settings.
*
* Instance-level settings can have different values on each field instance,
* and thus allow greater flexibility than field-level settings. It is
* recommended to put settings at the instance level whenever possible.
* Notable exceptions: settings acting on the storage schema, or settings that
* Views needs to use across field instances (for example, settings defining
* the list of allowed values for the field).
*
* @var array
*/
public $instance_settings;
/**
* The plugin_id of the default widget for this field type.
*
......
......@@ -65,14 +65,6 @@ class FieldWidget extends Plugin {
*/
public $field_types = array();
/**
* An array whose keys are the names of the settings available to the widget
* type, and whose values are the default values for those settings.
*
* @var array
*/
public $settings = array();
/**
* Does the field widget handles multiple values at once.
*
......
......@@ -23,6 +23,20 @@
*/
abstract class FieldItemBase extends Map implements FieldItemInterface {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array();
}
/**
* {@inheritdoc}
*/
public static function defaultInstanceSettings() {
return array();
}
/**
* {@inheritdoc}
*/
......
......@@ -212,6 +212,22 @@ public function delete();
*/
public function deleteRevision();
/**
* Defines the field-level settings for this plugin.
*
* @return array
* A list of default settings, keyed by the setting name.
*/
public static function defaultSettings();
/**
* Defines the instance-level settings for this plugin.
*
* @return array
* A list of default settings, keyed by the setting name.
*/
public static function defaultInstanceSettings();
/**
* Returns a form for the field-level settings.
*
......
......@@ -7,6 +7,7 @@
namespace Drupal\Core\Field;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManager;
......@@ -17,14 +18,6 @@
*/
class FieldTypePluginManager extends DefaultPluginManager implements FieldTypePluginManagerInterface {
/**
* {@inheritdoc}
*/
protected $defaults = array(
'settings' => array(),
'instance_settings' => array(),
);
/**
* Constructs the FieldTypePluginManager object
*
......@@ -58,16 +51,24 @@ public function processDefinition(&$definition, $plugin_id) {
* {@inheritdoc}
*/
public function getDefaultSettings($type) {
$info = $this->getDefinition($type);
return isset($info['settings']) ? $info['settings'] : array();
$plugin_definition = $this->getDefinition($type);
if (!empty($plugin_definition['class'])) {
$plugin_class = DefaultFactory::getPluginClass($type, $plugin_definition);
return $plugin_class::defaultSettings();
}
return array();
}
/**
* {@inheritdoc}
*/
public function getDefaultInstanceSettings($type) {
$info = $this->getDefinition($type);
return isset($info['instance_settings']) ? $info['instance_settings'] : array();
$plugin_definition = $this->getDefinition($type);
if (!empty($plugin_definition['class'])) {
$plugin_class = DefaultFactory::getPluginClass($type, $plugin_definition);
return $plugin_class::defaultInstanceSettings();
}
return array();
}
/**
......
......@@ -201,8 +201,12 @@ public function getOptions($field_type = NULL) {
* definition, or an empty array if type or settings are undefined.
*/
public function getDefaultSettings($type) {
$info = $this->getDefinition($type);
return isset($info['settings']) ? $info['settings'] : array();
$plugin_definition = $this->getDefinition($type);
if (!empty($plugin_definition['class'])) {
$plugin_class = DefaultFactory::getPluginClass($type, $plugin_definition);
return $plugin_class::defaultSettings();
}
return array();
}
}
......@@ -20,17 +20,23 @@
* field_types = {
* "decimal",
* "float"
* },
* settings = {
* "thousand_separator" = "",
* "decimal_separator" = ".",
* "scale" = "2",
* "prefix_suffix" = "TRUE"
* }
* )
*/
class DecimalFormatter extends NumericFormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'thousand_separator' => '',
'decimal_separator' => '.',
'scale' => 2,
'prefix_suffix' => TRUE,
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
......
......@@ -19,15 +19,21 @@
* label = @Translation("Default"),
* field_types = {
* "integer"
* },
* settings = {
* "thousand_separator" = "",
* "prefix_suffix" = "TRUE"
* }
* )
*/
class IntegerFormatter extends NumericFormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'thousand_separator' => '',
'prefix_suffix' => TRUE,
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
......
......@@ -17,22 +17,22 @@
* id = "decimal",
* label = @Translation("Number (decimal)"),
* description = @Translation("This field stores a number in the database in a fixed decimal format."),
* settings = {
* "precision" = "10",
* "scale" = "2"
* },
* instance_settings = {
* "min" = "",
* "max" = "",
* "prefix" = "",
* "suffix" = ""
* },
* default_widget = "number",
* default_formatter = "number_decimal"
* )
*/
class DecimalItem extends NumericItemBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'precision' => 10,
'scale' => 2,
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
......
......@@ -32,6 +32,25 @@
*/
class EntityReferenceItem extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'target_type' => \Drupal::moduleHandler()->moduleExists('node') ? 'node' : 'user',
'target_bundle' => NULL,
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public static function defaultInstanceSettings() {
return array(
'handler' => 'default',
) + parent::defaultInstanceSettings();
}
/**
* {@inheritdoc}
*/
......
......@@ -17,12 +17,6 @@
* id = "float",
* label = @Translation("Number (float)"),
* description = @Translation("This field stores a number in the database in a floating point format."),
* instance_settings = {
* "min" = "",
* "max" = "",
* "prefix" = "",
* "suffix" = ""
* },
* default_widget = "number",
* default_formatter = "number_decimal"
* )
......
......@@ -17,12 +17,6 @@
* id = "integer",
* label = @Translation("Number (integer)"),
* description = @Translation("This field stores a number in the database as an integer."),
* instance_settings = {
* "min" = "",
* "max" = "",
* "prefix" = "",
* "suffix" = ""
* },
* default_widget = "number",
* default_formatter = "number_integer"
* )
......
......@@ -14,6 +14,18 @@
*/
abstract class NumericItemBase extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function defaultInstanceSettings() {
$settings = parent::defaultInstanceSettings();
$settings['min'] = '';
$settings['max'] = '';
$settings['prefix'] = '';
$settings['suffix'] = '';
return $settings;
}
/**
* {@inheritdoc}
*/
......
......@@ -18,9 +18,6 @@
* id = "string",
* label = @Translation("String"),
* description = @Translation("An entity field containing a string value."),
* settings = {
* "max_length" = "255"
* },
* no_ui = TRUE,
* default_widget = "string",
* default_formatter = "string"
......@@ -28,6 +25,15 @@
*/
class StringItem extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'max_length' => 255,
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
......
......@@ -21,14 +21,20 @@
* id = "uri",
* label = @Translation("URI"),
* description = @Translation("An entity field containing a URI."),
* settings = {
* "max_length" = "2048"
* },
* no_ui = TRUE
* )
*/
class UriItem extends StringItem {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'max_length' => 2048,
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
......
......@@ -16,14 +16,20 @@
* id = "uuid",
* label = @Translation("UUID"),
* description = @Translation("An entity field containing a UUID."),
* settings = {
* "max_length" = "128"
* },
* no_ui = TRUE
* )
*/
class UuidItem extends StringItem {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'max_length' => 128,
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
......
......@@ -18,14 +18,20 @@
* label = @Translation("E-mail"),
* field_types = {
* "email"
* },
* settings = {
* "placeholder" = ""
* }
* )
*/
class EmailDefaultWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'placeholder' => '',
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
......
......@@ -21,14 +21,20 @@
* "integer",
* "decimal",
* "float"
* },
* settings = {
* "placeholder" = ""
* }
* )
*/
class NumberWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'placeholder' => '',
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
......
......@@ -18,15 +18,21 @@
* label = @Translation("String field"),
* field_types = {
* "string"
* },
* settings = {
* "size" = "60",
* "placeholder" = ""
* }
* )
*/
class StringWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'size' => 60,
'placeholder' => '',
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
......
......@@ -31,7 +31,14 @@ abstract class PluginSettingsBase extends PluginBase implements PluginSettingsIn
protected $defaultSettingsMerged = FALSE;
/**
* Implements Drupal\field\Plugin\PluginSettingsInterface::getSettings().
* {@inheritdoc}
*/
public static function defaultSettings() {
return array();
}
/**
* {@inheritdoc}
*/
public function getSettings() {
// Merge defaults before returning the array.
......@@ -42,7 +49,7 @@ public function getSettings() {
}
/**
* Implements Drupal\field\Plugin\PluginSettingsInterface::getSetting().
* {@inheritdoc}
*/
public function getSetting($key) {
// Merge defaults if we have no value for the key.
......@@ -56,20 +63,12 @@ public function getSetting($key) {
* Merges default settings values into $settings.
*/
protected function mergeDefaults() {
$this->settings += $this->getDefaultSettings();
$this->settings += static::defaultSettings();
$this->defaultSettingsMerged = TRUE;
}
/**
* Implements Drupal\field\Plugin\PluginSettingsInterface::getDefaultSettings().
*/
public function getDefaultSettings() {
$definition = $this->getPluginDefinition();
return $definition['settings'];
}
/**
* Implements Drupal\field\Plugin\PluginSettingsInterface::setSettings().
* {@inheritdoc}
*/
public function setSettings(array $settings) {
$this->settings = $settings;
......@@ -78,7 +77,7 @@ public function setSettings(array $settings) {
}
/**
* Implements Drupal\field\Plugin\PluginSettingsInterface::setSetting().
* {@inheritdoc}
*/
public function setSetting($key, $value) {
$this->settings[$key] = $value;
......
......@@ -14,6 +14,14 @@
*/
interface PluginSettingsInterface extends PluginInspectionInterface {
/**
* Defines the default settings for this plugin.
*
* @return array
* A list of default settings, keyed by the setting name.
*/
public static function defaultSettings();
/**
* Returns the array of settings, including defaults for missing settings.
*
......@@ -33,14 +41,6 @@ public function getSettings();
*/
public function getSetting($key);
/**
* Returns the default settings for the plugin.
*
* @return array
* The array of default setting values, keyed by setting names.
*/
public function getDefaultSettings();
/**
* Sets the settings for the plugin.
*
......@@ -48,8 +48,7 @@ public function getDefaultSettings();
* The array of settings, keyed by setting names. Missing settings will be
* assigned their default values.
*
* @return \Drupal\field\Plugin\PluginSettingsInterface
* The plugin itself.
* @return $this
*/
public function setSettings(array $settings);
......@@ -61,8 +60,7 @@ public function setSettings(array $settings);
* @param mixed $value
* The setting value.
*
* @return \Drupal\field\Plugin\PluginSettingsInterface
* The plugin itself.
* @return $this
*/
public function setSetting($key, $value);
......
......@@ -203,8 +203,13 @@ public function getOptions($field_type = NULL) {
* definition, or an empty array if type or settings are undefined.
*/
public function getDefaultSettings($type) {
$info = $this->getDefinition($type);
return isset($info['settings']) ? $info['settings'] : array();
$plugin_definition = $this->getDefinition($type);
if (!empty($plugin_definition['class'])) {
$plugin_class = DefaultFactory::getPluginClass($type, $plugin_definition);
return $plugin_class::defaultSettings();
}
return array();
}
}
......@@ -65,7 +65,7 @@ protected function defineOptions() {
*
* @return array
* An array of block-specific settings to override the defaults provided in
* \Drupal\views\Plugin\Block\ViewsBlock::settings().
* \Drupal\views\Plugin\Block\ViewsBlock::defaultConfiguration().
*
* @see \Drupal\views\Plugin\Block\ViewsBlock::defaultConfiguration()
*/
......
......@@ -29,14 +29,20 @@
* },
* edit = {
* "editor" = "disabled"
* },
* settings = {
* "pager_id" = 0
* }
* )
*/
class CommentDefaultFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'pager_id' => 0,