diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigEntityReferenceItemBase.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigEntityReferenceItemBase.php index fc791c9..b3c0150 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigEntityReferenceItemBase.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigEntityReferenceItemBase.php @@ -168,98 +168,4 @@ protected function getLegacyCallback($hook) { } } - /** - * {@inheritdoc} - * - * Copied from \Drupal\field\Plugin\Type\FieldType\ConfigFieldItemBase, - * since we cannot extend it. - */ - public function defaultValuesForm(array &$form, array &$form_state) { - $entity = $this->getParent()->getParent(); - $items = $this->getParent(); - $widget = $this->defaultValueWidget($form_state); - - $element = array('#parents' => array('default_value_input')); - $element += $widget->form($entity, $entity->language()->id, $items, $element, $form_state); - - return $element; - } - - /** - * {@inheritdoc} - * - * Copied from \Drupal\field\Plugin\Type\FieldType\ConfigFieldItemBase, - * since we cannot extend it. - */ - public function defaultValuesFormValidate(array $element, array &$form, array &$form_state) { - $entity = $this->getParent()->getParent(); - $items = $this->getParent(); - $widget = $this->defaultValueWidget($form_state); - - // Extract the 'default value'. - $widget->extractFormValues($entity, $entity->language()->id, $items, $element, $form_state); - $violations = $items->validate(); - - if (count($violations)) { - // Store reported errors in $form_state. - $field_name = $this->getFieldDefinition()->getFieldName(); - $field_state = field_form_get_state($element['#parents'], $field_name, $entity->language()->id, $form_state); - $field_state['constraint_violations'] = $violations; - field_form_set_state($element['#parents'], $field_name, $entity->language()->id, $form_state, $field_state); - - // Assign reported errors to the correct form element. - $widget->flagErrors($entity, $entity->language()->id, $this->getParent(), $element, $form_state); - } - } - - /** - * {@inheritdoc} - * - * Copied from \Drupal\field\Plugin\Type\FieldType\ConfigFieldItemBase, - * since we cannot extend it. - */ - public function defaultValuesFormSubmit(array $element, array &$form, array &$form_state) { - $entity = $this->getParent()->getParent(); - $items = $this->getParent(); - $widget = $this->defaultValueWidget($form_state); - - // Extract the 'default value', and return it as an array. - $widget->extractFormValues($entity, $entity->language()->id, $items, $element, $form_state); - return $items->getValue(); - } - - /** - * Returns the widget object used in default value form. - * - * Copied from \Drupal\field\Plugin\Type\FieldType\ConfigFieldItemBase, - * since we cannot extend it. - * - * @param array $form_state - * The form_state array. - * - * @return \Drupal\field\Plugin\Type\Widget\WidgetInterface - * A Widget object. - */ - protected function defaultValueWidget(array &$form_state) { - if (!isset($form_state['default_value_widget'])) { - $entity = $this->getParent()->getParent(); - - // Force a non-required widget. - $this->getFieldDefinition()->required = FALSE; - $this->getFieldDefinition()->description = ''; - - // Use the widget currently configured for the 'default' form mode, or - // fallback to the default widget for the field type. - $entity_form_display = entity_get_form_display($entity->entityType(), $entity->bundle(), 'default'); - $widget = $entity_form_display->getRenderer($this->getFieldDefinition()->getFieldName()); - if (!$widget) { - $widget = \Drupal::service('plugin.manager.field.widget')->getInstance(array('field_definition' => $this->getFieldDefinition())); - } - - $form_state['default_value_widget'] = $widget; - } - - return $form_state['default_value_widget']; - } - } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php index 1a58ff4..6eab246 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php @@ -79,4 +79,88 @@ protected function getDefaultValue() { return $this->getInstance()->getFieldDefaultValue($this->getParent()); } + /** + * {@inheritdoc} + */ + public function defaultValuesForm(array &$form, array &$form_state) { + if (empty($this->getInstance()->default_value_function)) { + $entity = $this->getParent(); + $widget = $this->defaultValueWidget($form_state); + + // Place the input in a separate place in the submitted values tree. + $element = array('#parents' => array('default_value_input')); + $element += $widget->form($entity, $entity->language()->id, $this, $element, $form_state); + + return $element; + } + } + + /** + * {@inheritdoc} + */ + public function defaultValuesFormValidate(array $element, array &$form, array &$form_state) { + $entity = $this->getParent(); + $langcode = $entity->language()->id; + $widget = $this->defaultValueWidget($form_state); + + // Extract the submitted value, and validate it. + $widget->extractFormValues($entity, $langcode, $this, $element, $form_state); + $violations = $this->validate(); + + if (count($violations)) { + // Store reported errors in $form_state. + $field_name = $this->getFieldDefinition()->getFieldName(); + $field_state = field_form_get_state($element['#parents'], $field_name, $langcode, $form_state); + $field_state['constraint_violations'] = $violations; + field_form_set_state($element['#parents'], $field_name, $langcode, $form_state, $field_state); + + // Assign reported errors to the correct form element. + $widget->flagErrors($entity, $langcode, $this, $element, $form_state); + } + } + + /** + * {@inheritdoc} + */ + public function defaultValuesFormSubmit(array $element, array &$form, array &$form_state) { + $entity = $this->getParent(); + $langcode = $entity->language()->id; + $widget = $this->defaultValueWidget($form_state); + + // Extract the submitted value, and return it as an array. + $widget->extractFormValues($entity, $langcode, $this, $element, $form_state); + return $this->getValue(); + } + + /** + * Returns the widget object used in default value form. + * + * @param array $form_state + * The form_state array. + * + * @return \Drupal\field\Plugin\Type\Widget\WidgetInterface + * A Widget object. + */ + protected function defaultValueWidget(array &$form_state) { + if (!isset($form_state['default_value_widget'])) { + $entity = $this->getParent(); + + // Force a non-required widget. + $this->getFieldDefinition()->required = FALSE; + $this->getFieldDefinition()->description = ''; + + // Use the widget currently configured for the 'default' form mode, or + // fallback to the default widget for the field type. + $entity_form_display = entity_get_form_display($entity->entityType(), $entity->bundle(), 'default'); + $widget = $entity_form_display->getRenderer($this->getFieldDefinition()->getFieldName()); + if (!$widget) { + $widget = \Drupal::service('plugin.manager.field.widget')->getInstance(array('field_definition' => $this->getFieldDefinition())); + } + + $form_state['default_value_widget'] = $widget; + } + + return $form_state['default_value_widget']; + } + } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldInterface.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldInterface.php index 090cda9..b71fdbd 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldInterface.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldInterface.php @@ -21,4 +21,53 @@ */ public function getInstance(); + /** + * Returns a form for the default value input. + * + * Invoked from \Drupal\field_ui\Form\FieldInstanceEditForm to allow + * administrators to configure instance-level default value. + * + * @param array $form + * The form where the settings form is being included in. + * @param array $form_state + * The form state of the (entire) configuration form. + * + * @return array + * The form definition for the field instance default value. + */ + public function defaultValuesForm(array &$form, array &$form_state); + + /** + * Validates the submitted default value. + * + * Invoked from \Drupal\field_ui\Form\FieldInstanceEditForm to allow + * administrators to configure instance-level default value. + * + * @param array $element + * The default value form base element + * @param array $form + * The form where the settings form is being included in. + * @param array $form_state + * The form state of the (entire) configuration form. + */ + public function defaultValuesFormValidate(array $element, array &$form, array &$form_state); + + /** + * Processes the submitted default value. + * + * Invoked from \Drupal\field_ui\Form\FieldInstanceEditForm to allow + * administrators to configure instance-level default value. + * + * @param array $element + * The default value form base element + * @param array $form + * The form where the settings form is being included in. + * @param array $form_state + * The form state of the (entire) configuration form. + * + * @return array + * The field instance default value. + */ + public function defaultValuesFormSubmit(array $element, array &$form, array &$form_state); + } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemBase.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemBase.php index 6686cd5..4313b43 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemBase.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemBase.php @@ -45,86 +45,4 @@ public function instanceSettingsForm(array $form, array &$form_state) { return array(); } - /** - * {@inheritdoc} - */ - public function defaultValuesForm(array &$form, array &$form_state) { - $entity = $this->getParent()->getParent(); - $items = $this->getParent(); - $widget = $this->defaultValueWidget($form_state); - - $element = array('#parents' => array('default_value_input')); - $element += $widget->form($entity, $entity->language()->id, $items, $element, $form_state); - - return $element; - } - - /** - * {@inheritdoc} - */ - public function defaultValuesFormValidate(array $element, array &$form, array &$form_state) { - $entity = $this->getParent()->getParent(); - $items = $this->getParent(); - $widget = $this->defaultValueWidget($form_state); - - // Extract the 'default value'. - $widget->extractFormValues($entity, $entity->language()->id, $items, $element, $form_state); - $violations = $items->validate(); - - if (count($violations)) { - // Store reported errors in $form_state. - $field_name = $this->getFieldDefinition()->getFieldName(); - $field_state = field_form_get_state($element['#parents'], $field_name, $entity->language()->id, $form_state); - $field_state['constraint_violations'] = $violations; - field_form_set_state($element['#parents'], $field_name, $entity->language()->id, $form_state, $field_state); - - // Assign reported errors to the correct form element. - $widget->flagErrors($entity, $entity->language()->id, $this->getParent(), $element, $form_state); - } - } - - /** - * {@inheritdoc} - */ - public function defaultValuesFormSubmit(array $element, array &$form, array &$form_state) { - $entity = $this->getParent()->getParent(); - $items = $this->getParent(); - $widget = $this->defaultValueWidget($form_state); - - // Extract the 'default value', and return it as an array. - $widget->extractFormValues($entity, $entity->language()->id, $items, $element, $form_state); - return $items->getValue(); - } - - /** - * Returns the widget object used in default value form. - * - * @param array $form_state - * The form_state array. - * - * @return \Drupal\field\Plugin\Type\Widget\WidgetInterface - * A Widget object. - */ - protected function defaultValueWidget(array &$form_state) { - if (!isset($form_state['default_value_widget'])) { - $entity = $this->getParent()->getParent(); - - // Force a non-required widget. - $this->getFieldDefinition()->required = FALSE; - $this->getFieldDefinition()->description = ''; - - // Use the widget currently configured for the 'default' form mode, or - // fallback to the default widget for the field type. - $entity_form_display = entity_get_form_display($entity->entityType(), $entity->bundle(), 'default'); - $widget = $entity_form_display->getRenderer($this->getFieldDefinition()->getFieldName()); - if (!$widget) { - $widget = \Drupal::service('plugin.manager.field.widget')->getInstance(array('field_definition' => $this->getFieldDefinition())); - } - - $form_state['default_value_widget'] = $widget; - } - - return $form_state['default_value_widget']; - } - } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemInterface.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemInterface.php index 47b4cfb..a584640 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemInterface.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemInterface.php @@ -96,56 +96,4 @@ public function settingsForm(array $form, array &$form_state, $has_data); */ public function instanceSettingsForm(array $form, array &$form_state); - /** - * Builds the default value widget for a given field instance. - * - * Invoked from \Drupal\field_ui\Form\FieldInstanceEditForm to allow - * administrators to configure instance-level default value. - * - * @param array $form - * The form where the settings form is being included in. - * @param array $form_state - * The form state of the (entire) configuration form. - * - * @return array - * The form definition for the field instance default value. - */ - public function defaultValuesForm(array &$form, array &$form_state); - - /** - * Validates the default value widget for a given field instance. - * - * Invoked from \Drupal\field_ui\Form\FieldInstanceEditForm to allow - * administrators to configure instance-level default value. - * - * @param array $element - * The default value form base element - * @param array $form - * The form where the settings form is being included in. - * @param array $form_state - * The form state of the (entire) configuration form. - * - * @return array - * The field instance default value. - */ - public function defaultValuesFormValidate(array $element, array &$form, array &$form_state); - - /** - * Submits the default value widget for a given field instance. - * - * Invoked from \Drupal\field_ui\Form\FieldInstanceEditForm to allow - * administrators to configure instance-level default value. - * - * @param array $element - * The default value form base element - * @param array $form - * The form where the settings form is being included in. - * @param array $form_state - * The form state of the (entire) configuration form. - * - * @return array - * The field instance default value. - */ - public function defaultValuesFormSubmit(array $element, array &$form, array &$form_state); - } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/LegacyFieldTypeDiscoveryDecorator.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/LegacyFieldTypeDiscoveryDecorator.php index 04778b3..2936b47 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/LegacyFieldTypeDiscoveryDecorator.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/LegacyFieldTypeDiscoveryDecorator.php @@ -63,9 +63,11 @@ public function getDefinitions() { $function = $module . '_field_info'; if (function_exists($function)) { foreach ($function() as $plugin_id => $definition) { - $definition['id'] = $plugin_id; - $definition['provider'] = $module; - $definition['list_class'] = '\Drupal\field\Plugin\field\field_type\LegacyConfigField'; + $definition += array( + 'id' => $plugin_id, + 'provider' => $module, + 'list_class' => '\Drupal\field\Plugin\field\field_type\LegacyConfigField', + ); $definitions[$plugin_id] = $definition; } } diff --git a/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldInstanceEditForm.php b/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldInstanceEditForm.php index 72bc745..ba692ec 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldInstanceEditForm.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldInstanceEditForm.php @@ -81,7 +81,7 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac // Create an arbitrary entity object (used by the 'default value' widget). $ids = (object) array('entity_type' => $this->instance['entity_type'], 'bundle' => $this->instance['bundle'], 'entity_id' => NULL); $form['#entity'] = _field_create_entity_from_ids($ids); - $item = $this->getFieldItem($form['#entity'], $this->instance['field_name']); + $items = $this->getFieldItems($form['#entity'], $this->instance['field_name']); if (!empty($field['locked'])) { $form['locked'] = array( @@ -135,11 +135,11 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac ); // Add instance settings for the field type. - $form['instance']['settings'] = $item->instanceSettingsForm($form, $form_state); + $form['instance']['settings'] = $items[0]->instanceSettingsForm($form, $form_state); $form['instance']['settings']['#weight'] = 10; // Add handling for default value. - if (empty($this->instance['default_value_function']) && $element = $item->defaultValuesForm($form, $form_state)) { + if ($element = $items->defaultValuesForm($form, $form_state)) { $element += array( '#type' => 'details', '#title' => t('Default value'), @@ -166,8 +166,8 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac */ public function validateForm(array &$form, array &$form_state) { if (isset($form['instance']['default_value'])) { - $item = $this->getFieldItem($form['#entity'], $this->instance['field_name']); - $item->defaultValuesFormValidate($form['instance']['default_value'], $form, $form_state); + $items = $this->getFieldItems($form['#entity'], $this->instance['field_name']); + $items->defaultValuesFormValidate($form['instance']['default_value'], $form, $form_state); } } @@ -177,8 +177,8 @@ public function validateForm(array &$form, array &$form_state) { public function submitForm(array &$form, array &$form_state) { // Handle the default value. if (isset($form['instance']['default_value'])) { - $item = $this->getFieldItem($form['#entity'], $this->instance['field_name']); - $default_value = $item->defaultValuesFormSubmit($form['instance']['default_value'], $form, $form_state); + $items = $this->getFieldItems($form['#entity'], $this->instance['field_name']); + $default_value = $items->defaultValuesFormSubmit($form['instance']['default_value'], $form, $form_state); } else { $default_value = array(); @@ -209,25 +209,23 @@ public function delete(array &$form, array &$form_state) { } /** - * Returns a FieldItem object for an entity. - * - * @todo Remove when all entity types extend EntityNG. + * Returns a Field object for an entity. * * @param \Drupal\Core\Entity\EntityInterface $entity * An entity. * @param string $field_name * The field name. * - * @return \Drupal\field\Plugin\Type\FieldType\ConfigFieldItemInterface - * The field item object. + * @return \Drupal\field\Plugin\Type\FieldType\ConfigFieldInterface + * The field object. */ - protected function getFieldItem(EntityInterface $entity, $field_name) { + protected function getFieldItems(EntityInterface $entity, $field_name) { if ($entity instanceof EntityNG) { - $item = $entity->get($field_name)->offsetGet(0); + $item = $entity->get($field_name); } else { $definitions = \Drupal::entityManager()->getFieldDefinitions($entity->entityType(), $entity->bundle()); - $item = \Drupal::typedData()->create($definitions[$field_name], $this->instance->default_value, $field_name, $entity)->offsetGet(0); + $item = \Drupal::typedData()->create($definitions[$field_name], $this->instance->default_value, $field_name, $entity); } return $item; } diff --git a/core/modules/file/file.field.inc b/core/modules/file/file.field.inc index 3e3806e..7acd885 100644 --- a/core/modules/file/file.field.inc +++ b/core/modules/file/file.field.inc @@ -30,6 +30,7 @@ function file_field_info() { 'default_widget' => 'file_generic', 'default_formatter' => 'file_default', 'class' => '\Drupal\file\Type\FileItem', + 'list_class' => '\Drupal\file\Type\FileField', ), ); } diff --git a/core/modules/file/lib/Drupal/file/Type/FileField.php b/core/modules/file/lib/Drupal/file/Type/FileField.php new file mode 100644 index 0000000..3666238 --- /dev/null +++ b/core/modules/file/lib/Drupal/file/Type/FileField.php @@ -0,0 +1,22 @@ + 'image_image', 'default_formatter' => 'image', 'class' => '\Drupal\image\Type\ImageItem', + 'list_class' => '\Drupal\image\Type\ImageField', ), ); } diff --git a/core/modules/image/lib/Drupal/image/Type/ImageField.php b/core/modules/image/lib/Drupal/image/Type/ImageField.php new file mode 100644 index 0000000..aea434b --- /dev/null +++ b/core/modules/image/lib/Drupal/image/Type/ImageField.php @@ -0,0 +1,22 @@ +