diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DatetimeDefaultFormatter.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DatetimeDefaultFormatter.php index e038471..c50d5ae 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DatetimeDefaultFormatter.php +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DatetimeDefaultFormatter.php @@ -32,7 +32,7 @@ class DateTimeDefaultFormatter extends FormatterBase { /** - * Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements(). + * {@inheritdoc} */ public function viewElements(EntityInterface $entity, $langcode, array $items) { @@ -53,7 +53,7 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { // The formatted output will be in local time. $date->setTimeZone(timezone_open(drupal_get_user_timezone())); - if ($this->field['settings']['datetime_type'] == 'date') { + if ($this->field_definition['settings']['datetime_type'] == 'date') { // A date without time will pick up the current time, use the default. datetime_date_default_time($date); } @@ -93,7 +93,7 @@ function dateFormat($date) { } /** - * Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm(). + * {@inheritdoc} */ public function settingsForm(array $form, array &$form_state) { @@ -119,7 +119,7 @@ public function settingsForm(array $form, array &$form_state) { } /** - * Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsSummary(). + * {@inheritdoc} */ public function settingsSummary() { diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DatetimePlainFormatter.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DatetimePlainFormatter.php index 45a85a2..55525fd 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DatetimePlainFormatter.php +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DatetimePlainFormatter.php @@ -28,7 +28,7 @@ class DateTimePlainFormatter extends FormatterBase { /** - * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements(). + * {@inheritdoc} */ public function viewElements(EntityInterface $entity, $langcode, array $items) { @@ -43,7 +43,7 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { $date = $item['date']; $date->setTimeZone(timezone_open(drupal_get_user_timezone())); $format = DATETIME_DATETIME_STORAGE_FORMAT; - if ($this->field['settings']['datetime_type'] == 'date') { + if ($this->field_definition['settings']['datetime_type'] == 'date') { // A date without time will pick up the current time, use the default. datetime_date_default_time($date); $format = DATETIME_DATE_STORAGE_FORMAT; diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDatelistWidget.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDatelistWidget.php index 366a4c4..1e9b0a6 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDatelistWidget.php +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDatelistWidget.php @@ -12,7 +12,6 @@ use Drupal\Component\Plugin\Discovery\DiscoveryInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\field\Plugin\PluginSettingsBase; -use Drupal\field\Plugin\Core\Entity\FieldInstance; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\datetime\DateHelper; @@ -36,23 +35,12 @@ class DateTimeDatelistWidget extends WidgetBase { /** - * Constructs a DateTimeDatelist Widget object. - * - * @param array $plugin_id - * The plugin_id for the widget. - * @param array $plugin_definition - * The plugin implementation definition. - * @param \Drupal\field\Plugin\Core\Entity\FieldInstance $instance - * The field instance to which the widget is associated. - * @param array $settings - * The widget settings. - * @param int $weight - * The widget weight. + * {@inheritdoc} */ - public function __construct($plugin_id, array $plugin_definition, FieldInstance $instance, array $settings, $weight) { + public function __construct($plugin_id, array $plugin_definition, array $field_definition, array $settings, $weight) { // Identify the function used to set the default value. - $instance['default_value_function'] = $this->defaultValueFunction(); - parent::__construct($plugin_id, $plugin_definition, $instance, $settings, $weight); + $field_definition['default_value_function'] = $this->defaultValueFunction(); + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $weight); } /** @@ -66,13 +54,9 @@ public function defaultValueFunction() { } /** - * Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement(). + * {@inheritdoc} */ public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { - - $field = $this->field; - $instance = $this->instance; - $date_order = $this->getSetting('date_order'); $time_type = $this->getSetting('time_type'); $increment = $this->getSetting('increment'); @@ -89,7 +73,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr $element['#element_validate'][] = 'datetime_datelist_widget_validate'; // Identify the type of date and time elements to use. - switch ($field['settings']['datetime_type']) { + switch ($this->field_definition['settings']['datetime_type']) { case 'date': $storage_format = DATETIME_DATE_STORAGE_FORMAT; $type_type = 'none'; @@ -137,7 +121,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr ); // Set the storage and widget options so the validation can use them. The - // validator will not have access to field or instance settings. + // validator will not have access to the field definition. $element['value']['#date_storage_format'] = $storage_format; if (!empty($items[$delta]['date'])) { @@ -145,7 +129,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr // The date was created and verified during field_load(), so it is safe to // use without further inspection. $date->setTimezone( new \DateTimeZone($element['value']['#date_timezone'])); - if ($field['settings']['datetime_type'] == 'date') { + if ($this->field_definition['settings']['datetime_type'] == 'date') { // A date without time will pick up the current time, use the default // time. datetime_date_default_time($date); @@ -156,22 +140,11 @@ public function formElement(array $items, $delta, array $element, $langcode, arr } /** - * - * - * @param array $form - * The form definition as an array. - * @param array $form_state - * The current state of the form as an array. - * - * @return array - * + * {@inheritdoc} */ function settingsForm(array $form, array &$form_state) { $element = parent::settingsForm($form, $form_state); - $field = $this->field; - $instance = $this->instance; - $element['date_order'] = array( '#type' => 'select', '#title' => t('Date part order'), @@ -179,7 +152,7 @@ function settingsForm(array $form, array &$form_state) { '#options' => array('MDY' => t('Month/Day/Year'), 'DMY' => t('Day/Month/Year'), 'YMD' => t('Year/Month/Day')), ); - if ($field['settings']['datetime_type'] == 'datetime') { + if ($this->field_definition['settings']['datetime_type'] == 'datetime') { $element['time_type'] = array( '#type' => 'select', '#title' => t('Time type'), diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php index d51bac4..1d8f8e6 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php @@ -12,7 +12,6 @@ use Drupal\Component\Plugin\Discovery\DiscoveryInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\field\Plugin\PluginSettingsBase; -use Drupal\field\Plugin\Core\Entity\FieldInstance; use Drupal\Core\Datetime\DrupalDateTime; /** @@ -30,23 +29,12 @@ class DateTimeDefaultWidget extends WidgetBase { /** - * Constructs a DateTimeDefault Widget object. - * - * @param array $plugin_id - * The plugin_id for the widget. - * @param array $plugin_definition - * The plugin implementation definition. - * @param \Drupal\field\Plugin\Core\Entity\FieldInstance $instance - * The field instance to which the widget is associated. - * @param array $settings - * The widget settings. - * @param int $weight - * The widget weight. + * {@inheritdoc} */ - public function __construct($plugin_id, array $plugin_definition, FieldInstance $instance, array $settings, $weight) { + public function __construct($plugin_id, array $plugin_definition, array $field_definition, array $settings, $weight) { // Identify the function used to set the default value. - $instance['default_value_function'] = $this->defaultValueFunction(); - parent::__construct($plugin_id, $plugin_definition, $instance, $settings, $weight); + $field_definition['default_value_function'] = $this->defaultValueFunction(); + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $weight); } /** @@ -60,13 +48,9 @@ public function defaultValueFunction() { } /** - * Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement(). - * + * {@inheritdoc} */ public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { - - $field = $this->field; - $instance = $this->instance; $format_type = datetime_default_format_type(); // We are nesting some sub-elements inside the parent, so we need a wrapper. @@ -80,7 +64,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr $element['#element_validate'][] = 'datetime_datetime_widget_validate'; // Identify the type of date and time elements to use. - switch ($field['settings']['datetime_type']) { + switch ($this->field_definition['settings']['datetime_type']) { case 'date': $date_type = 'date'; $time_type = 'none'; @@ -115,7 +99,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr ); // Set the storage and widget options so the validation can use them. The - // validator will not have access to field or instance settings. + // validator will not have access to the field definition. $element['value']['#date_element_format'] = $element_format; $element['value']['#date_storage_format'] = $storage_format; @@ -124,7 +108,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr // The date was created and verified during field_load(), so it is safe to // use without further inspection. $date->setTimezone(new \DateTimeZone($element['value']['#date_timezone'])); - if ($field['settings']['datetime_type'] == 'date') { + if ($this->field_definition['settings']['datetime_type'] == 'date') { // A date without time will pick up the current time, use the default // time. datetime_date_default_time($date); diff --git a/core/modules/edit/lib/Drupal/edit/Access/EditEntityFieldAccessCheck.php b/core/modules/edit/lib/Drupal/edit/Access/EditEntityFieldAccessCheck.php index f7f0c06..e139d0e 100644 --- a/core/modules/edit/lib/Drupal/edit/Access/EditEntityFieldAccessCheck.php +++ b/core/modules/edit/lib/Drupal/edit/Access/EditEntityFieldAccessCheck.php @@ -41,7 +41,7 @@ public function access(Route $route, Request $request) { * Implements EntityFieldAccessCheckInterface::accessEditEntityField(). */ public function accessEditEntityField(EntityInterface $entity, $field_name) { - return $entity->access('update') && field_access('edit', $field_name, $entity->entityType(), $entity); + return $entity->access('update') && ($field = field_info_field($field_name)) && field_access('edit', $field, $entity->entityType(), $entity); } /** diff --git a/core/modules/entity_reference/entity_reference.module b/core/modules/entity_reference/entity_reference.module index 3c68c46..cfb968c 100644 --- a/core/modules/entity_reference/entity_reference.module +++ b/core/modules/entity_reference/entity_reference.module @@ -80,10 +80,9 @@ function entity_reference_field_widget_info_alter(&$info) { * * @return \Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface */ -function entity_reference_get_selection_handler($field, $instance, EntityInterface $entity = NULL) { +function entity_reference_get_selection_handler(array $field_definition, EntityInterface $entity = NULL) { $options = array( - 'field' => $field, - 'instance' => $instance, + 'field_definition' => $field_definition, 'entity' => $entity, ); return drupal_container()->get('plugin.manager.entity_reference.selection')->getInstance($options); @@ -149,7 +148,8 @@ function entity_reference_field_validate(EntityInterface $entity = NULL, $field, } if ($ids) { - $valid_ids = entity_reference_get_selection_handler($field, $instance, $entity)->validateReferencableEntities(array_keys($ids)); + $field_definition = $instance->getFieldDefinition(); + $valid_ids = entity_reference_get_selection_handler($field_definition, $entity)->validateReferencableEntities(array_keys($ids)); $invalid_entities = array_diff_key($ids, array_flip($valid_ids)); if ($invalid_entities) { @@ -290,7 +290,8 @@ function entity_reference_field_instance_settings_form($field, $instance, $form_ '#attributes' => array('class' => array('entity_reference-settings')), ); - $handler = entity_reference_get_selection_handler($field, $instance); + $field_definition = $instance->getFieldDefinition(); + $handler = entity_reference_get_selection_handler($field_definition); $form['handler']['handler_settings'] += $handler->settingsForm($field, $instance); return $form; @@ -386,13 +387,13 @@ function entity_reference_settings_ajax_submit($form, &$form_state) { /** * Implements hook_options_list(). */ -function entity_reference_options_list($field, $instance, $entity_type = NULL, $entity = NULL) { - if (!$options = entity_reference_get_selection_handler($field, $instance, $entity)->getReferencableEntities()) { +function entity_reference_options_list(array $field_definition, $entity_type = NULL, $entity = NULL) { + if (!$options = entity_reference_get_selection_handler($field_definition, $entity)->getReferencableEntities()) { return array(); } // Rebuild the array by changing the bundle key into the bundle label. - $target_type = $field['settings']['target_type']; + $target_type = $field_definition['settings']['target_type']; $bundles = entity_get_bundles($target_type); $return = array(); diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceAutocomplete.php b/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceAutocomplete.php index b3fe3a0..609d36d 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceAutocomplete.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceAutocomplete.php @@ -71,7 +71,8 @@ public function getMatches($field, $instance, $entity_type, $entity_id = '', $pr throw new AccessDeniedHttpException(); } } - $handler = entity_reference_get_selection_handler($field, $instance, $entity); + $field_definition = $instance->getFieldDefinition(); + $handler = entity_reference_get_selection_handler($field_definition, $entity); if (isset($string)) { // Get an array of matching entities. diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Derivative/SelectionBase.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Derivative/SelectionBase.php index 9e191bf..55dfe97 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Derivative/SelectionBase.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Derivative/SelectionBase.php @@ -46,7 +46,7 @@ public function getDerivativeDefinitions(array $base_plugin_definition) { foreach (entity_get_info() as $entity_type => $info) { if (!in_array($entity_type, $supported_entities)) { $this->derivatives[$entity_type] = $base_plugin_definition; - $this->derivatives[$entity_type]['label'] = t('@enitty_type selection', array('@entity_type' => $info['label'])); + $this->derivatives[$entity_type]['label'] = t('@entity_type selection', array('@entity_type' => $info['label'])); } } return $this->derivatives; diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/Selection/SelectionBroken.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/Selection/SelectionBroken.php index 31c1cb1..29e4d87 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/Selection/SelectionBroken.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/Selection/SelectionBroken.php @@ -17,9 +17,8 @@ class SelectionBroken implements SelectionInterface { /** * Constructs a SelectionBroken object. */ - public function __construct($field, $instance = NULL) { - $this->field = $field; - $this->instance = $instance; + public function __construct($field_definition) { + $this->field_definition = $field_definition; } /** diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/SelectionPluginManager.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/SelectionPluginManager.php index f178242..adea96f 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/SelectionPluginManager.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/SelectionPluginManager.php @@ -42,7 +42,7 @@ public function createInstance($plugin_id, array $configuration = array()) { return parent::createInstance($plugin_id, $configuration); } catch (PluginException $e) { - return new SelectionBroken($configuration['field'], $configuration['instance']); + return new SelectionBroken($configuration['field_definition']); } } @@ -50,8 +50,8 @@ public function createInstance($plugin_id, array $configuration = array()) { * Overrides \Drupal\Component\Plugin\PluginManagerBase::getInstance(). */ public function getInstance(array $options) { - $selection_handler = $options['instance']['settings']['handler']; - $target_entity_type = $options['field']['settings']['target_type']; + $selection_handler = $options['field_definition']['settings']['handler']; + $target_entity_type = $options['field_definition']['settings']['target_type']; // Get all available selection plugins for this entity type. $selection_handler_groups = $this->getSelectionGroups($target_entity_type); diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionBase.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionBase.php index ab60905..a346fcc 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionBase.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionBase.php @@ -30,18 +30,11 @@ class SelectionBase implements SelectionInterface { /** - * The field array. + * The field definition array. * * @var array */ - protected $field; - - /** - * The instance array. - * - * @var array - */ - protected $instance; + protected $field_definition; /** * The entity object, or NULL @@ -53,9 +46,8 @@ class SelectionBase implements SelectionInterface { /** * Constructs a SelectionBase object. */ - public function __construct($field, $instance, EntityInterface $entity = NULL) { - $this->field = $field; - $this->instance = $instance; + public function __construct(array $field_definition, EntityInterface $entity = NULL) { + $this->field_definition = $field_definition; $this->entity = $entity; } @@ -165,7 +157,7 @@ public static function settingsForm(&$field, &$instance) { * Implements SelectionInterface::getReferencableEntities(). */ public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) { - $target_type = $this->field['settings']['target_type']; + $target_type = $this->field_definition['settings']['target_type']; $query = $this->buildEntityQuery($match, $match_operator); if ($limit > 0) { @@ -204,7 +196,7 @@ public function countReferencableEntities($match = NULL, $match_operator = 'CONT public function validateReferencableEntities(array $ids) { $result = array(); if ($ids) { - $target_type = $this->field['settings']['target_type']; + $target_type = $this->field_definition['settings']['target_type']; $entity_info = entity_get_info($target_type); $query = $this->buildEntityQuery(); $result = $query @@ -264,7 +256,7 @@ public function validateAutocompleteInput($input, &$element, &$form_state, $form * it. */ public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { - $target_type = $this->field['settings']['target_type']; + $target_type = $this->field_definition['settings']['target_type']; $entity_info = entity_get_info($target_type); $query = \Drupal::entityQuery($target_type); @@ -277,17 +269,17 @@ public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { } // Add entity-access tag. - $query->addTag($this->field['settings']['target_type'] . '_access'); + $query->addTag($this->field_definition['settings']['target_type'] . '_access'); // Add the Selection handler for // entity_reference_query_entity_reference_alter(). $query->addTag('entity_reference'); - $query->addMetaData('field', $this->field); + $query->addMetaData('field_definition', $this->field_definition); $query->addMetaData('entity_reference_selection_handler', $this); // Add the sort option. - if (!empty($this->instance['settings']['handler_settings']['sort'])) { - $sort_settings = $this->instance['settings']['handler_settings']['sort']; + if (!empty($this->field_definition['settings']['handler_settings']['sort'])) { + $sort_settings = $this->field_definition['settings']['handler_settings']['sort']; if ($sort_settings['field'] != '_none') { $query->sort($sort_settings['field'], $sort_settings['direction']); } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceEntityFormatter.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceEntityFormatter.php index d829cb2..b2f0f50 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceEntityFormatter.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceEntityFormatter.php @@ -33,10 +33,10 @@ class EntityReferenceEntityFormatter extends EntityReferenceFormatterBase { /** - * Overrides \Drupal\entity_reference\Plugin\field\formatter\EntityReferenceFormatterBase::settingsForm(). + * {@inheritdoc} */ public function settingsForm(array $form, array &$form_state) { - $view_modes = entity_get_view_modes($this->field['settings']['target_type']); + $view_modes = entity_get_view_modes($this->field_definition['settings']['target_type']); $options = array(); foreach ($view_modes as $view_mode => $view_mode_settings) { $options[$view_mode] = $view_mode_settings['label']; @@ -60,12 +60,12 @@ public function settingsForm(array $form, array &$form_state) { } /** - * Overrides \Drupal\field\Plugin\Type\Formatter\FormatterBase::settingsSummary(). + * {@inheritdoc} */ public function settingsSummary() { $summary = array(); - $view_modes = entity_get_view_modes($this->field['settings']['target_type']); + $view_modes = entity_get_view_modes($this->field_definition['settings']['target_type']); $view_mode = $this->getSetting('view_mode'); $summary[] = t('Rendered as @mode', array('@mode' => isset($view_modes[$view_mode]['label']) ? $view_modes[$view_mode]['label'] : $view_mode)); $summary[] = $this->getSetting('links') ? t('Display links') : t('Do not display links'); @@ -74,7 +74,7 @@ public function settingsSummary() { } /** - * Overrides \Drupal\entity_reference\Plugin\field\formatter\EntityReferenceFormatterBase::viewElements(). + * {@inheritdoc} */ public function viewElements(EntityInterface $entity, $langcode, array $items) { // Remove un-accessible items. @@ -83,7 +83,7 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { $view_mode = $this->getSetting('view_mode'); $links = $this->getSetting('links'); - $target_type = $this->field['settings']['target_type']; + $target_type = $this->field_definition['settings']['target_type']; $elements = array(); diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php index a88f2ea..0e4664f 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php @@ -34,14 +34,14 @@ class AutocompleteTagsWidget extends AutocompleteWidgetBase { /** - * Overrides \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase::elementValidate() + * {@inheritdoc} */ public function elementValidate($element, &$form_state, $form) { $value = array(); // If a value was entered into the autocomplete. - $handler = entity_reference_get_selection_handler($this->field, $this->instance); - $bundles = entity_get_bundles($this->field['settings']['target_type']); - $auto_create = isset($this->instance['settings']['handler_settings']['auto_create']) ? $this->instance['settings']['handler_settings']['auto_create'] : FALSE; + $handler = entity_reference_get_selection_handler($this->field_definition); + $bundles = entity_get_bundles($this->field_definition['settings']['target_type']); + $auto_create = isset($this->field_definition['settings']['handler_settings']['auto_create']) ? $this->field_definition['settings']['handler_settings']['auto_create'] : FALSE; if (!empty($element['#value'])) { $entities = drupal_explode_tags($element['#value']); @@ -62,7 +62,7 @@ public function elementValidate($element, &$form_state, $form) { if ($match) { $value[] = array('target_id' => $match); } - elseif ($auto_create && (count($this->instance['settings']['handler_settings']['target_bundles']) == 1 || count($bundles) == 1)) { + elseif ($auto_create && (count($this->field_definition['settings']['handler_settings']['target_bundles']) == 1 || count($bundles) == 1)) { // Auto-create item. see entity_reference_field_presave(). $value[] = array('target_id' => 'auto_create', 'label' => $entity); } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php index 57c4b7b..db8e9cd 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php @@ -38,7 +38,7 @@ class AutocompleteWidget extends AutocompleteWidgetBase { /** - * Overrides \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase::formElement(). + * {@inheritdoc} */ public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { // We let the Field API handles multiple values for us, only take care of @@ -54,10 +54,10 @@ public function formElement(array $items, $delta, array $element, $langcode, arr } /** - * Overrides \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase::elementValidate() + * {@inheritdoc} */ public function elementValidate($element, &$form_state, $form) { - $auto_create = isset($this->instance['settings']['handler_settings']['auto_create']) ? $this->instance['settings']['handler_settings']['auto_create'] : FALSE; + $auto_create = isset($this->field_definition['settings']['handler_settings']['auto_create']) ? $this->field_definition['settings']['handler_settings']['auto_create'] : FALSE; // If a value was entered into the autocomplete. $value = ''; @@ -69,11 +69,11 @@ public function elementValidate($element, &$form_state, $form) { else { // Try to get a match from the input string when the user didn't use the // autocomplete but filled in a value manually. - $handler = entity_reference_get_selection_handler($this->field, $this->instance); + $handler = entity_reference_get_selection_handler($this->field_definition); $value = $handler->validateAutocompleteInput($element['#value'], $element, $form_state, $form, !$auto_create); } - if (!$value && $auto_create && (count($this->instance['settings']['handler_settings']['target_bundles']) == 1)) { + if (!$value && $auto_create && (count($this->field_definition['settings']['handler_settings']['target_bundles']) == 1)) { // Auto-create item. see entity_reference_field_presave(). $value = array( 'target_id' => 'auto_create', diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php index 6a8df6f..bede809 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php @@ -17,7 +17,7 @@ abstract class AutocompleteWidgetBase extends WidgetBase { /** - * Overrides \Drupal\field\Plugin\Type\Widget\WidgetBase::settingsForm(). + * {@inheritdoc} */ public function settingsForm(array $form, array &$form_state) { $element['match_operator'] = array( @@ -50,24 +50,15 @@ public function settingsForm(array $form, array &$form_state) { } /** - * Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement(). + * {@inheritdoc} */ public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { - $instance = $this->instance; - $field = $this->field; - $entity = isset($element['#entity']) ? $element['#entity'] : NULL; + $field_definition = $this->field_definition; + $entity = $element['#entity']; // Prepare the autocomplete path. $autocomplete_path = $this->getSetting('autocomplete_path'); - $autocomplete_path .= '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'] . '/'; - - // Use as a placeholder in the URL when we don't have an entity. - // Most web servers collapse two consecutive slashes. - $id = 'NULL'; - if ($entity && $entity_id = $entity->id()) { - $id = $entity_id; - } - $autocomplete_path .= $id; + $autocomplete_path .= '/' . $field_definition['field_name'] . '/' . $entity->entityType() . '/' . $entity->bundle() . '/' . $entity->id(); $element += array( '#type' => 'textfield', @@ -83,7 +74,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr } /** - * Overrides \Drupal\field\Plugin\Type\Widget\WidgetBase::errorElement(). + * {@inheritdoc} */ public function errorElement(array $element, array $error, array $form, array &$form_state) { return $element['target_id']; @@ -107,7 +98,7 @@ protected function getLabels(array $items) { } // Load those entities and loop through them to extract their labels. - $entities = entity_load_multiple($this->field['settings']['target_type'], $entity_ids); + $entities = entity_load_multiple($this->field_definition['settings']['target_type'], $entity_ids); foreach ($entities as $entity_id => $entity_item) { $label = $entity_item->label(); diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionAccessTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionAccessTest.php index cf1c619..849a16d 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionAccessTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionAccessTest.php @@ -31,8 +31,8 @@ function setUp() { $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article')); } - protected function assertReferencable($field, $instance, $tests, $handler_name) { - $handler = entity_reference_get_selection_handler($field, $instance); + protected function assertReferencable(array $field_definition, $tests, $handler_name) { + $handler = entity_reference_get_selection_handler($field_definition); foreach ($tests as $test) { foreach ($test['arguments'] as $arguments) { @@ -57,19 +57,15 @@ protected function assertReferencable($field, $instance, $tests, $handler_name) * Test the node-specific overrides of the entity handler. */ public function testNodeHandler() { - // Build a fake field instance. - $field = array( + // Build a fake field definition. + $field_definition = array( 'translatable' => FALSE, 'entity_types' => array(), - 'settings' => array( - 'target_type' => 'node', - ), 'field_name' => 'test_field', 'type' => 'entity_reference', 'cardinality' => '1', - ); - $instance = array( 'settings' => array( + 'target_type' => 'node', 'handler' => 'default', 'handler_settings' => array( 'target_bundles' => array(), @@ -159,7 +155,7 @@ public function testNodeHandler() { 'result' => array(), ), ); - $this->assertReferencable($field, $instance, $referencable_tests, 'Node handler'); + $this->assertReferencable($field_definition, $referencable_tests, 'Node handler'); // Test as an admin. $admin_user = $this->drupalCreateUser(array('access content', 'bypass node access')); @@ -188,26 +184,22 @@ public function testNodeHandler() { ), ), ); - $this->assertReferencable($field, $instance, $referencable_tests, 'Node handler (admin)'); + $this->assertReferencable($field_definition, $referencable_tests, 'Node handler (admin)'); } /** * Test the user-specific overrides of the entity handler. */ public function testUserHandler() { - // Build a fake field instance. - $field = array( + // Build a fake field definition. + $field_definition = array( 'translatable' => FALSE, 'entity_types' => array(), - 'settings' => array( - 'target_type' => 'user', - ), 'field_name' => 'test_field', 'type' => 'entity_reference', 'cardinality' => '1', - ); - $instance = array( 'settings' => array( + 'target_type' => 'user', 'handler' => 'default', 'handler_settings' => array( 'target_bundles' => array(), @@ -289,7 +281,7 @@ public function testUserHandler() { 'result' => array(), ), ); - $this->assertReferencable($field, $instance, $referencable_tests, 'User handler'); + $this->assertReferencable($field_definition, $referencable_tests, 'User handler'); $GLOBALS['user'] = $users['admin']; $referencable_tests = array( @@ -328,26 +320,22 @@ public function testUserHandler() { ), ), ); - $this->assertReferencable($field, $instance, $referencable_tests, 'User handler (admin)'); + $this->assertReferencable($field_definition, $referencable_tests, 'User handler (admin)'); } /** * Test the comment-specific overrides of the entity handler. */ public function testCommentHandler() { - // Build a fake field instance. - $field = array( + // Build a fake field definition. + $field_definition = array( 'translatable' => FALSE, 'entity_types' => array(), - 'settings' => array( - 'target_type' => 'comment', - ), 'field_name' => 'test_field', 'type' => 'entity_reference', 'cardinality' => '1', - ); - $instance = array( 'settings' => array( + 'target_type' => 'comment', 'handler' => 'default', 'handler_settings' => array( 'target_bundles' => array(), @@ -453,7 +441,7 @@ public function testCommentHandler() { 'result' => array(), ), ); - $this->assertReferencable($field, $instance, $referencable_tests, 'Comment handler'); + $this->assertReferencable($field_definition, $referencable_tests, 'Comment handler'); // Test as a comment admin. $admin_user = $this->drupalCreateUser(array('access content', 'access comments', 'administer comments')); @@ -471,7 +459,7 @@ public function testCommentHandler() { ), ), ); - $this->assertReferencable($field, $instance, $referencable_tests, 'Comment handler (comment admin)'); + $this->assertReferencable($field_definition, $referencable_tests, 'Comment handler (comment admin)'); // Test as a node and comment admin. $admin_user = $this->drupalCreateUser(array('access content', 'access comments', 'administer comments', 'bypass node access')); @@ -490,6 +478,6 @@ public function testCommentHandler() { ), ), ); - $this->assertReferencable($field, $instance, $referencable_tests, 'Comment handler (comment + node admin)'); + $this->assertReferencable($field_definition, $referencable_tests, 'Comment handler (comment + node admin)'); } } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php index d3245e0..a20c3b7 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php @@ -54,20 +54,15 @@ public function testSort() { field_create_instance($instance_info); - // Build a fake field instance. - $field = array( + // Build a fake field definition. + $field_definition = array( 'translatable' => FALSE, 'entity_types' => array(), - 'settings' => array( - 'target_type' => 'node', - ), 'field_name' => 'test_field', 'type' => 'entity_reference', 'cardinality' => 1, - ); - - $instance = array( 'settings' => array( + 'target_type' => 'node', 'handler' => 'default', 'handler_settings' => array( 'target_bundles' => array(), @@ -119,7 +114,7 @@ public function testSort() { $normal_user = $this->drupalCreateUser(array('access content')); $GLOBALS['user'] = $normal_user; - $handler = entity_reference_get_selection_handler($field, $instance); + $handler = entity_reference_get_selection_handler($field_definition); // Not only assert the result, but make sure the keys are sorted as // expected. @@ -131,11 +126,11 @@ public function testSort() { $this->assertIdentical($result['article'], $expected_result, 'Query sorted by field returned expected values.'); // Assert sort by property. - $instance['settings']['handler_settings']['sort'] = array( + $field_definition['settings']['handler_settings']['sort'] = array( 'field' => 'nid', 'direction' => 'ASC', ); - $handler = entity_reference_get_selection_handler($field, $instance); + $handler = entity_reference_get_selection_handler($field_definition); $result = $handler->getReferencableEntities(); $expected_result = array( $nodes['published1']->nid => $node_labels['published1'], diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/Views/SelectionTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/Views/SelectionTest.php index 6998209..e9831d9 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/Views/SelectionTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/Views/SelectionTest.php @@ -39,19 +39,15 @@ public function testSelectionHandler() { $nodes[$node->type][$node->nid] = $node->label(); } - // Build a fake field instance. - $field = array( + // Build a fake field definition. + $field_definition = array( 'translatable' => FALSE, 'entity_types' => array(), - 'settings' => array( - 'target_type' => 'node', - ), 'field_name' => 'test_field', 'type' => 'entity_reference', 'cardinality' => '1', - ); - $instance = array( 'settings' => array( + 'target_type' => 'node', 'handler' => 'views', 'handler_settings' => array( 'target_bundles' => array(), @@ -65,7 +61,7 @@ public function testSelectionHandler() { ); // Get values from selection handler. - $handler = entity_reference_get_selection_handler($field, $instance); + $handler = entity_reference_get_selection_handler($field_definition); $result = $handler->getReferencableEntities(); $success = FALSE; diff --git a/core/modules/field/field.api.php b/core/modules/field/field.api.php index 33f2581..cfce664 100644 --- a/core/modules/field/field.api.php +++ b/core/modules/field/field.api.php @@ -729,8 +729,9 @@ function hook_field_widget_info_alter(array &$info) { * arguments received by hook_field_widget_form(): * - form: The form structure to which widgets are being attached. This may be * a full form structure, or a sub-element of a larger form. - * - field: The field structure. - * - instance: The field instance structure. + * - field_definition: The field definition. + * - entity_type: The entity type. + * - bundle: The entity bundle. * - langcode: The language associated with $items. * - items: Array of default values for this field. * - delta: The order of this item in the array of subelements (0, 1, 2, etc). @@ -764,9 +765,9 @@ function hook_field_widget_form_alter(&$element, &$form_state, $context) { * arguments received by hook_field_widget_form(): * - "form": The form structure where widgets are being attached to. This * might be a full form structure, or a sub-element of a larger form. - * - "field": The field structure. - * - "instance": The field instance structure. - * - "langcode": The language associated with $items. + * - "field_definition": The field definition. + * - "entity_type": The entity type. + * - "bundle": The entity bundle. * - "items": Array of default values for this field. * - "delta": The order of this item in the array of subelements (0, 1, 2, * etc). @@ -2138,7 +2139,7 @@ function hook_field_storage_purge(\Drupal\Core\Entity\EntityInterface $entity, $ * * @param $op * The operation to be performed. Possible values: 'edit', 'view'. - * @param $field + * @param \Drupal\field\FieldInterface $field * The field on which the operation is to be performed. * @param $entity_type * The type of $entity; for example, 'node' or 'user'. @@ -2150,7 +2151,7 @@ function hook_field_storage_purge(\Drupal\Core\Entity\EntityInterface $entity, $ * @return * TRUE if the operation is allowed, and FALSE if the operation is denied. */ -function hook_field_access($op, $field, $entity_type, $entity, $account) { +function hook_field_access($op, \Drupal\field\FieldInterface $field, $entity_type, $entity, $account) { if ($field['field_name'] == 'field_of_interest' && $op == 'edit') { return user_access('edit field of interest', $account); } diff --git a/core/modules/field/field.form.inc b/core/modules/field/field.form.inc index 203a112..2100332 100644 --- a/core/modules/field/field.form.inc +++ b/core/modules/field/field.form.inc @@ -158,8 +158,8 @@ function field_add_more_js($form, $form_state) { $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state); - $field = $field_state['field']; - if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED) { + $field_definition = $field_state['field_definition']; + if ($field_definition['cardinality'] != FIELD_CARDINALITY_UNLIMITED) { return; } @@ -260,48 +260,21 @@ function _field_form_state_parents($parents, $field_name, $langcode) { * Retrieves the field definition for a widget's helper callbacks. * * Widget helper element callbacks (such as #process, #element_validate, - * #value_callback, ...) should use field_widget_field() and - * field_widget_instance() instead of field_info_field() and - * field_info_instance() when they need to access field or instance properties. - * See hook_field_widget_form() for more details. + * #value_callback, ...) should use field_widget_field_definition() instead of + * field_info_field() and field_info_instance() when they need to access field + * or instance properties. See hook_field_widget_form() for more details. * * @param $element * The structured array for the widget. * @param $form_state * The form state. * - * @return - * The $field definition array for the current widget. - * - * @see field_widget_instance() - * @see hook_field_widget_form() - */ -function field_widget_field($element, $form_state) { - $field_state = field_form_get_state($element['#field_parents'], $element['#field_name'], $element['#language'], $form_state); - return $field_state['field']; -} - -/** - * Retrieves the instance definition array for a widget's helper callbacks. - * - * Widgets helper element callbacks (such as #process, #element_validate, - * #value_callback, ...) should use field_widget_field() and - * field_widget_instance() instead of field_info_field() and - * field_info_instance() when they need to access field or instance properties. - * See hook_field_widget_form() for more details. - * - * @param $element - * The structured array for the widget. - * @param $form_state - * The form state. - * - * @return - * The $instance definition array for the current widget. + * @return array + * The field definition array for the current widget. * - * @see field_widget_field() * @see hook_field_widget_form() */ -function field_widget_instance($element, $form_state) { +function field_widget_field_definition($element, $form_state) { $field_state = field_form_get_state($element['#field_parents'], $element['#field_name'], $element['#language'], $form_state); - return $field_state['instance']; + return $field_state['field_definition']; } diff --git a/core/modules/field/field.module b/core/modules/field/field.module index 7660f57..c467010 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -6,6 +6,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Template\Attribute; +use Drupal\field\FieldInterface; /* * Load all public Field API functions. Drupal currently has no @@ -944,9 +945,8 @@ function field_has_data($field) { * The operation to be performed. Possible values: * - edit * - view - * @param array $field - * The full field structure array for the field on which the operation is to - * be performed. See field_info_field(). + * @param \Drupal\field\FieldInterface $field + * The field on which the operation is to be performed. * @param $entity_type * The type of $entity; for example, 'node' or 'user'. * @param $entity @@ -957,7 +957,7 @@ function field_has_data($field) { * @return * TRUE if the operation is allowed; FALSE if the operation is denied. */ -function field_access($op, $field, $entity_type, $entity = NULL, $account = NULL) { +function field_access($op, FieldInterface $field, $entity_type, $entity = NULL, $account = NULL) { global $user; if (!isset($account)) { diff --git a/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php index e7e9bd5..01c7ff7 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php @@ -493,6 +493,40 @@ public function getBundles() { } /** + * Returns a field definition array. + * + * @return array + * An array with the following keys: + * - cache_id + * - field_name + * - type + * - label + * - translatable + * - settings + * - cardinality + * - required + * - description + * - columns + * - module + */ + public function getFieldDefinition() { + $field_definition = array( + 'cache_id' => $this->uuid(), + 'field_name' => $this->id, + 'type' => $this->type, + 'label' => $this->label(), + 'translatable' => $this->translatable, + 'settings' => $this->settings, + 'cardinality' => $this->cardinality, + 'required' => FALSE, + 'description' => '', + 'columns' => $this['columns'], + 'module' => $this->module, + ); + return $field_definition; + } + + /** * {@inheritdoc} */ public function offsetExists($offset) { diff --git a/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldInstance.php b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldInstance.php index 53132ec..4944957 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldInstance.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldInstance.php @@ -455,6 +455,40 @@ public function getField() { } /** + * Returns a field definition array. + * + * @return array + * An array with the following keys: + * - cache_id + * - field_name + * - type + * - label + * - translatable + * - settings + * - cardinality + * - required + * - description + * - columns + * - module + */ + public function getFieldDefinition() { + $field_definition = array( + 'cache_id' => $this->uuid(), + 'field_name' => $this->field->id, + 'type' => $this->field->type, + 'label' => $this->label(), + 'translatable' => $this->field->translatable, + 'settings' => $this->settings + $this->field->settings, + 'cardinality' => $this->field->cardinality, + 'required' => $this->required, + 'description' => $this->description, + 'columns' => $this->field['columns'], + 'module' => $this->field->module, + ); + return $field_definition; + } + + /** * {@inheritdoc} */ public function getWidget() { @@ -465,8 +499,7 @@ public function getWidget() { $context = array( 'entity_type' => $this->entity_type, 'bundle' => $this->bundle, - 'field' => $this->field, - 'instance' => $this, + 'field_definition' => $this->getFieldDefinition(), ); // Invoke hook_field_widget_properties_alter() and // hook_field_widget_properties_ENTITY_TYPE_alter(). diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php index a46b465..0e90509 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php @@ -2,14 +2,13 @@ /** * @file - * Definition of Drupal\field\Plugin\Type\Formatter\FormatterBase. + * Contains \Drupal\field\Plugin\Type\Formatter\FormatterBase. */ namespace Drupal\field\Plugin\Type\Formatter; use Drupal\Core\Entity\EntityInterface; use Drupal\field\Plugin\PluginSettingsBase; -use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Base class for 'Field formatter' plugin implementations. @@ -17,18 +16,11 @@ abstract class FormatterBase extends PluginSettingsBase implements FormatterInterface { /** - * The field definition. + * The field definition array. * * @var array */ - protected $field; - - /** - * The field instance definition. - * - * @var \Drupal\field\Plugin\Core\Entity\FieldInstance - */ - protected $instance; + protected $field_definition; /** * The formatter settings. @@ -58,8 +50,8 @@ * The plugin_id for the formatter. * @param array $plugin_definition * The plugin implementation definition. - * @param \Drupal\field\Plugin\Core\Entity\FieldInstance $instance - * The field instance to which the formatter is associated. + * @param array $field_definition + * The definition of the field to which the formatter is associated. * @param array $settings * The formatter settings. * @param string $label @@ -67,23 +59,19 @@ * @param string $view_mode * The view mode. */ - public function __construct($plugin_id, array $plugin_definition, $instance, array $settings, $label, $view_mode) { + public function __construct($plugin_id, array $plugin_definition, array $field_definition, array $settings, $label, $view_mode) { parent::__construct(array(), $plugin_id, $plugin_definition); - $this->instance = $instance; - $this->field = field_info_field($instance['field_name']); + $this->field_definition = $field_definition; $this->settings = $settings; $this->label = $label; $this->viewMode = $view_mode; } /** - * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::view(). + * {@inheritdoc} */ public function view(EntityInterface $entity, $langcode, array $items) { - $field = $this->field; - $instance = $this->instance; - $addition = array(); $elements = $this->viewElements($entity, $langcode, $items); @@ -91,14 +79,14 @@ public function view(EntityInterface $entity, $langcode, array $items) { $entity_type = $entity->entityType(); $info = array( '#theme' => 'field', - '#title' => $instance['label'], - '#access' => field_access('view', $field, $entity->entityType(), $entity), + '#title' => $this->field_definition['label'], + '#access' => $this->checkFieldAccess('view', $entity), '#label_display' => $this->label, '#view_mode' => $this->viewMode, '#language' => $langcode, - '#field_name' => $field['field_name'], - '#field_type' => $field['type'], - '#field_translatable' => $field['translatable'], + '#field_name' => $this->field_definition['field_name'], + '#field_type' => $this->field_definition['type'], + '#field_translatable' => $this->field_definition['translatable'], '#entity_type' => $entity_type, '#bundle' => $entity->bundle(), '#object' => $entity, @@ -106,29 +94,43 @@ public function view(EntityInterface $entity, $langcode, array $items) { '#formatter' => $this->getPluginId(), ); - $addition[$field['field_name']] = array_merge($info, $elements); + $addition[$this->field_definition['field_name']] = array_merge($info, $elements); } return $addition; } /** - * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm(). + * {@inheritdoc} */ public function settingsForm(array $form, array &$form_state) { return array(); } /** - * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsSummary(). + * {@inheritdoc} */ public function settingsSummary() { return ''; } /** - * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::prepareView(). + * {@inheritdoc} */ public function prepareView(array $entities, $langcode, array &$items) { } + /** + * Returns whether the currently logged in user has access to the field. + * + * @todo Remove this once Field API access is unified with entity field + * access: http://drupal.org/node/1994140. + */ + protected function checkFieldAccess($op, $entity) { + if ($field = field_info_field($this->field_definition['field_name'])) { + return field_access($op, $field, $entity->entityType(), $entity); + } + else { + return FALSE; + } + } } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterFactory.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterFactory.php index 0df5b97..244e305 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterFactory.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterFactory.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\field\Plugin\Type\Formatter\FormatterFactory. + * Contains \Drupal\field\Plugin\Type\Formatter\FormatterFactory. */ namespace Drupal\field\Plugin\Type\Formatter; @@ -15,11 +15,12 @@ class FormatterFactory extends DefaultFactory { /** - * Overrides Drupal\Component\Plugin\Factory\DefaultFactory::createInstance(). + * {@inheritdoc} */ public function createInstance($plugin_id, array $configuration) { $plugin_definition = $this->discovery->getDefinition($plugin_id); $plugin_class = static::getPluginClass($plugin_id, $plugin_definition); - return new $plugin_class($plugin_id, $plugin_definition, $configuration['instance'], $configuration['settings'], $configuration['label'], $configuration['view_mode']); + $field_definition = $configuration['instance']->getFieldDefinition(); + return new $plugin_class($plugin_id, $plugin_definition, $field_definition, $configuration['settings'], $configuration['label'], $configuration['view_mode']); } } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php index 64c83a2..ec2645c 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\field\Plugin\Type\Widget\WidgetBase. + * Contains \Drupal\field\Plugin\Type\Widget\WidgetBase. */ namespace Drupal\field\Plugin\Type\Widget; @@ -10,7 +10,6 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Entity\EntityInterface; use Drupal\field\Plugin\PluginSettingsBase; -use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Base class for 'Field widget' plugin implementations. @@ -18,18 +17,11 @@ abstract class WidgetBase extends PluginSettingsBase implements WidgetInterface { /** - * The field definition. + * The field definition array. * * @var array */ - protected $field; - - /** - * The field instance definition. - * - * @var \Drupal\field\Plugin\Core\Entity\FieldInstance - */ - protected $instance; + protected $field_definition; /** * The widget settings. @@ -52,29 +44,26 @@ * The plugin_id for the widget. * @param array $plugin_definition * The plugin implementation definition. - * @param \Drupal\field\Plugin\Core\Entity\FieldInstance $instance - * The field instance to which the widget is associated. + * @param array $field_definition + * The definition of the field to which the widget is associated. * @param array $settings * The widget settings. * @param int $weight * The widget weight. */ - public function __construct($plugin_id, array $plugin_definition, FieldInstance $instance, array $settings, $weight) { + public function __construct($plugin_id, array $plugin_definition, array $field_definition, array $settings, $weight) { parent::__construct(array(), $plugin_id, $plugin_definition); - $this->instance = $instance; - $this->field = field_info_field($instance['field_name']); + $this->field_definition = $field_definition; $this->settings = $settings; $this->weight = $weight; } /** - * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::form(). + * {@inheritdoc} */ public function form(EntityInterface $entity, $langcode, array $items, array &$form, array &$form_state, $get_delta = NULL) { - $field = $this->field; - $instance = $this->instance; - $field_name = $field['field_name']; + $field_name = $this->field_definition['field_name']; $parents = $form['#parents']; @@ -85,11 +74,10 @@ public function form(EntityInterface $entity, $langcode, array $items, array &$f // Store field information in $form_state. if (!field_form_get_state($parents, $field_name, $langcode, $form_state)) { $field_state = array( - 'field' => $field, - 'instance' => $instance, - 'items_count' => count($items), - 'array_parents' => array(), - 'errors' => array(), + 'field_definition' => $this->field_definition, + 'items_count' => count($items), + 'array_parents' => array(), + 'errors' => array(), ); field_form_set_state($parents, $field_name, $langcode, $form_state, $field_state); } @@ -104,8 +92,8 @@ public function form(EntityInterface $entity, $langcode, array $items, array &$f if (isset($get_delta) || $definition['multiple_values']) { $delta = isset($get_delta) ? $get_delta : 0; $element = array( - '#title' => check_plain($instance['label']), - '#description' => field_filter_xss(\Drupal::token()->replace($instance['description'])), + '#title' => check_plain($this->field_definition['label']), + '#description' => field_filter_xss(\Drupal::token()->replace($this->field_definition['description'])), ); $element = $this->formSingleElement($entity, $items, $delta, $langcode, $element, $form, $form_state); @@ -135,7 +123,7 @@ public function form(EntityInterface $entity, $langcode, array $items, array &$f '#type' => 'container', '#attributes' => array( 'class' => array( - 'field-type-' . drupal_html_class($field['type']), + 'field-type-' . drupal_html_class($this->field_definition['type']), 'field-name-' . drupal_html_class($field_name), 'field-widget-' . drupal_html_class($this->getPluginId()), ), @@ -157,7 +145,7 @@ public function form(EntityInterface $entity, $langcode, array $items, array &$f // when $langcode is unknown. '#language' => $langcode, $langcode => $elements, - '#access' => field_access('edit', $field, $entity->entityType(), $entity), + '#access' => $this->checkFieldAccess('edit', $entity), ); return $addition; @@ -172,14 +160,11 @@ public function form(EntityInterface $entity, $langcode, array $items, array &$f * - table display and drag-n-drop value reordering */ protected function formMultipleElements(EntityInterface $entity, array $items, $langcode, array &$form, array &$form_state) { - $field = $this->field; - $instance = $this->instance; - $field_name = $field['field_name']; - + $field_name = $this->field_definition['field_name']; $parents = $form['#parents']; // Determine the number of widgets to display. - switch ($field['cardinality']) { + switch ($this->field_definition['cardinality']) { case FIELD_CARDINALITY_UNLIMITED: $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state); $max = $field_state['items_count']; @@ -187,16 +172,16 @@ protected function formMultipleElements(EntityInterface $entity, array $items, $ break; default: - $max = $field['cardinality'] - 1; - $is_multiple = ($field['cardinality'] > 1); + $max = $this->field_definition['cardinality'] - 1; + $is_multiple = ($this->field_definition['cardinality'] > 1); break; } $id_prefix = implode('-', array_merge($parents, array($field_name))); $wrapper_id = drupal_html_id($id_prefix . '-add-more-wrapper'); - $title = check_plain($instance['label']); - $description = field_filter_xss(\Drupal::token()->replace($instance['description'])); + $title = check_plain($this->field_definition['label']); + $description = field_filter_xss(\Drupal::token()->replace($this->field_definition['description'])); $elements = array(); @@ -232,9 +217,9 @@ protected function formMultipleElements(EntityInterface $entity, array $items, $ if ($elements) { $elements += array( '#theme' => 'field_multiple_value_form', - '#field_name' => $field['field_name'], - '#cardinality' => $field['cardinality'], - '#required' => $instance['required'], + '#field_name' => $this->field_definition['field_name'], + '#cardinality' => $this->field_definition['cardinality'], + '#required' => $this->field_definition['required'], '#title' => $title, '#description' => $description, '#prefix' => '
', @@ -243,7 +228,7 @@ protected function formMultipleElements(EntityInterface $entity, array $items, $ ); // Add 'add more' button, if not working with a programmed form. - if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED && empty($form_state['programmed'])) { + if ($this->field_definition['cardinality'] == FIELD_CARDINALITY_UNLIMITED && empty($form_state['programmed'])) { $elements['add_more'] = array( '#type' => 'submit', '#name' => strtr($id_prefix, '-', '_') . '_add_more', @@ -267,19 +252,16 @@ protected function formMultipleElements(EntityInterface $entity, array $items, $ * Generates the form element for a single copy of the widget. */ protected function formSingleElement(EntityInterface $entity, array $items, $delta, $langcode, array $element, array &$form, array &$form_state) { - $instance = $this->instance; - $field = $this->field; - $element += array( '#entity_type' => $entity->entityType(), '#bundle' => $entity->bundle(), '#entity' => $entity, - '#field_name' => $field['field_name'], + '#field_name' => $this->field_definition['field_name'], '#language' => $langcode, '#field_parents' => $form['#parents'], - '#columns' => array_keys($field['columns']), + '#columns' => array_keys($this->field_definition['columns']), // Only the first widget should be required. - '#required' => $delta == 0 && $instance['required'], + '#required' => $delta == 0 && $this->field_definition['required'], '#delta' => $delta, '#weight' => $delta, ); @@ -290,8 +272,9 @@ protected function formSingleElement(EntityInterface $entity, array $items, $del // Allow modules to alter the field widget form element. $context = array( 'form' => $form, - 'field' => $field, - 'instance' => $instance, + 'field_definition' => $this->field_definition, + 'entity_type' => $entity->entityType(), + 'bundle' => $entity->bundle(), 'langcode' => $langcode, 'items' => $items, 'delta' => $delta, @@ -304,10 +287,10 @@ protected function formSingleElement(EntityInterface $entity, array $items, $del } /** - * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::extractFormValues(). + * {@inheritdoc} */ public function extractFormValues(EntityInterface $entity, $langcode, array &$items, array $form, array &$form_state) { - $field_name = $this->field['field_name']; + $field_name = $this->field_definition['field_name']; // Extract the values from $form_state['values']. $path = array_merge($form['#parents'], array($field_name, $langcode)); @@ -342,7 +325,7 @@ public function extractFormValues(EntityInterface $entity, $langcode, array &$it $this->sortItems($items); // Remove empty values. - $items = _field_filter_items($this->field, $items); + $items = _field_filter_items($this->field_definition, $items); // Put delta mapping in $form_state, so that flagErrors() can use it. $field_state = field_form_get_state($form['#parents'], $field_name, $langcode, $form_state); @@ -355,10 +338,10 @@ public function extractFormValues(EntityInterface $entity, $langcode, array &$it } /** - * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::flagErrors(). + * {@inheritdoc} */ public function flagErrors(EntityInterface $entity, $langcode, array $items, array $form, array &$form_state) { - $field_name = $this->field['field_name']; + $field_name = $this->field_definition['field_name']; $field_state = field_form_get_state($form['#parents'], $field_name, $langcode, $form_state); @@ -421,7 +404,7 @@ public function massageFormValues(array $values, array $form, array &$form_state * The field values. */ protected function sortItems(array &$items) { - $is_multiple = ($this->field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($this->field['cardinality'] > 1); + $is_multiple = ($this->field_definition['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($this->field_definition['cardinality'] > 1); if ($is_multiple && isset($items[0]['_weight'])) { usort($items, function ($a, $b) { $a_weight = (is_array($a) ? $a['_weight'] : 0); @@ -437,4 +420,19 @@ protected function sortItems(array &$items) { } } + /** + * Returns whether the currently logged in user has access to the field. + * + * @todo Remove this once Field API access is unified with entity field + * access: http://drupal.org/node/1994140. + */ + protected function checkFieldAccess($op, $entity) { + if ($field = field_info_field($this->field_definition['field_name'])) { + return field_access($op, $field, $entity->entityType(), $entity); + } + else { + return FALSE; + } + } + } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetFactory.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetFactory.php index 392ef5e..e11876a 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetFactory.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetFactory.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\field\Plugin\WidgetFactory. + * Contains \Drupal\field\Plugin\WidgetFactory. */ namespace Drupal\field\Plugin\Type\Widget; @@ -15,11 +15,12 @@ class WidgetFactory extends DefaultFactory { /** - * Overrides Drupal\Component\Plugin\Factory\DefaultFactory::createInstance(). + * {@inheritdoc} */ public function createInstance($plugin_id, array $configuration) { $plugin_definition = $this->discovery->getDefinition($plugin_id); $plugin_class = static::getPluginClass($plugin_id, $plugin_definition); - return new $plugin_class($plugin_id, $plugin_definition, $configuration['instance'], $configuration['settings'], $configuration['weight']); + $field_definition = $configuration['instance']->getFieldDefinition(); + return new $plugin_class($plugin_id, $plugin_definition, $field_definition, $configuration['settings'], $configuration['weight']); } } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetInterface.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetInterface.php index eb82844..571838f 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetInterface.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetInterface.php @@ -58,9 +58,9 @@ public function settingsForm(array $form, array &$form_state); * Therefore, the FAPI element callbacks (such as #process, #element_validate, * #value_callback...) used by the widget cannot use the field_info_field() * or field_info_instance() functions to retrieve the $field or $instance - * definitions they should operate on. The field_widget_field() and - * field_widget_instance() functions should be used instead to fetch the - * current working definitions from $form_state, where Field API stores them. + * definitions they should operate on. The field_widget_field_definition() + * should be used instead to fetch the current working definitions from + * $form_state, where Field API stores them. * * Alternatively, hook_field_widget_form() can extract the needed specific * properties from $field and $instance and set them as ad-hoc @@ -107,8 +107,7 @@ public function settingsForm(array $form, array &$form_state); * @return array * The form elements for a single widget for this field. * - * @see field_widget_field() - * @see field_widget_instance() + * @see field_widget_field_definition() * @see hook_field_widget_form_alter() * @see hook_field_widget_WIDGET_TYPE_form_alter() */ diff --git a/core/modules/field/lib/Drupal/field/Plugin/views/argument/FieldList.php b/core/modules/field/lib/Drupal/field/Plugin/views/argument/FieldList.php index 2176c91..a82da70 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/views/argument/FieldList.php +++ b/core/modules/field/lib/Drupal/field/Plugin/views/argument/FieldList.php @@ -36,7 +36,8 @@ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$o parent::init($view, $display, $options); $field = field_info_field($this->definition['field_name']); - $this->allowed_values = options_allowed_values($field); + $field_definition = $field->getFieldDefinition(); + $this->allowed_values = options_allowed_values($field_definition); } protected function defineOptions() { diff --git a/core/modules/field/lib/Drupal/field/Plugin/views/argument/ListString.php b/core/modules/field/lib/Drupal/field/Plugin/views/argument/ListString.php index ada688f..20038cd 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/views/argument/ListString.php +++ b/core/modules/field/lib/Drupal/field/Plugin/views/argument/ListString.php @@ -36,7 +36,8 @@ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$o parent::init($view, $display, $options); $field = field_info_field($this->definition['field_name']); - $this->allowed_values = options_allowed_values($field); + $field_definition = $field->getFieldDefinition(); + $this->allowed_values = options_allowed_values($field_definition); } protected function defineOptions() { diff --git a/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php b/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php index e9517c0..d339b1e 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php +++ b/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php @@ -13,6 +13,7 @@ use Drupal\views\Plugin\views\field\FieldPluginBase; use Drupal\Component\Annotation\PluginID; use Drupal\views\Views; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * A field that displays fieldapi fields. @@ -60,13 +61,6 @@ class Field extends FieldPluginBase { public $base_table; /** - * Store the field instance. - * - * @var array - */ - public $instance; - - /** * Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::init(). */ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) { @@ -415,8 +409,8 @@ public function buildOptionsForm(&$form, &$form_state) { * @param array $formatter_settings * An associative array of settings for the formatter. * - * @return array - * An associative array of instance date for the fake field. + * @return \Drupal\field\Plugin\Core\Entity\FieldInstance + * An Field instance object for the fake field. * * @see field_info_instance() */ @@ -426,7 +420,7 @@ function fakeFieldInstance($formatter, $formatter_settings) { $field_type = field_info_field_types($field['type']); - return array( + return new FieldInstance(array( // Build a fake entity type and bundle. 'field_name' => $field_name, 'entity_type' => 'views_fake', @@ -452,7 +446,7 @@ function fakeFieldInstance($formatter, $formatter_settings) { 'label' => $field_name, 'description' => '', 'deleted' => FALSE, - ); + )); } /** diff --git a/core/modules/field/tests/modules/field_test/field_test.field.inc b/core/modules/field/tests/modules/field_test/field_test.field.inc index d2f4a94..734ef8d 100644 --- a/core/modules/field/tests/modules/field_test/field_test.field.inc +++ b/core/modules/field/tests/modules/field_test/field_test.field.inc @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\field\FieldException; +use Drupal\field\FieldInterface; /** * Implements hook_field_info(). @@ -197,7 +198,7 @@ function field_test_default_value(EntityInterface $entity, $field, $instance) { /** * Implements hook_field_access(). */ -function field_test_field_access($op, $field, $entity_type, $entity, $account) { +function field_test_field_access($op, FieldInterface $field, $entity_type, $entity, $account) { if ($field['field_name'] == "field_no_{$op}_access") { return FALSE; } diff --git a/core/modules/field/tests/modules/field_test/field_test.module b/core/modules/field/tests/modules/field_test/field_test.module index 15b3ea5..bdc23f8 100644 --- a/core/modules/field/tests/modules/field_test/field_test.module +++ b/core/modules/field/tests/modules/field_test/field_test.module @@ -204,7 +204,7 @@ function field_test_field_attach_view_alter(&$output, $context) { */ function field_test_field_widget_properties_alter(&$widget, $context) { // Make the alter_test_text field 42 characters for nodes and comments. - if (in_array($context['entity_type'], array('node', 'comment')) && ($context['field']['field_name'] == 'alter_test_text')) { + if (in_array($context['entity_type'], array('node', 'comment')) && ($context['field_definition']['field_name'] == 'alter_test_text')) { $widget['settings']['size'] = 42; } } @@ -214,7 +214,7 @@ function field_test_field_widget_properties_alter(&$widget, $context) { */ function field_test_field_widget_properties_user_alter(&$widget, $context) { // Always use buttons for the alter_test_options field on user forms. - if ($context['field']['field_name'] == 'alter_test_options') { + if ($context['field_definition']['field_name'] == 'alter_test_options') { $widget['type'] = 'options_buttons'; } } @@ -223,13 +223,16 @@ function field_test_field_widget_properties_user_alter(&$widget, $context) { * Implements hook_field_widget_form_alter(). */ function field_test_field_widget_form_alter(&$element, &$form_state, $context) { - switch ($context['field']['field_name']) { + $field_name = $context['field_definition']['field_name']; + switch ($field_name) { case 'alter_test_text': - drupal_set_message('Field size: ' . $context['instance']->getWidget()->getSetting('size')); + $instance = field_info_instance($context['entity_type'], $field_name, $context['bundle']); + drupal_set_message('Field size: ' . $instance->getWidget()->getSetting('size')); break; case 'alter_test_options': - drupal_set_message('Widget type: ' . $context['instance']->getWidget()->getPluginId()); + $instance = field_info_instance($context['entity_type'], $field_name, $context['bundle']); + drupal_set_message('Widget type: ' . $instance->getWidget()->getPluginId()); break; } // Set a message if this is for the form displayed to set default value for 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 4803701..46013ce 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 @@ -181,6 +181,7 @@ public function validateForm(array &$form, array &$form_state) { // value' gets validated using the instance settings being submitted. $field_name = $this->instance['field_name']; $entity = $form['#entity']; + $field = $this->instance->getField(); if (isset($form['instance']['default_value_widget'])) { $element = $form['instance']['default_value_widget']; @@ -189,9 +190,8 @@ public function validateForm(array &$form, array &$form_state) { $items = array(); $this->instance->getWidget()->extractFormValues($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); - // Grab the field definition from $form_state. + // Get the field state. $field_state = field_form_get_state($element['#parents'], $field_name, LANGUAGE_NOT_SPECIFIED, $form_state); - $field = $field_state['field']; // Validate the value. $errors = array(); diff --git a/core/modules/file/file.field.inc b/core/modules/file/file.field.inc index de03e50..12f2c13 100644 --- a/core/modules/file/file.field.inc +++ b/core/modules/file/file.field.inc @@ -317,18 +317,18 @@ function file_field_displayed($item, $field) { /** * Retrieves the upload validators for a file field. * - * @param $field - * A field array. + * @param array $field_definition + * A field definition array. * * @return * An array suitable for passing to file_save_upload() or the file field * element's '#upload_validators' property. */ -function file_field_widget_upload_validators($field, $instance) { +function file_field_widget_upload_validators(array $field_definition) { // Cap the upload size according to the PHP limit. $max_filesize = parse_size(file_upload_max_size()); - if (!empty($instance['settings']['max_filesize']) && parse_size($instance['settings']['max_filesize']) < $max_filesize) { - $max_filesize = parse_size($instance['settings']['max_filesize']); + if (!empty($field_definition['settings']['max_filesize']) && parse_size($field_definition['settings']['max_filesize']) < $max_filesize) { + $max_filesize = parse_size($field_definition['settings']['max_filesize']); } $validators = array(); @@ -337,8 +337,8 @@ function file_field_widget_upload_validators($field, $instance) { $validators['file_validate_size'] = array($max_filesize); // Add the extension check if necessary. - if (!empty($instance['settings']['file_extensions'])) { - $validators['file_validate_extensions'] = array($instance['settings']['file_extensions']); + if (!empty($field_definition['settings']['file_extensions'])) { + $validators['file_validate_extensions'] = array($field_definition['settings']['file_extensions']); } return $validators; @@ -347,10 +347,8 @@ function file_field_widget_upload_validators($field, $instance) { /** * Determines the URI for a file field instance. * - * @param $field - * A field array. - * @param $instance - * A field instance array. + * @param array $field_definition + * A field definition array. * @param $data * An array of token objects to pass to * \Drupal\Core\Utility\Token::replace(). @@ -360,13 +358,13 @@ function file_field_widget_upload_validators($field, $instance) { * * @see \Drupal\Core\Utility\Token::replace() */ -function file_field_widget_uri($field, $instance, $data = array()) { - $destination = trim($instance['settings']['file_directory'], '/'); +function file_field_widget_uri(array $field_definition, $data = array()) { + $destination = trim($field_definition['settings']['file_directory'], '/'); // Replace tokens. $destination = Drupal::token()->replace($destination, $data); - return $field['settings']['uri_scheme'] . '://' . $destination; + return $field_definition['settings']['uri_scheme'] . '://' . $destination; } /** @@ -378,9 +376,9 @@ function file_field_widget_value($element, $input = FALSE, $form_state) { if ($input) { // Checkboxes lose their value when empty. // If the display field is present make sure its unchecked value is saved. - $field = field_widget_field($element, $form_state); + $field_definition = field_widget_field_definition($element, $form_state); if (empty($input['display'])) { - $input['display'] = $field['settings']['display_field'] ? 0 : 1; + $input['display'] = $field_definition['settings']['display_field'] ? 0 : 1; } } @@ -449,18 +447,17 @@ function file_field_widget_process($element, &$form_state, $form) { $item = $element['#value']; $item['fids'] = $element['fids']['#value']; - $field = field_widget_field($element, $form_state); - $instance = field_widget_instance($element, $form_state); - $settings = $instance['widget']['settings']; + $field_definition = field_widget_field_definition($element, $form_state); + $settings = $field_definition['settings']; $element['#theme'] = 'file_widget'; // Add the display field if enabled. - if (!empty($field['settings']['display_field']) && $item['fids']) { + if (!empty($field_definition['settings']['display_field']) && $item['fids']) { $element['display'] = array( '#type' => empty($item['fids']) ? 'hidden' : 'checkbox', '#title' => t('Include file in display'), - '#value' => isset($item['display']) ? $item['display'] : $field['settings']['display_default'], + '#value' => isset($item['display']) ? $item['display'] : $field_definition['settings']['display_default'], '#attributes' => array('class' => array('file-display')), ); } @@ -472,7 +469,7 @@ function file_field_widget_process($element, &$form_state, $form) { } // Add the description field if enabled. - if (!empty($instance['settings']['description_field']) && $item['fids']) { + if (!empty($field_definition['settings']['description_field']) && $item['fids']) { $config = config('file.settings'); $element['description'] = array( '#type' => $config->get('description.type'), @@ -485,7 +482,7 @@ function file_field_widget_process($element, &$form_state, $form) { // Adjust the Ajax settings so that on upload and remove of any individual // file, the entire group of file fields is updated together. - if ($field['cardinality'] != 1) { + if ($field_definition['cardinality'] != 1) { $parents = array_slice($element['#array_parents'], 0, -1); $new_path = 'file/ajax/' . implode('/', $parents) . '/' . $form['form_build_id']['#value']; $field_element = NestedArray::getValue($form, $parents); diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php b/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php index 4eebf6c..362f0ad 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php +++ b/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php @@ -31,7 +31,7 @@ class FileWidget extends WidgetBase { /** - * Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm(). + * {@inheritdoc} */ public function settingsForm(array $form, array &$form_state) { $element['progress_indicator'] = array( @@ -50,14 +50,12 @@ public function settingsForm(array $form, array &$form_state) { } /** - * Overrides \Drupal\field\Plugin\Type\Widget\WidgetBase::formMultipleElements(). + * {@inheritdoc} * * Special handling for draggable multiple widgets and 'add more' button. */ protected function formMultipleElements(EntityInterface $entity, array $items, $langcode, array &$form, array &$form_state) { - $field = $this->field; - $instance = $this->instance; - $field_name = $field['field_name']; + $field_name = $this->field_definition['field_name']; $parents = $form['#parents']; @@ -70,23 +68,23 @@ protected function formMultipleElements(EntityInterface $entity, array $items, $ } // Determine the number of widgets to display. - switch ($field['cardinality']) { + switch ($this->field_definition['cardinality']) { case FIELD_CARDINALITY_UNLIMITED: $max = count($items); $is_multiple = TRUE; break; default: - $max = $field['cardinality'] - 1; - $is_multiple = ($field['cardinality'] > 1); + $max = $this->field_definition['cardinality'] - 1; + $is_multiple = ($this->field_definition['cardinality'] > 1); break; } $id_prefix = implode('-', array_merge($parents, array($field_name))); $wrapper_id = drupal_html_id($id_prefix . '-add-more-wrapper'); - $title = check_plain($instance['label']); - $description = field_filter_xss($instance['description']); + $title = check_plain($this->field_definition['label']); + $description = field_filter_xss($this->field_definition['description']); $elements = array(); @@ -120,8 +118,8 @@ protected function formMultipleElements(EntityInterface $entity, array $items, $ } } - $empty_single_allowed = ($this->field['cardinality'] == 1 && $delta == 0); - $empty_multiple_allowed = ($this->field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta < $this->field['cardinality']) && empty($form_state['programmed']); + $empty_single_allowed = ($this->field_definition['cardinality'] == 1 && $delta == 0); + $empty_multiple_allowed = ($this->field_definition['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta < $this->field_definition['cardinality']) && empty($form_state['programmed']); // Add one more empty row for new uploads except when this is a programmed // multiple form as it is not necessary. @@ -150,25 +148,25 @@ protected function formMultipleElements(EntityInterface $entity, array $items, $ $elements['#description'] = $description; $elements['#field_name'] = $element['#field_name']; $elements['#language'] = $element['#language']; - $elements['#display_field'] = !empty($this->field['settings']['display_field']); + $elements['#display_field'] = !empty($this->field_definition['settings']['display_field']); // Add some properties that will eventually be added to the file upload // field. These are added here so that they may be referenced easily // through a hook_form_alter(). $elements['#file_upload_title'] = t('Add a new file'); - $elements['#file_upload_description'] = theme('file_upload_help', array('description' => '', 'upload_validators' => $elements[0]['#upload_validators'], 'cardinality' => $this->field['cardinality'])); + $elements['#file_upload_description'] = theme('file_upload_help', array('description' => '', 'upload_validators' => $elements[0]['#upload_validators'], 'cardinality' => $this->field_definition['cardinality'])); } return $elements; } /** - * Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement(). + * {@inheritdoc} */ public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { $defaults = array( 'fids' => array(), - 'display' => !empty($this->field['settings']['display_default']), + 'display' => !empty($this->field_definition['settings']['display_default']), 'description' => '', ); @@ -177,8 +175,8 @@ public function formElement(array $items, $delta, array $element, $langcode, arr $element_info = element_info('managed_file'); $element += array( '#type' => 'managed_file', - '#upload_location' => file_field_widget_uri($this->field, $this->instance), - '#upload_validators' => file_field_widget_upload_validators($this->field, $this->instance), + '#upload_location' => file_field_widget_uri($this->field_definition), + '#upload_validators' => file_field_widget_upload_validators($this->field_definition), '#value_callback' => 'file_field_widget_value', '#process' => array_merge($element_info['#process'], array('file_field_widget_process')), '#progress_indicator' => $this->getSetting('progress_indicator'), @@ -197,7 +195,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr $default_fids = $element['#extended'] ? $element['#default_value']['fids'] : $element['#default_value']; if (empty($default_fids)) { - $cardinality = isset($this->field['cardinality']) ? $this->field['cardinality'] : 1; + $cardinality = isset($this->field_definition['cardinality']) ? $this->field_definition['cardinality'] : 1; $element['#description'] = theme('file_upload_help', array('description' => $element['#description'], 'upload_validators' => $element['#upload_validators'], 'cardinality' => $cardinality)); $element['#multiple'] = $cardinality != 1 ? TRUE : FALSE; if ($cardinality != 1 && $cardinality != -1) { @@ -209,7 +207,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr } /** - * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::massageFormValues(). + * {@inheritdoc} */ public function massageFormValues(array $values, array $form, array &$form_state) { // Since file upload widget now supports uploads of more than one file at a diff --git a/core/modules/image/image.field.inc b/core/modules/image/image.field.inc index a287620..3331ff4 100644 --- a/core/modules/image/image.field.inc +++ b/core/modules/image/image.field.inc @@ -361,8 +361,8 @@ function image_field_widget_process($element, &$form_state, $form) { } // Get field settings. - $instance = field_widget_instance($element, $form_state); - $settings = $instance['settings']; + $field_definition = field_widget_field_definition($element, $form_state); + $settings = $field_definition['settings']; // Add the additional alt and title fields. $element['alt'] = array( diff --git a/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php b/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php index 8062ff6..77ab4e0 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php +++ b/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php @@ -52,33 +52,33 @@ public function settingsForm(array $form, array &$form_state) { } /** - * Overrides \Drupal\file\Plugin\field\widget\FileWidget::formMultipleElements(). + * {@inheritdoc} * * Special handling for draggable multiple widgets and 'add more' button. */ protected function formMultipleElements(EntityInterface $entity, array $items, $langcode, array &$form, array &$form_state) { $elements = parent::formMultipleElements($entity, $items, $langcode, $form, $form_state); - if ($this->field['cardinality'] == 1) { + if ($this->field_definition['cardinality'] == 1) { // If there's only one field, return it as delta 0. if (empty($elements[0]['#default_value']['fids'])) { - $elements[0]['#description'] = theme('file_upload_help', array('description' => $this->instance['description'], 'upload_validators' => $elements[0]['#upload_validators'], 'cardinality' => $this->field['cardinality'])); + $elements[0]['#description'] = theme('file_upload_help', array('description' => $this->field_definition['description'], 'upload_validators' => $elements[0]['#upload_validators'], 'cardinality' => $this->field_definition['cardinality'])); } } else { - $elements['#file_upload_description'] = theme('file_upload_help', array('upload_validators' => $elements[0]['#upload_validators'], 'cardinality' => $this->field['cardinality'])); + $elements['#file_upload_description'] = theme('file_upload_help', array('upload_validators' => $elements[0]['#upload_validators'], 'cardinality' => $this->field_definition['cardinality'])); } return $elements; } /** - * Overrides \Drupal\file\Plugin\field\widget\FileWidget::formElement(). + * {@inheritdoc} */ public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { $element = parent::formElement($items, $delta, $element, $langcode, $form, $form_state); - $settings = $this->instance['settings']; + $settings = $this->field_definition['settings']; // Add upload resolution validation. if ($settings['max_resolution'] || $settings['min_resolution']) { diff --git a/core/modules/link/lib/Drupal/link/Plugin/field/widget/LinkWidget.php b/core/modules/link/lib/Drupal/link/Plugin/field/widget/LinkWidget.php index 8a1aebb..5d6d1f2 100644 --- a/core/modules/link/lib/Drupal/link/Plugin/field/widget/LinkWidget.php +++ b/core/modules/link/lib/Drupal/link/Plugin/field/widget/LinkWidget.php @@ -33,8 +33,6 @@ class LinkWidget extends WidgetBase { * {@inheritdoc} */ public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { - $instance = $this->instance; - $element['url'] = array( '#type' => 'url', '#title' => t('URL'), @@ -49,13 +47,13 @@ public function formElement(array $items, $delta, array $element, $langcode, arr '#placeholder' => $this->getSetting('placeholder_title'), '#default_value' => isset($items[$delta]['title']) ? $items[$delta]['title'] : NULL, '#maxlength' => 255, - '#access' => $instance['settings']['title'] != DRUPAL_DISABLED, + '#access' => $this->field_definition['settings']['title'] != DRUPAL_DISABLED, ); // Post-process the title field to make it conditionally required if URL is // non-empty. Omit the validation on the field edit form, since the field // settings cannot be saved otherwise. $is_field_edit_form = ($element['#entity'] === NULL); - if (!$is_field_edit_form && $instance['settings']['title'] == DRUPAL_REQUIRED) { + if (!$is_field_edit_form && $this->field_definition['settings']['title'] == DRUPAL_REQUIRED) { $element['#element_validate'] = array(array($this, 'validateTitle')); } @@ -70,7 +68,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr // If cardinality is 1, ensure a label is output for the field by wrapping it // in a details element. - if ($this->field['cardinality'] == 1) { + if ($this->field_definition['cardinality'] == 1) { $element += array( '#type' => 'fieldset', ); diff --git a/core/modules/number/lib/Drupal/number/Plugin/field/widget/NumberWidget.php b/core/modules/number/lib/Drupal/number/Plugin/field/widget/NumberWidget.php index 37c857f..4d4f87a 100644 --- a/core/modules/number/lib/Drupal/number/Plugin/field/widget/NumberWidget.php +++ b/core/modules/number/lib/Drupal/number/Plugin/field/widget/NumberWidget.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\number\Plugin\field\widget\NumberWidget. + * Contains \Drupal\number\Plugin\field\widget\NumberWidget. */ namespace Drupal\number\Plugin\field\widget; @@ -31,7 +31,7 @@ class NumberWidget extends WidgetBase { /** - * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm(). + * {@inheritdoc} */ public function settingsForm(array $form, array &$form_state) { $element['placeholder'] = array( @@ -44,12 +44,9 @@ public function settingsForm(array $form, array &$form_state) { } /** - * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement(). + * {@inheritdoc} */ public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { - $field = $this->field; - $instance = $this->instance; - $value = isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL; $element += array( @@ -59,9 +56,9 @@ public function formElement(array $items, $delta, array $element, $langcode, arr ); // Set the step for floating point and decimal numbers. - switch ($field['type']) { + switch ($this->field_definition['type']) { case 'number_decimal': - $element['#step'] = pow(0.1, $field['settings']['scale']); + $element['#step'] = pow(0.1, $this->field_definition['settings']['scale']); break; case 'number_float': @@ -70,20 +67,20 @@ public function formElement(array $items, $delta, array $element, $langcode, arr } // Set minimum and maximum. - if (is_numeric($instance['settings']['min'])) { - $element['#min'] = $instance['settings']['min']; + if (is_numeric($this->field_definition['settings']['min'])) { + $element['#min'] = $this->field_definition['settings']['min']; } - if (is_numeric($instance['settings']['max'])) { - $element['#max'] = $instance['settings']['max']; + if (is_numeric($this->field_definition['settings']['max'])) { + $element['#max'] = $this->field_definition['settings']['max']; } // Add prefix and suffix. - if (!empty($instance['settings']['prefix'])) { - $prefixes = explode('|', $instance['settings']['prefix']); + if (!empty($this->field_definition['settings']['prefix'])) { + $prefixes = explode('|', $this->field_definition['settings']['prefix']); $element['#field_prefix'] = field_filter_xss(array_pop($prefixes)); } - if (!empty($instance['settings']['suffix'])) { - $suffixes = explode('|', $instance['settings']['suffix']); + if (!empty($this->field_definition['settings']['suffix'])) { + $suffixes = explode('|', $this->field_definition['settings']['suffix']); $element['#field_suffix'] = field_filter_xss(array_pop($suffixes)); } @@ -91,7 +88,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr } /** - * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::errorElement(). + * {@inheritdoc} */ public function errorElement(array $element, array $error, array $form, array &$form_state) { return $element['value']; diff --git a/core/modules/options/lib/Drupal/options/Plugin/field/formatter/OptionsDefaultFormatter.php b/core/modules/options/lib/Drupal/options/Plugin/field/formatter/OptionsDefaultFormatter.php index 01b4efb..7583053 100644 --- a/core/modules/options/lib/Drupal/options/Plugin/field/formatter/OptionsDefaultFormatter.php +++ b/core/modules/options/lib/Drupal/options/Plugin/field/formatter/OptionsDefaultFormatter.php @@ -35,7 +35,7 @@ class OptionsDefaultFormatter extends FormatterBase { public function viewElements(EntityInterface $entity, $langcode, array $items) { $elements = array(); - $allowed_values = options_allowed_values($this->field, $this->instance, $entity); + $allowed_values = options_allowed_values($this->field_definition, $entity); foreach ($items as $delta => $item) { if (isset($allowed_values[$item['value']])) { diff --git a/core/modules/options/lib/Drupal/options/Plugin/field/widget/OnOffWidget.php b/core/modules/options/lib/Drupal/options/Plugin/field/widget/OnOffWidget.php index 7e6b4a5..dceee88 100644 --- a/core/modules/options/lib/Drupal/options/Plugin/field/widget/OnOffWidget.php +++ b/core/modules/options/lib/Drupal/options/Plugin/field/widget/OnOffWidget.php @@ -57,7 +57,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr // Override the title from the incoming $element. if ($this->getSetting('display_label')) { - $element['#title'] = $this->instance['label']; + $element['#title'] = $this->field_definition['label']; } else { $element['#title'] = isset($options[1]) ? $options[1] : ''; diff --git a/core/modules/options/lib/Drupal/options/Plugin/field/widget/OptionsWidgetBase.php b/core/modules/options/lib/Drupal/options/Plugin/field/widget/OptionsWidgetBase.php index 5e0e3bf..cd6dc61 100644 --- a/core/modules/options/lib/Drupal/options/Plugin/field/widget/OptionsWidgetBase.php +++ b/core/modules/options/lib/Drupal/options/Plugin/field/widget/OptionsWidgetBase.php @@ -36,12 +36,10 @@ /** * {@inheritdoc} */ - public function __construct($plugin_id, array $plugin_definition, FieldInstance $instance, array $settings, $weight) { - parent::__construct($plugin_id, $plugin_definition, $instance, $settings, $weight); + public function __construct($plugin_id, array $plugin_definition, array $field_definition, array $settings, $weight) { + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $weight); - // Reset internal pointer since we're dealing with objects now. - reset($this->field['columns']); - $this->column = key($this->field['columns']); + $this->column = key($this->field_definition['columns']); } /** @@ -52,7 +50,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr // element. $this->entity = $element['#entity']; $this->required = $element['#required']; - $this->multiple = ($this->field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($this->field['cardinality'] > 1); + $this->multiple = ($this->field_definition['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($this->field_definition['cardinality'] > 1); $this->has_value = isset($items[0][$this->column]); // Add our custom validator. @@ -115,7 +113,7 @@ protected function getOptions() { $module_handler = \Drupal::moduleHandler(); // Get the list of options from the field type module, and sanitize them. - $options = (array) $module_handler->invoke($this->field['module'], 'options_list', array($this->field, $this->instance, $this->entity)); + $options = (array) $module_handler->invoke($this->field_definition['module'], 'options_list', array($this->field_definition, $this->entity)); // Add an empty option if the widget needs one. if ($empty_option = $this->getEmptyOption()) { @@ -133,8 +131,7 @@ protected function getOptions() { } $context = array( - 'field' => $this->field, - 'instance' => $this->instance, + 'field_definition' => $this->field_definition, 'entity' => $this->entity, ); $module_handler->alter('options_list', $options, $context); diff --git a/core/modules/options/options.api.php b/core/modules/options/options.api.php index a026898..2fef782 100644 --- a/core/modules/options/options.api.php +++ b/core/modules/options/options.api.php @@ -13,12 +13,8 @@ * implement this hook to specify the list of options to display in the * widgets. * - * @param $field - * The field definition. - * @param $instance - * The instance definition. It is recommended to only use instance level - * properties to filter out values from a list defined by field level - * properties. + * @param array $field + * The field definition array. * @param \Drupal\Core\Entity\EntityInterface $entity * The entity object the field is attached to. * @@ -31,7 +27,7 @@ * widget. The HTML tags defined in _field_filter_xss_allowed_tags() are * allowed, other tags will be filtered. */ -function hook_options_list($field, $instance, $entity) { +function hook_options_list(array $field_definition, $entity) { // Sample structure. $options = array( 0 => t('Zero'), @@ -57,7 +53,7 @@ function hook_options_list($field, $instance, $entity) { // In actual implementations, the array of options will most probably depend // on properties of the field. Example from taxonomy.module: $options = array(); - foreach ($field['settings']['allowed_values'] as $tree) { + foreach ($field_definition['settings']['allowed_values'] as $tree) { $terms = taxonomy_get_tree($tree['vid'], $tree['parent'], NULL, TRUE); if ($terms) { foreach ($terms as $term) { @@ -89,6 +85,9 @@ function hook_options_list($field, $instance, $entity) { * (\Drupal\Core\Entity\EntityInterface). * * @see hook_options_list() + * + * @todo Fix documentation and example to be based on $field_definition rather + * that $field and $instance. */ function hook_options_list_alter(array &$options, array $context) { // Check if this is the field we want to change. diff --git a/core/modules/options/options.module b/core/modules/options/options.module index 0d8ec00..618624b 100644 --- a/core/modules/options/options.module +++ b/core/modules/options/options.module @@ -225,10 +225,8 @@ function options_field_update_field($field, $prior_field, $has_data) { * The strings are not safe for output. Keys and values of the array should be * sanitized through field_filter_xss() before being displayed. * - * @param $field - * The field definition. - * @param $instance - * (optional) A field instance array. Defaults to NULL. + * @param array $field_definition + * A field definition array. * @param \Drupal\Core\Entity\EntityInterface $entity * (optional) The entity object. Defaults to NULL. * @@ -236,31 +234,31 @@ function options_field_update_field($field, $prior_field, $has_data) { * The array of allowed values. Keys of the array are the raw stored values * (number or text), values of the array are the display labels. */ -function options_allowed_values($field, $instance = NULL, EntityInterface $entity = NULL) { +function options_allowed_values(array $field_definition, EntityInterface $entity = NULL) { $allowed_values = &drupal_static(__FUNCTION__, array()); - if (!isset($allowed_values[$field['uuid']])) { - $function = $field['settings']['allowed_values_function']; + if (!isset($allowed_values[$field_definition['cache_id']])) { + $function = $field_definition['settings']['allowed_values_function']; // If $cacheable is FALSE, then the allowed values are not statically // cached. See options_test_dynamic_values_callback() for an example of // generating dynamic and uncached values. $cacheable = TRUE; if (!empty($function)) { - $values = $function($field, $instance, $entity, $cacheable); + $values = $function($field_definition, $entity, $cacheable); } else { - $values = $field['settings']['allowed_values']; + $values = $field_definition['settings']['allowed_values']; } if ($cacheable) { - $allowed_values[$field['uuid']] = $values; + $allowed_values[$field_definition['cache_id']] = $values; } else { return $values; } } - return $allowed_values[$field['uuid']]; + return $allowed_values[$field_definition['cache_id']]; } /** @@ -400,7 +398,8 @@ function _options_values_in_use($field, $values) { * - 'list_illegal_value': The value is not part of the list of allowed values. */ function options_field_validate(EntityInterface $entity = NULL, $field, $instance, $langcode, $items, &$errors) { - $allowed_values = options_allowed_values($field, $instance, $entity); + $field_definition = $instance->getFieldDefinition(); + $allowed_values = options_allowed_values($field_definition, $entity); foreach ($items as $delta => $item) { if (!empty($item['value'])) { if (!empty($allowed_values) && !isset($allowed_values[$item['value']])) { @@ -426,7 +425,7 @@ function options_field_is_empty($item, $field) { /** * Implements hook_options_list(). */ -function options_options_list($field, $instance, $entity) { - return options_allowed_values($field, $instance, $entity); +function options_options_list(array $field_definition, $entity) { + return options_allowed_values($field_definition, $entity); } diff --git a/core/modules/options/tests/options_test.module b/core/modules/options/tests/options_test.module index 90f6bf1..715d3cd 100644 --- a/core/modules/options/tests/options_test.module +++ b/core/modules/options/tests/options_test.module @@ -10,7 +10,7 @@ /** * Allowed values callback. */ -function options_test_allowed_values_callback($field, $instance, $entity) { +function options_test_allowed_values_callback(array $field_definition, EntityInterface $entity) { $values = array( 'Group 1' => array( 0 => 'Zero', @@ -27,7 +27,7 @@ function options_test_allowed_values_callback($field, $instance, $entity) { /** * An entity-bound allowed values callback. */ -function options_test_dynamic_values_callback($field, $instance, EntityInterface $entity, &$cacheable) { +function options_test_dynamic_values_callback(array $field_definition, EntityInterface $entity, &$cacheable) { $cacheable = FALSE; // We need the values of the entity as keys. return drupal_map_assoc(array( diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php index 7ceb350..8a424cc 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\taxonomy\Plugin\field\widget\TaxonomyAutocompleteWidget. + * Contains \Drupal\taxonomy\Plugin\field\widget\TaxonomyAutocompleteWidget. */ namespace Drupal\taxonomy\Plugin\field\widget; @@ -32,7 +32,7 @@ class TaxonomyAutocompleteWidget extends WidgetBase { /** - * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm(). + * {@inheritdoc} */ public function settingsForm(array $form, array &$form_state) { $element['placeholder'] = array( @@ -45,11 +45,9 @@ public function settingsForm(array $form, array &$form_state) { } /** - * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement(). + * {@inheritdoc} */ public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { - $field = $this->field; - $tags = array(); foreach ($items as $item) { $tags[$item['tid']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']); @@ -57,7 +55,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr $element += array( '#type' => 'textfield', '#default_value' => taxonomy_implode_tags($tags), - '#autocomplete_path' => $this->getSetting('autocomplete_path') . '/' . $field['field_name'], + '#autocomplete_path' => $this->getSetting('autocomplete_path') . '/' . $this->field_definition['field_name'], '#size' => $this->getSetting('size'), '#placeholder' => $this->getSetting('placeholder'), '#maxlength' => 1024, @@ -68,16 +66,15 @@ public function formElement(array $items, $delta, array $element, $langcode, arr } /** - * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::massageFormValues() + * {@inheritdoc} */ public function massageFormValues(array $values, array $form, array &$form_state) { // Autocomplete widgets do not send their tids in the form, so we must detect // them here and process them independently. $items = array(); - $field = $this->field; // Collect candidate vocabularies. - foreach ($field['settings']['allowed_values'] as $tree) { + foreach ($this->field_definition['settings']['allowed_values'] as $tree) { if ($vocabulary = entity_load('taxonomy_vocabulary', $tree['vocabulary'])) { $vocabularies[$vocabulary->id()] = $vocabulary; } diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index d576b54..c8ab8a6 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -929,9 +929,9 @@ function taxonomy_field_widget_info_alter(&$info) { /** * Implements hook_options_list(). */ -function taxonomy_options_list($field, $instance, $entity) { - $function = !empty($field['settings']['options_list_callback']) ? $field['settings']['options_list_callback'] : 'taxonomy_allowed_values'; - return $function($field, $instance, $entity); +function taxonomy_options_list(array $field_definition, $entity) { + $function = !empty($field_definition['settings']['options_list_callback']) ? $field_definition['settings']['options_list_callback'] : 'taxonomy_allowed_values'; + return $function($field_definition, $entity); } /** @@ -1003,21 +1003,17 @@ function taxonomy_field_is_empty($item, $field) { /** * Returns the set of valid terms for a taxonomy field. * - * @param $field - * The field definition. - * @param $instance - * The instance definition. It is recommended to only use instance level - * properties to filter out values from a list defined by field level - * properties. + * @param array $field_definition + * The field definition array. * @param \Drupal\Core\Entity\EntityInterface $entity * The entity object the field is attached to. * * @return * The array of valid terms for this field, keyed by term id. */ -function taxonomy_allowed_values($field, $instance, EntityInterface $entity) { +function taxonomy_allowed_values(array $field_definition, EntityInterface $entity) { $options = array(); - foreach ($field['settings']['allowed_values'] as $tree) { + foreach ($field_definition['settings']['allowed_values'] as $tree) { if ($vocabulary = taxonomy_vocabulary_load($tree['vocabulary'])) { if ($terms = taxonomy_get_tree($vocabulary->id(), $tree['parent'], NULL, TRUE)) { foreach ($terms as $term) { diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextDefaultFormatter.php b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextDefaultFormatter.php index 4a96cc3..8db0662 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextDefaultFormatter.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextDefaultFormatter.php @@ -38,7 +38,7 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { $elements = array(); foreach ($items as $delta => $item) { - $output = text_sanitize($this->instance['settings']['text_processing'], $langcode, $item, 'value'); + $output = text_sanitize($this->field_definition['settings']['text_processing'], $langcode, $item, 'value'); $elements[$delta] = array('#markup' => $output); } diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextTrimmedFormatter.php b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextTrimmedFormatter.php index ba8b417..3cf23cf 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextTrimmedFormatter.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextTrimmedFormatter.php @@ -3,7 +3,7 @@ /** * @file * - * Definition of Drupal\text\Plugin\field\formatter\TextTrimmedFormatter. + * Contains \Drupal\text\Plugin\field\formatter\TextTrimmedFormatter. */ namespace Drupal\text\Plugin\field\formatter; @@ -40,7 +40,7 @@ class TextTrimmedFormatter extends FormatterBase { /** - * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm(). + * {@inheritdoc} */ public function settingsForm(array $form, array &$form_state) { $element['trim_length'] = array( @@ -54,7 +54,7 @@ public function settingsForm(array $form, array &$form_state) { } /** - * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsSummary(). + * {@inheritdoc} */ public function settingsSummary() { return t('Trim length: @trim_length', array( @@ -63,18 +63,18 @@ public function settingsSummary() { } /** - * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements(). + * {@inheritdoc} */ public function viewElements(EntityInterface $entity, $langcode, array $items) { $elements = array(); foreach ($items as $delta => $item) { if ($this->getPluginId() == 'text_summary_or_trimmed' && !empty($item['summary'])) { - $output = text_sanitize($this->instance['settings']['text_processing'], $langcode, $item, 'summary'); + $output = text_sanitize($this->field_definition['settings']['text_processing'], $langcode, $item, 'summary'); } else { - $output = text_sanitize($this->instance['settings']['text_processing'], $langcode, $item, 'value'); - $output = text_summary($output, $this->instance['settings']['text_processing'] ? $item['format'] : NULL, $this->getSetting('trim_length')); + $output = text_sanitize($this->field_definition['settings']['text_processing'], $langcode, $item, 'value'); + $output = text_summary($output, $this->field_definition['settings']['text_processing'] ? $item['format'] : NULL, $this->getSetting('trim_length')); } $elements[$delta] = array('#markup' => $output); } diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextareaWidget.php b/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextareaWidget.php index 496ea12..3e29b53 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextareaWidget.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextareaWidget.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\text\Plugin\field\widget\TextareaWidget. + * Contains \Drupal\text\Plugin\field\widget\TextareaWidget. */ namespace Drupal\text\Plugin\field\widget; @@ -30,7 +30,7 @@ class TextareaWidget extends WidgetBase { /** - * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm(). + * {@inheritdoc} */ public function settingsForm(array $form, array &$form_state) { $element['rows'] = array( @@ -50,7 +50,7 @@ public function settingsForm(array $form, array &$form_state) { } /** - * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement(). + * {@inheritdoc} */ public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { $main_widget = $element + array( @@ -61,7 +61,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr '#attributes' => array('class' => array('text-full')), ); - if ($this->instance['settings']['text_processing']) { + if ($this->field_definition['settings']['text_processing']) { $element = $main_widget; $element['#type'] = 'text_format'; $element['#format'] = isset($items[$delta]['format']) ? $items[$delta]['format'] : NULL; diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextareaWithSummaryWidget.php b/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextareaWithSummaryWidget.php index b3a68ac..46e9455 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextareaWithSummaryWidget.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextareaWithSummaryWidget.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\text\Plugin\field\widget\TextareaWithSummaryWidget. + * Contains \Drupal\text\Plugin\field\widget\TextareaWithSummaryWidget. */ namespace Drupal\text\Plugin\field\widget; @@ -30,12 +30,12 @@ class TextareaWithSummaryWidget extends TextareaWidget { /** - * Overrides TextareaWidget::formElement(). + * {@inheritdoc} */ function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { $element = parent::formElement($items, $delta, $element, $langcode, $form, $form_state); - $display_summary = !empty($items[$delta]['summary']) || $this->instance['settings']['display_summary']; + $display_summary = !empty($items[$delta]['summary']) || $this->field_definition['settings']['display_summary']; $element['summary'] = array( '#type' => $display_summary ? 'textarea' : 'value', '#default_value' => isset($items[$delta]['summary']) ? $items[$delta]['summary'] : NULL, @@ -55,7 +55,7 @@ function formElement(array $items, $delta, array $element, $langcode, array &$fo } /** - * Overrides TextareaWidget::errorElement(). + * {@inheritdoc} */ public function errorElement(array $element, array $error, array $form, array &$form_state) { switch ($error['error']) { diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextfieldWidget.php b/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextfieldWidget.php index a6e26b7..ef1accf 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextfieldWidget.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextfieldWidget.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\text\Plugin\field\widget\TextfieldWidget. + * Contains \Drupal\text\Plugin\field\widget\TextfieldWidget. */ namespace Drupal\text\Plugin\field\widget; @@ -30,7 +30,7 @@ class TextfieldWidget extends WidgetBase { /** - * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm(). + * {@inheritdoc} */ public function settingsForm(array $form, array &$form_state) { $element['size'] = array( @@ -50,7 +50,7 @@ public function settingsForm(array $form, array &$form_state) { } /** - * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement(). + * {@inheritdoc} */ public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { $main_widget = $element + array( @@ -58,11 +58,11 @@ public function formElement(array $items, $delta, array $element, $langcode, arr '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL, '#size' => $this->getSetting('size'), '#placeholder' => $this->getSetting('placeholder'), - '#maxlength' => $this->field['settings']['max_length'], + '#maxlength' => $this->field_definition['settings']['max_length'], '#attributes' => array('class' => array('text-full')), ); - if ($this->instance['settings']['text_processing']) { + if ($this->field_definition['settings']['text_processing']) { $element = $main_widget; $element['#type'] = 'text_format'; $element['#format'] = isset($items[$delta]['format']) ? $items[$delta]['format'] : NULL; diff --git a/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php b/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php index c3cf1ac..a72a4f7 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php +++ b/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php @@ -36,9 +36,8 @@ class ViewsSelection implements SelectionInterface { /** * Constructs a View selection handler. */ - public function __construct($field, $instance = NULL, EntityInterface $entity = NULL) { - $this->field = $field; - $this->instance = $instance; + public function __construct(array $field_definition, EntityInterface $entity = NULL) { + $this->field_definition = $field_definition; $this->entity = $entity; } @@ -117,13 +116,13 @@ public static function settingsForm(&$field, &$instance) { * Return TRUE if the view was initialized, FALSE otherwise. */ protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $limit = 0, $ids = NULL) { - $view_name = $this->instance['settings']['handler_settings']['view']['view_name']; - $display_name = $this->instance['settings']['handler_settings']['view']['display_name']; + $view_name = $this->field_definition['settings']['handler_settings']['view']['view_name']; + $display_name = $this->field_definition['settings']['handler_settings']['view']['display_name']; // Check that the view is valid and the display still exists. $this->view = views_get_view($view_name); if (!$this->view || !$this->view->access($display_name)) { - drupal_set_message(t('The reference view %view_name used in the %field_name field cannot be found.', array('%view_name' => $view_name, '%field_name' => $this->instance['label'])), 'warning'); + drupal_set_message(t('The reference view %view_name used in the %field_name field cannot be found.', array('%view_name' => $view_name, '%field_name' => $this->field_definition['label'])), 'warning'); return FALSE; } $this->view->setDisplay($display_name); @@ -143,8 +142,8 @@ protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $ * Implements \Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface::getReferencableEntities(). */ public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) { - $display_name = $this->instance['settings']['handler_settings']['view']['display_name']; - $arguments = $this->instance['settings']['handler_settings']['view']['arguments']; + $display_name = $this->field_definition['settings']['handler_settings']['view']['display_name']; + $arguments = $this->field_definition['settings']['handler_settings']['view']['arguments']; $result = array(); if ($this->initializeView($match, $match_operator, $limit)) { // Get the results. @@ -173,8 +172,8 @@ public function countReferencableEntities($match = NULL, $match_operator = 'CONT * Implements \Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface::validateReferencableEntities(). */ public function validateReferencableEntities(array $ids) { - $display_name = $this->instance['settings']['handler_settings']['view']['display_name']; - $arguments = $this->instance['settings']['handler_settings']['view']['arguments']; + $display_name = $this->field_definition['settings']['handler_settings']['view']['display_name']; + $arguments = $this->field_definition['settings']['handler_settings']['view']['arguments']; $result = array(); if ($this->initializeView(NULL, 'CONTAINS', 0, $ids)) { // Get the results.