diff --git a/core/lib/Drupal/Core/Field/Annotation/FieldType.php b/core/lib/Drupal/Core/Field/Annotation/FieldType.php index 90d0cf7..13feaae 100644 --- a/core/lib/Drupal/Core/Field/Annotation/FieldType.php +++ b/core/lib/Drupal/Core/Field/Annotation/FieldType.php @@ -52,16 +52,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 diff --git a/core/lib/Drupal/Core/Field/FieldItemBase.php b/core/lib/Drupal/Core/Field/FieldItemBase.php index fce6089..5802b4a 100644 --- a/core/lib/Drupal/Core/Field/FieldItemBase.php +++ b/core/lib/Drupal/Core/Field/FieldItemBase.php @@ -26,6 +26,13 @@ /** * {@inheritdoc} */ + public static function settings() { + return array(); + } + + /** + * {@inheritdoc} + */ public static function mainPropertyName() { return 'value'; } diff --git a/core/lib/Drupal/Core/Field/FieldItemInterface.php b/core/lib/Drupal/Core/Field/FieldItemInterface.php index ae7bf6c..579f0d4 100644 --- a/core/lib/Drupal/Core/Field/FieldItemInterface.php +++ b/core/lib/Drupal/Core/Field/FieldItemInterface.php @@ -213,6 +213,14 @@ 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 settings(); + + /** * Returns a form for the field-level settings. * * Invoked from \Drupal\field_ui\Form\FieldEditForm to allow administrators to diff --git a/core/lib/Drupal/Core/Field/FieldTypePluginManager.php b/core/lib/Drupal/Core/Field/FieldTypePluginManager.php index 3928525..f719aaf 100644 --- a/core/lib/Drupal/Core/Field/FieldTypePluginManager.php +++ b/core/lib/Drupal/Core/Field/FieldTypePluginManager.php @@ -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; @@ -21,7 +22,6 @@ class FieldTypePluginManager extends DefaultPluginManager implements FieldTypePl * {@inheritdoc} */ protected $defaults = array( - 'settings' => array(), 'instance_settings' => array(), ); @@ -58,8 +58,12 @@ 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::settings(); + } + return array(); } /** diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php index 0d8dee4..86dfa01 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php @@ -17,10 +17,6 @@ * 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" = "", @@ -36,6 +32,16 @@ class DecimalItem extends NumericItemBase { /** * {@inheritdoc} */ + public static function settings() { + $settings = parent::settings(); + $settings['precision'] = 10; + $settings['scale'] = 2; + return $settings; + } + + /** + * {@inheritdoc} + */ public static function propertyDefinitions(FieldDefinitionInterface $field_definition) { $properties['value'] = DataDefinition::create('string') ->setLabel(t('Decimal value')); diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php index 2ef9971..c1e303f 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php @@ -18,9 +18,6 @@ * id = "string", * label = @Translation("String"), * description = @Translation("An entity field containing a string value."), - * settings = { - * "max_length" = "255" - * }, * configurable = FALSE, * default_widget = "string", * default_formatter = "string" @@ -31,6 +28,15 @@ class StringItem extends FieldItemBase { /** * {@inheritdoc} */ + public static function settings() { + $settings = parent::settings(); + $settings['max_length'] = 255; + return $settings; + } + + /** + * {@inheritdoc} + */ public static function propertyDefinitions(FieldDefinitionInterface $field_definition) { $properties['value'] = DataDefinition::create('string') ->setLabel(t('Text value')); diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UriItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UriItem.php index 0fa6820..0f8dfbc 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UriItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UriItem.php @@ -21,9 +21,6 @@ * id = "uri", * label = @Translation("URI"), * description = @Translation("An entity field containing a URI."), - * settings = { - * "max_length" = "2048" - * }, * configurable = FALSE * ) */ @@ -32,6 +29,15 @@ class UriItem extends StringItem { /** * {@inheritdoc} */ + public static function settings() { + $settings = parent::settings(); + $settings['max_length'] = 2048; + return $settings; + } + + /** + * {@inheritdoc} + */ public static function propertyDefinitions(FieldDefinitionInterface $field_definition) { $properties['value'] = DataDefinition::create('uri') ->setLabel(t('URI value')); diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UuidItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UuidItem.php index 27ca9e9..9d38ae6 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UuidItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UuidItem.php @@ -16,9 +16,6 @@ * id = "uuid", * label = @Translation("UUID"), * description = @Translation("An entity field containing a UUID."), - * settings = { - * "max_length" = "128" - * }, * configurable = FALSE * ) */ @@ -27,6 +24,15 @@ class UuidItem extends StringItem { /** * {@inheritdoc} */ + public static function settings() { + $settings = parent::settings(); + $settings['max_length'] = 128; + return $settings; + } + + /** + * {@inheritdoc} + */ public function applyDefaultValue($notify = TRUE) { // Default to one field item with a generated UUID. $uuid = \Drupal::service('uuid'); diff --git a/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldType/CommentItem.php b/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldType/CommentItem.php index 115bce2..0a41d3d 100644 --- a/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldType/CommentItem.php +++ b/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldType/CommentItem.php @@ -18,9 +18,6 @@ * id = "comment", * label = @Translation("Comments"), * description = @Translation("This field manages configuration and presentation of comments on an entity."), - * settings = { - * "description" = "", - * }, * instance_settings = { * "default_mode" = COMMENT_MODE_THREADED, * "per_page" = 50, @@ -38,6 +35,15 @@ class CommentItem extends FieldItemBase implements CommentItemInterface { /** * {@inheritdoc} */ + public static function settings() { + $settings = parent::settings(); + $settings['description'] = ''; + return $settings; + } + + /** + * {@inheritdoc} + */ public static function propertyDefinitions(FieldDefinitionInterface $field_definition) { $properties['status'] = DataDefinition::create('integer') ->setLabel(t('Comment status value')); diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/Field/FieldType/DateTimeItem.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/Field/FieldType/DateTimeItem.php index 6119730..7130ac5 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Plugin/Field/FieldType/DateTimeItem.php +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/Field/FieldType/DateTimeItem.php @@ -19,9 +19,6 @@ * id = "datetime", * label = @Translation("Date"), * description = @Translation("Create and store date values."), - * settings = { - * "datetime_type" = "datetime" - * }, * default_widget = "datetime_default", * default_formatter = "datetime_default", * list_class = "\Drupal\datetime\Plugin\Field\FieldType\DateTimeFieldItemList" @@ -30,6 +27,15 @@ class DateTimeItem extends FieldItemBase implements PrepareCacheInterface { /** + * {@inheritdoc} + */ + public static function settings() { + $settings = parent::settings(); + $settings['datetime_type'] = 'datetime'; + return $settings; + } + + /** * Value for the 'datetime_type' setting: store only a date. */ const DATETIME_TYPE_DATE = 'date'; diff --git a/core/modules/field/lib/Drupal/field/Entity/FieldConfig.php b/core/modules/field/lib/Drupal/field/Entity/FieldConfig.php index e3aaea4..061817a 100644 --- a/core/modules/field/lib/Drupal/field/Entity/FieldConfig.php +++ b/core/modules/field/lib/Drupal/field/Entity/FieldConfig.php @@ -321,7 +321,9 @@ 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. - $this->settings += $field_type['settings']; + if (isset($field_type['class'])) { + $this->settings += $field_type['class']::settings(); + } // Notify the entity storage controller. $entity_manager->getStorageController($this->entity_type)->onFieldCreate($this); @@ -510,8 +512,12 @@ public function getSettings() { // $this. $field_type_info = \Drupal::service('plugin.manager.field.field_type')->getDefinition($this->type); - $settings = $this->settings + $field_type_info['settings'] + $field_type_info['instance_settings']; - return $settings; + $settings = array(); + if (isset($field_type_info['class'])) { + $settings = $field_type_info['class']::settings(); + } + // @todo Convert instance settings to method. + return $this->settings + $settings + $field_type_info['instance_settings']; } /** diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldType/ShapeItem.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldType/ShapeItem.php index 348886a..50cb184 100644 --- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldType/ShapeItem.php +++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldType/ShapeItem.php @@ -18,9 +18,6 @@ * id = "shape", * label = @Translation("Shape"), * description = @Translation("Another dummy field type."), - * settings = { - * "foreign_key_name" = "shape" - * }, * default_widget = "test_field_widget", * default_formatter = "field_test_default" * ) @@ -30,6 +27,15 @@ class ShapeItem extends FieldItemBase { /** * {@inheritdoc} */ + public static function settings() { + $settings = parent::settings(); + $settings['foreign_key_name'] = 'shape'; + return $settings; + } + + /** + * {@inheritdoc} + */ public static function propertyDefinitions(FieldDefinitionInterface $field_definition) { $properties['shape'] = DataDefinition::create('string') ->setLabel(t('Shape')); diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldType/TestItem.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldType/TestItem.php index 6c85daa..a608ec2 100644 --- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldType/TestItem.php +++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldType/TestItem.php @@ -19,11 +19,6 @@ * id = "test_field", * label = @Translation("Test field"), * description = @Translation("Dummy field type used for tests."), - * settings = { - * "test_field_setting" = "dummy test string", - * "changeable" = "a changeable field setting", - * "unchangeable" = "an unchangeable field setting" - * }, * instance_settings = { * "test_instance_setting" = "dummy test string", * "test_cached_data" = FALSE @@ -37,6 +32,17 @@ class TestItem extends FieldItemBase implements PrepareCacheInterface { /** * {@inheritdoc} */ + public static function settings() { + $settings = parent::settings(); + $settings['test_field_setting'] = 'dummy test string'; + $settings['changeable'] = 'a changeable field setting'; + $settings['unchangeable'] = 'an unchangeable field setting'; + return $settings; + } + + /** + * {@inheritdoc} + */ public static function propertyDefinitions(FieldDefinitionInterface $field_definition) { $properties['value'] = DataDefinition::create('integer') ->setLabel(t('Test integer value')); diff --git a/core/modules/file/lib/Drupal/file/Plugin/Field/FieldType/FileItem.php b/core/modules/file/lib/Drupal/file/Plugin/Field/FieldType/FileItem.php index d72ccad..77379de 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/Field/FieldType/FileItem.php +++ b/core/modules/file/lib/Drupal/file/Plugin/Field/FieldType/FileItem.php @@ -18,12 +18,6 @@ * id = "file", * label = @Translation("File"), * description = @Translation("This field stores the ID of a file as an integer value."), - * settings = { - * "target_type" = "file", - * "display_field" = "0", - * "display_default" = "0", - * "uri_scheme" = "" - * }, * instance_settings = { * "file_extensions" = "txt", * "file_directory" = "", @@ -40,6 +34,18 @@ class FileItem extends EntityReferenceItem { /** * {@inheritdoc} */ + public static function settings() { + $settings = parent::settings(); + $settings['target_type'] = 'file'; + $settings['display_field'] = 0; + $settings['display_default'] = 0; + $settings['uri_scheme'] = ''; + return $settings; + } + + /** + * {@inheritdoc} + */ public static function schema(FieldDefinitionInterface $field_definition) { return array( 'columns' => array( diff --git a/core/modules/image/lib/Drupal/image/Plugin/Field/FieldType/ImageItem.php b/core/modules/image/lib/Drupal/image/Plugin/Field/FieldType/ImageItem.php index 5442d17..6162a41 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/Field/FieldType/ImageItem.php +++ b/core/modules/image/lib/Drupal/image/Plugin/Field/FieldType/ImageItem.php @@ -18,31 +18,6 @@ * id = "image", * label = @Translation("Image"), * description = @Translation("This field stores the ID of an image file as an integer value."), - * settings = { - * "target_type" = "file", - * "uri_scheme" = "", - * "default_image" = { - * "fid" = NULL, - * "alt" = "", - * "title" = "", - * "width" = NULL, - * "height" = NULL - * }, - * "column_groups" = { - * "file" = { - * "label" = @Translation("File"), - * "columns" = { "target_id", "width", "height" } - * }, - * "alt" = { - * "label" = @Translation("Alt"), - * "translatable" = TRUE - * }, - * "title" = { - * "label" = @Translation("Title"), - * "translatable" = TRUE - * } - * } - * }, * instance_settings = { * "file_extensions" = "png gif jpg jpeg", * "file_directory" = "", @@ -71,6 +46,37 @@ class ImageItem extends FileItem { /** * {@inheritdoc} */ + public static function settings() { + $settings = parent::settings(); + $settings['target_type'] = 'file'; + $settings['uri_scheme'] = ''; + $settings['default_image'] = array( + 'fid' => NULL, + 'alt' => '', + 'title' => '', + 'width' => NULL, + 'height' => NULL, + ); + $settings['column_groups'] = array( + 'file' => array( + 'label' => t('File'), + 'columns' => array('target_id', 'width', 'height'), + ), + 'alt' => array( + 'label' => t('Alt'), + 'translatable' => TRUE, + ), + 'title' => array( + 'label' => t('Title'), + 'translatable' => TRUE, + ), + ); + return $settings; + } + + /** + * {@inheritdoc} + */ public static function schema(FieldDefinitionInterface $field_definition) { return array( 'columns' => array( diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/Field/FieldType/TaxonomyTermReferenceItem.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/Field/FieldType/TaxonomyTermReferenceItem.php index d044f96..bacc4ef 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/Field/FieldType/TaxonomyTermReferenceItem.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/Field/FieldType/TaxonomyTermReferenceItem.php @@ -19,16 +19,6 @@ * id = "taxonomy_term_reference", * label = @Translation("Term Reference"), * description = @Translation("This field stores a reference to a taxonomy term."), - * settings = { - * "target_type" = "taxonomy_term", - * "options_list_callback" = NULL, - * "allowed_values" = { - * { - * "vocabulary" = "", - * "parent" = "0" - * } - * } - * }, * instance_settings = { }, * default_widget = "options_select", * default_formatter = "taxonomy_term_reference_link", @@ -40,6 +30,22 @@ class TaxonomyTermReferenceItem extends EntityReferenceItem implements AllowedVa /** * {@inheritdoc} */ + public static function settings() { + $settings = parent::settings(); + $settings['target_type'] = 'taxonomy_term'; + $settings['options_list_callback'] = NULL; + $settings['allowed_values'] = array( + array( + 'vocabulary' => '', + 'parent' => 0, + ), + ); + return $settings; + } + + /** + * {@inheritdoc} + */ public function getPossibleValues(AccountInterface $account = NULL) { // Flatten options firstly, because Possible Options may contain group // arrays. diff --git a/core/modules/text/lib/Drupal/text/Plugin/Field/FieldType/TextItem.php b/core/modules/text/lib/Drupal/text/Plugin/Field/FieldType/TextItem.php index 93bd949..0eee199 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/Field/FieldType/TextItem.php +++ b/core/modules/text/lib/Drupal/text/Plugin/Field/FieldType/TextItem.php @@ -16,9 +16,6 @@ * id = "text", * label = @Translation("Text"), * description = @Translation("This field stores varchar text in the database."), - * settings = { - * "max_length" = "255" - * }, * instance_settings = { * "text_processing" = "0" * }, @@ -31,6 +28,15 @@ class TextItem extends TextItemBase { /** * {@inheritdoc} */ + public static function settings() { + $settings = parent::settings(); + $settings['max_length'] = 255; + return $settings; + } + + /** + * {@inheritdoc} + */ public static function schema(FieldDefinitionInterface $field_definition) { return array( 'columns' => array(