Commit 5207055b authored by alexpott's avatar alexpott

Issue #2032393 by effulgentsia: Limit APIs to FieldDefinitionInterface when...

Issue #2032393 by effulgentsia: Limit APIs to FieldDefinitionInterface when FieldInstance isn't needed.
parent 1a0f40d5
......@@ -48,6 +48,14 @@ public function __construct(array $definition, $name = NULL, TypedDataInterface
$this->list[0] = $this->createItem(0);
}
/**
* {@inheritdoc}
*/
public function getFieldDefinition() {
// @todo https://drupal.org/node/1988612
return NULL;
}
/**
* {@inheritdoc}
*/
......
......@@ -27,6 +27,14 @@
*/
interface FieldInterface extends ListInterface, AccessibleInterface {
/**
* Gets the field definition.
*
* @return \Drupal\Core\Entity\Field\FieldDefinitionInterface
* The field definition.
*/
public function getFieldDefinition();
/**
* Filters out empty field items and re-numbers the item deltas.
*/
......
......@@ -36,6 +36,13 @@ public function __construct(array $definition, $name = NULL, TypedDataInterface
}
}
/**
* {@inheritdoc}
*/
public function getFieldDefinition() {
return $this->getParent()->getFieldDefinition();
}
/**
* Overrides \Drupal\Core\TypedData\TypedData::setValue().
*
......
......@@ -23,6 +23,14 @@
*/
interface FieldItemInterface extends ComplexDataInterface {
/**
* Gets the field definition.
*
* @return \Drupal\Core\Entity\Field\FieldDefinitionInterface
* The field definition.
*/
public function getFieldDefinition();
/**
* Magic method: Gets a property value.
*
......
......@@ -8,7 +8,7 @@
namespace Drupal\edit;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\field\Plugin\Core\Entity\FieldInstance;
use Drupal\Core\Entity\Field\FieldDefinitionInterface;
/**
* Defines an interface for in-place editors (Create.js PropertyEditor widgets).
......@@ -21,15 +21,15 @@ interface EditPluginInterface extends PluginInspectionInterface {
/**
* Checks whether this editor is compatible with a given field instance.
*
* @param \Drupal\field\Plugin\Core\Entity\FieldInstance $instance
* The field instance of the field being edited.
* @param \Drupal\Core\Entity\Field\FieldDefinitionInterface $field_definition
* The field definition of the field being edited.
* @param array $items
* The field's item values.
*
* @return bool
* TRUE if it is compatible, FALSE otherwise.
*/
public function isCompatible(FieldInstance $instance, array $items);
public function isCompatible(FieldDefinitionInterface $field_definition, array $items);
/**
* Generates metadata that is needed specifically for this editor.
......@@ -37,8 +37,8 @@ public function isCompatible(FieldInstance $instance, array $items);
* Will only be called by \Drupal\edit\MetadataGeneratorInterface::generate()
* when the passed in field instance & item values will use this editor.
*
* @param \Drupal\field\Plugin\Core\Entity\FieldInstance $instance
* The field instance of the field being edited.
* @param \Drupal\Core\Entity\Field\FieldDefinitionInterface $field_definition
* The field definition of the field being edited.
* @param array $items
* The field's item values.
*
......@@ -46,7 +46,7 @@ public function isCompatible(FieldInstance $instance, array $items);
* A keyed array with metadata. Each key should be prefixed with the plugin
* ID of the editor.
*/
public function getMetadata(FieldInstance $instance, array $items);
public function getMetadata(FieldDefinitionInterface $field_definition, array $items);
/**
* Returns the attachments for this editor.
......
......@@ -9,7 +9,7 @@
use Drupal\Component\Plugin\PluginBase;
use Drupal\edit\EditPluginInterface;
use Drupal\field\Plugin\Core\Entity\FieldInstance;
use Drupal\Core\Entity\Field\FieldDefinitionInterface;
/**
* Defines a base editor implementation.
......@@ -17,9 +17,9 @@
abstract class EditorBase extends PluginBase implements EditPluginInterface {
/**
* Implements \Drupal\edit\EditPluginInterface::getMetadata().
* {@inheritdoc}
*/
function getMetadata(FieldInstance $instance, array $items) {
function getMetadata(FieldDefinitionInterface $field_definition, array $items) {
return array();
}
......
......@@ -9,7 +9,7 @@
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\field\Plugin\Core\Entity\FieldInstance;
use Drupal\Core\Entity\Field\FieldDefinitionInterface;
/**
* Selects an in-place editor (an Editor plugin) for a field.
......@@ -43,7 +43,7 @@ public function __construct(PluginManagerInterface $editor_manager) {
/**
* {@inheritdoc}
*/
public function getEditor($formatter_type, FieldInstance $instance, array $items) {
public function getEditor($formatter_type, FieldDefinitionInterface $field_definition, array $items) {
// Build a static cache of the editors that have registered themselves as
// alternatives to a certain editor.
if (!isset($this->alternatives)) {
......@@ -80,7 +80,7 @@ public function getEditor($formatter_type, FieldInstance $instance, array $items
// Make a choice.
foreach ($editor_choices as $editor_id) {
$editor = $this->editorManager->createInstance($editor_id);
if ($editor->isCompatible($instance, $items)) {
if ($editor->isCompatible($field_definition, $items)) {
return $editor_id;
}
}
......
......@@ -7,7 +7,7 @@
namespace Drupal\edit;
use Drupal\field\Plugin\Core\Entity\FieldInstance;
use Drupal\Core\Entity\Field\FieldDefinitionInterface;
/**
* Interface for selecting an in-place editor (an Editor plugin) for a field.
......@@ -19,15 +19,15 @@ interface EditorSelectorInterface {
*
* @param string $formatter_type
* The field's formatter type name.
* @param \Drupal\field\Plugin\Core\Entity\FieldInstance $instance
* The field's instance info.
* @param \Drupal\Core\Entity\Field\FieldDefinitionInterface $field_definition
* The field definition.
* @param array $items
* The field's item values.
*
* @return string|NULL
* The editor to use, or NULL to not enable in-place editing.
*/
public function getEditor($formatter_type, FieldInstance $instance, array $items);
public function getEditor($formatter_type, FieldDefinitionInterface $instance, array $items);
/**
* Returns the attachments for all editors.
......
......@@ -9,9 +9,9 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\field\Plugin\Core\Entity\FieldInstance;
use Drupal\Core\Entity\Field\FieldDefinitionInterface;
use Drupal\edit\Access\EditEntityFieldAccessCheckInterface;
use Drupal\field\FieldInstanceInterface;
/**
* Generates in-place editing metadata for an entity field.
......@@ -67,8 +67,8 @@ public function generateEntity(EntityInterface $entity, $langcode) {
/**
* {@inheritdoc}
*/
public function generateField(EntityInterface $entity, FieldInstance $instance, $langcode, $view_mode) {
$field_name = $instance['field_name'];
public function generateField(EntityInterface $entity, FieldDefinitionInterface $field_definition, $langcode, $view_mode) {
$field_name = $field_definition->getFieldName();
// Early-return if user does not have access.
$access = $this->accessChecker->accessEditEntityField($entity, $field_name);
......@@ -77,15 +77,15 @@ public function generateField(EntityInterface $entity, FieldInstance $instance,
}
// Early-return if no editor is available.
$formatter_id = entity_get_render_display($entity, $view_mode)->getRenderer($instance['field_name'])->getPluginId();
$formatter_id = entity_get_render_display($entity, $view_mode)->getRenderer($field_name)->getPluginId();
$items = $entity->getTranslation($langcode)->get($field_name)->getValue();
$editor_id = $this->editorSelector->getEditor($formatter_id, $instance, $items);
$editor_id = $this->editorSelector->getEditor($formatter_id, $field_definition, $items);
if (!isset($editor_id)) {
return array('access' => FALSE);
}
// Gather metadata, allow the editor to add additional metadata of its own.
$label = $instance['label'];
$label = $field_definition->getFieldLabel();
$editor = $this->editorManager->createInstance($editor_id);
$metadata = array(
'label' => check_plain($label),
......@@ -93,7 +93,7 @@ public function generateField(EntityInterface $entity, FieldInstance $instance,
'editor' => $editor_id,
'aria' => t('Entity @type @id, field @field', array('@type' => $entity->entityType(), '@id' => $entity->id(), '@field' => $label)),
);
$custom_metadata = $editor->getMetadata($instance, $items);
$custom_metadata = $editor->getMetadata($field_definition, $items);
if (count($custom_metadata)) {
$metadata['custom'] = $custom_metadata;
}
......
......@@ -8,7 +8,7 @@
namespace Drupal\edit;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Plugin\Core\Entity\FieldInstance;
use Drupal\Core\Entity\Field\FieldDefinitionInterface;
/**
* Interface for generating in-place editing metadata.
......@@ -33,8 +33,8 @@ public function generateEntity(EntityInterface $entity, $langcode);
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being edited.
* @param \Drupal\field\Plugin\Core\Entity\FieldInstance $instance
* The field instance of the field being edited.
* @param \Drupal\Core\Entity\Field\FieldDefinitionInterface $field_definition
* The field definition of the field being edited.
* @param string $langcode
* The name of the language for which the field is being edited.
* @param string $view_mode
......@@ -47,6 +47,6 @@ public function generateEntity(EntityInterface $entity, $langcode);
* - aria: the ARIA label.
* - custom: (optional) any additional metadata that the editor provides.
*/
public function generateField(EntityInterface $entity, FieldInstance $instance, $langcode, $view_mode);
public function generateField(EntityInterface $entity, FieldDefinitionInterface $field_definition, $langcode, $view_mode);
}
......@@ -9,7 +9,7 @@
use Drupal\edit\EditorBase;
use Drupal\edit\Annotation\InPlaceEditor;
use Drupal\field\Plugin\Core\Entity\FieldInstance;
use Drupal\Core\Entity\Field\FieldDefinitionInterface;
/**
* Defines the direct editor.
......@@ -21,20 +21,18 @@
class DirectEditor extends EditorBase {
/**
* Implements \Drupal\edit\EditPluginInterface::isCompatible().
* {@inheritdoc}
*
* @todo The processed text logic is too coupled to text fields. Figure out
* how to generalize to other textual field types.
*/
function isCompatible(FieldInstance $instance, array $items) {
$field = field_info_field($instance['field_name']);
function isCompatible(FieldDefinitionInterface $field_definition, array $items) {
// This editor is incompatible with multivalued fields.
if ($field['cardinality'] != 1) {
if ($field_definition->getFieldCardinality() != 1) {
return FALSE;
}
// This editor is incompatible with processed ("rich") text fields.
elseif (!empty($instance['settings']['text_processing'])) {
elseif ($field_definition->getFieldSetting('text_processing')) {
return FALSE;
}
else {
......
......@@ -9,7 +9,7 @@
use Drupal\edit\EditorBase;
use Drupal\edit\Annotation\InPlaceEditor;
use Drupal\field\Plugin\Core\Entity\FieldInstance;
use Drupal\Core\Entity\Field\FieldDefinitionInterface;
/**
* Defines the form editor.
......@@ -21,9 +21,9 @@
class FormEditor extends EditorBase {
/**
* Implements \Drupal\edit\EditPluginInterface::isCompatible().
* {@inheritdoc}
*/
function isCompatible(FieldInstance $instance, array $items) {
function isCompatible(FieldDefinitionInterface $field_definition, array $items) {
return TRUE;
}
......
......@@ -9,7 +9,7 @@
use Drupal\edit\EditorBase;
use Drupal\edit\Annotation\InPlaceEditor;
use Drupal\field\Plugin\Core\Entity\FieldInstance;
use Drupal\Core\Entity\Field\FieldDefinitionInterface;
/**
* Defines the wysiwyg editor.
......@@ -22,19 +22,17 @@
class WysiwygEditor extends EditorBase {
/**
* Implements \Drupal\edit\EditPluginInterface::isCompatible().
* {@inheritdoc}
*/
function isCompatible(FieldInstance $instance, array $items) {
$field = field_info_field($instance['field_name']);
function isCompatible(FieldDefinitionInterface $field_definition, array $items) {
// This editor is incompatible with multivalued fields.
if ($field['cardinality'] != 1) {
if ($field_definition->getFieldCardinality() != 1) {
return FALSE;
}
// This editor is compatible with processed ("rich") text fields; but only
// if there is a currently active text format and that text format is the
// 'full_html' text format.
elseif (!empty($instance['settings']['text_processing'])) {
elseif ($field_definition->getFieldSetting('text_processing')) {
$format_id = $items[0]['format'];
if (isset($format_id) && $format_id === 'full_html') {
return TRUE;
......@@ -44,9 +42,9 @@ function isCompatible(FieldInstance $instance, array $items) {
}
/**
* Implements \Drupal\edit\EditPluginInterface::getMetadata().
* {@inheritdoc}
*/
function getMetadata(FieldInstance $instance, array $items) {
function getMetadata(FieldDefinitionInterface $field_definition, array $items) {
$format_id = $items[0]['format'];
$metadata['format'] = $format_id;
return $metadata;
......
......@@ -11,7 +11,7 @@
use Drupal\edit\Annotation\InPlaceEditor;
use Drupal\Core\Annotation\Translation;
use Drupal\edit\EditPluginInterface;
use Drupal\field\Plugin\Core\Entity\FieldInstance;
use Drupal\Core\Entity\Field\FieldDefinitionInterface;
/**
* Defines the formatted text editor.
......@@ -24,19 +24,17 @@
class Editor extends PluginBase implements EditPluginInterface {
/**
* Implements \Drupal\edit\Plugin\EditPluginInterface::isCompatible().
* {@inheritdoc}
*/
function isCompatible(FieldInstance $instance, array $items) {
$field = field_info_field($instance['field_name']);
function isCompatible(FieldDefinitionInterface $field_definition, array $items) {
// This editor is incompatible with multivalued fields.
if ($field['cardinality'] != 1) {
if ($field_definition->getFieldCardinality() != 1) {
return FALSE;
}
// This editor is compatible with processed ("rich") text fields; but only
// if there is a currently active text format, that text format has an
// associated editor and that editor supports inline editing.
elseif (!empty($instance['settings']['text_processing'])) {
elseif ($field_definition->getFieldSetting('text_processing')) {
$format_id = $items[0]['format'];
if (isset($format_id) && $editor = editor_load($format_id)) {
$definition = \Drupal::service('plugin.manager.editor')->getDefinition($editor->editor);
......@@ -50,9 +48,9 @@ function isCompatible(FieldInstance $instance, array $items) {
}
/**
* Implements \Drupal\edit\Plugin\EditPluginInterface::getMetadata().
* {@inheritdoc}
*/
function getMetadata(FieldInstance $instance, array $items) {
function getMetadata(FieldDefinitionInterface $field_definition, array $items) {
$format_id = $items[0]['format'];
$metadata['format'] = $format_id;
$metadata['formatHasTransformations'] = $this->textFormatHasTransformationFilters($format_id);
......
......@@ -44,6 +44,13 @@ public function getInstance() {
return $this->instance;
}
/**
* {@inheritdoc}
*/
public function getFieldDefinition() {
return $this->getInstance();
}
/**
* {@inheritdoc}
*/
......@@ -52,13 +59,13 @@ public function getConstraints() {
// Check that the number of values doesn't exceed the field cardinality. For
// form submitted values, this can only happen with 'multiple value'
// widgets.
$cardinality = $this->getInstance()->getField()->cardinality;
$cardinality = $this->getFieldDefinition()->getFieldCardinality();
if ($cardinality != FIELD_CARDINALITY_UNLIMITED) {
$constraints[] = \Drupal::typedData()
->getValidationConstraintManager()
->create('Count', array(
'max' => $cardinality,
'maxMessage' => t('%name: this field cannot hold more than @count values.', array('%name' => $this->getInstance()->label, '@count' => $cardinality)),
'maxMessage' => t('%name: this field cannot hold more than @count values.', array('%name' => $this->getFieldDefinition()->getFieldLabel(), '@count' => $cardinality)),
));
}
......
......@@ -60,6 +60,27 @@ public static function schema(Field $field) {
);
}
/**
* {@inheritdoc}
*/
public function getConstraints() {
$constraint_manager = \Drupal::typedData()->getValidationConstraintManager();
$constraints = parent::getConstraints();
if ($max_length = $this->getFieldDefinition()->getFieldSetting('max_length')) {
$constraints[] = $constraint_manager->create('ComplexData', array(
'value' => array(
'Length' => array(
'max' => $max_length,
'maxMessage' => t('%name: the text may not be longer than @max characters.', array('%name' => $this->getFieldDefinition()->getFieldLabel(), '@max' => $max_length)),
)
),
));
}
return $constraints;
}
/**
* {@inheritdoc}
*/
......
......@@ -67,28 +67,6 @@ public function isEmpty() {
return $value === NULL || $value === '';
}
/**
* {@inheritdoc}
*/
public function getConstraints() {
$constraint_manager = \Drupal::typedData()->getValidationConstraintManager();
$constraints = parent::getConstraints();
if (!empty($this->getInstance()->getField()->settings['max_length'])) {
$max_length = $this->getInstance()->getField()->settings['max_length'];
$constraints[] = $constraint_manager->create('ComplexData', array(
'value' => array(
'Length' => array(
'max' => $max_length,
'maxMessage' => t('%name: the text may not be longer than @max characters.', array('%name' => $this->getInstance()->label, '@max' => $max_length)),
)
),
));
}
return $constraints;
}
/**
* {@inheritdoc}
*/
......@@ -96,12 +74,13 @@ public function prepareCache() {
// Where possible, generate the sanitized version of each field early so
// that it is cached in the field cache. This avoids the need to look up the
// field in the filter cache separately.
if (!$this->getInstance()->settings['text_processing'] || filter_format_allowcache($this->get('format')->getValue())) {
$text_processing = $this->getFieldDefinition()->getFieldSetting('text_processing');
if (!$text_processing || filter_format_allowcache($this->get('format')->getValue())) {
$itemBC = $this->getValue();
$langcode = $this->getParent()->getParent()->language()->id;
$this->set('safe_value', text_sanitize($this->getInstance()->settings['text_processing'], $langcode, $itemBC, 'value'));
$this->set('safe_value', text_sanitize($text_processing, $langcode, $itemBC, 'value'));
if ($this->getType() == 'field_item:text_with_summary') {
$this->set('safe_summary', text_sanitize($this->getInstance()->settings['text_processing'], $langcode, $itemBC, 'summary'));
$this->set('safe_summary', text_sanitize($text_processing, $langcode, $itemBC, 'summary'));
}
}
}
......
......@@ -103,28 +103,6 @@ public function isEmpty() {
return parent::isEmpty() && ($value === NULL || $value === '');
}
/**
* {@inheritdoc}
*/
public function getConstraints() {
$constraint_manager = \Drupal::typedData()->getValidationConstraintManager();
$constraints = parent::getConstraints();
if (!empty($this->getInstance()->getField()->settings['max_length'])) {
$max_length = $this->getInstance()->getField()->settings['max_length'];
$constraints[] = $constraint_manager->create('ComplexData', array(
'summary' => array(
'Length' => array(
'max' => $max_length,
'maxMessage' => t('%name: the summary may not be longer than @max characters.', array('%name' => $this->getInstance()->label, '@max' => $max_length)),
)
),
));
}
return $constraints;
}
/**
* {@inheritdoc}
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment