Commit 03117bc3 authored by catch's avatar catch

Issue #2274517 by Wim Leers: Remove \Drupal\Core\Field\PrepareCacheInterface.

parent abfc190c
......@@ -9,7 +9,6 @@
use Drupal\Component\Utility\String;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Field\PrepareCacheInterface;
use Drupal\field\FieldConfigInterface;
use Drupal\field\FieldInstanceConfigInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -151,17 +150,7 @@ protected function loadFieldItems(array $entities) {
$translation = $entity->getTranslation($langcode);
foreach ($translation as $field_name => $items) {
if ($items->getFieldDefinition() instanceof FieldInstanceConfigInterface && !$items->isEmpty()) {
foreach ($items as $delta => $item) {
// If the field item needs to prepare the cache data, call the
// corresponding method, otherwise use the values as cache
// data.
if ($item instanceof PrepareCacheInterface) {
$data[$langcode][$field_name][$delta] = $item->getCacheData();
}
else {
$data[$langcode][$field_name][$delta] = $item->getValue();
}
}
$data[$langcode][$field_name] = $items->getValue();
}
}
}
......
<?php
/**
* @file
* Contains \Drupal\Core\Field\PrepareCacheInterface.
*/
namespace Drupal\Core\Field;
/**
* Interface for preparing field values before they enter cache.
*
* If a field type implements this interface, this method will be used instead
* of the regular getValue() to collect the data to include in the cache of
* field values.
*/
interface PrepareCacheInterface {
/**
* Returns the data to store in the field cache.
*
* This method is called if the entity type has field caching enabled, when an
* entity is loaded and no existing cache entry was found in the field cache.
*
* This method should never trigger the loading of fieldable entities, since
* this is likely to cause infinite recursions. A common workaround is to
* provide a base formatter class implementing the prepareView() method
* instead.
*
* The recommended way to implement it is to provide a computed field item
* property that can accepts setting a value through setValue(). See
* \Drupal\text\Plugin\Field\FieldType\TextItemBase and the corresponding
* computed property Drupal\text\TextProcessed for an example.
*/
public function getCacheData();
}
......@@ -8,7 +8,6 @@
namespace Drupal\datetime\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\PrepareCacheInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\FieldItemBase;
......@@ -24,7 +23,7 @@
* list_class = "\Drupal\datetime\Plugin\Field\FieldType\DateTimeFieldItemList"
* )
*/
class DateTimeItem extends FieldItemBase implements PrepareCacheInterface {
class DateTimeItem extends FieldItemBase {
/**
* {@inheritdoc}
......@@ -101,21 +100,6 @@ public function settingsForm(array $form, array &$form_state, $has_data) {
return $element;
}
/**
* {@inheritdoc}
*/
public function getCacheData() {
$data = $this->getValue();
// The function generates a Date object for each field early so that it is
// cached in the field cache. This avoids the need to generate the object
// later. The date will be retrieved in UTC, the local timezone adjustment
// must be made in real time, based on the preferences of the site and user.
if (!empty($data['value'])) {
$data['date'] = $this->date;
}
return $data;
}
/**
* {@inheritdoc}
*/
......
......@@ -51,11 +51,6 @@ function testFieldAttachSaveLoad() {
$this->createFieldWithInstance('', $entity_type);
$cardinality = $this->field->getCardinality();
// Configure the instance so that we test
// \Drupal\field_test\Plugin\Field\FieldType\TestItem::getCacheData().
$this->instance->settings['test_cached_data'] = TRUE;
$this->instance->save();
// TODO : test empty values filtering and "compression" (store consecutive deltas).
// Preparation: create three revisions and store them in $revision array.
$values = array();
......@@ -80,10 +75,6 @@ function testFieldAttachSaveLoad() {
for ($delta = 0; $delta < $cardinality; $delta++) {
// The field value loaded matches the one inserted or updated.
$this->assertEqual($entity->{$this->field_name}[$delta]->value , $values[$current_revision][$delta]['value'], format_string('Current revision: expected value %delta was found.', array('%delta' => $delta)));
// The value added in
// \Drupal\field_test\Plugin\Field\FieldType\TestItem::getCacheData() is
// found.
$this->assertEqual($entity->{$this->field_name}[$delta]->additional_key, 'additional_value', format_string('Current revision: extra information for value %delta was found', array('%delta' => $delta)));
}
// Confirm each revision loads the correct data.
......@@ -134,11 +125,6 @@ function testFieldAttachLoadMultiple() {
'field_name' => $field_names[$i],
'entity_type' => $entity_type,
'bundle' => $bundles[$bundle],
// Configure the instance so that we test
// \Drupal\field_test\Plugin\Field\FieldType\TestItem::getCacheData().
'settings' => array(
'test_cached_data' => TRUE,
),
))->save();
}
}
......@@ -169,8 +155,6 @@ function testFieldAttachLoadMultiple() {
}
// The field value loaded matches the one inserted.
$this->assertEqual($entity->{$field_name}->value, $values[$index][$field_name], format_string('Entity %index: expected value was found.', array('%index' => $index)));
// The value added in hook_field_load() is found.
$this->assertEqual($entity->{$field_name}->additional_key, 'additional_value', format_string('Entity %index: extra information was found', array('%index' => $index)));
}
}
}
......
......@@ -8,7 +8,6 @@
namespace Drupal\field_test\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\PrepareCacheInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\FieldItemBase;
......@@ -23,7 +22,7 @@
* default_formatter = "field_test_default"
* )
*/
class TestItem extends FieldItemBase implements PrepareCacheInterface {
class TestItem extends FieldItemBase {
/**
* {@inheritdoc}
......@@ -104,20 +103,6 @@ public function instanceSettingsForm(array $form, array &$form_state) {
return $form;
}
/**
* {@inheritdoc}
*/
public function getCacheData() {
// To keep the test non-intrusive, only act for instances with the
// 'test_cached_data' setting explicitly set to TRUE. Also don't add
// anything on empty values.
if ($this->getSetting('test_cached_data') && !$this->isEmpty()) {
// Set the additional value so that getValue() will return it.
$this->additional_key = 'additional_value';
}
return $this->getValue();
}
/**
* {@inheritdoc}
*/
......
......@@ -9,13 +9,12 @@
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\PrepareCacheInterface;
use Drupal\Core\TypedData\DataDefinition;
/**
* Base class for 'text' configurable field types.
*/
abstract class TextItemBase extends FieldItemBase implements PrepareCacheInterface {
abstract class TextItemBase extends FieldItemBase {
/**
* {@inheritdoc}
......@@ -64,26 +63,6 @@ public function isEmpty() {
return $value === NULL || $value === '';
}
/**
* {@inheritdoc}
*/
public function getCacheData() {
$data = $this->getValue();
// Where possible, generate the processed (sanitized) version of each
// textual property (e.g., 'value', 'summary') within this field item early
// so that it is cached in the field cache. This avoids the need to look up
// the sanitized value in the filter cache separately.
$text_processing = $this->getSetting('text_processing');
if (!$text_processing || filter_format_allowcache($this->get('format')->getValue())) {
foreach ($this->definition->getPropertyDefinitions() as $property => $definition) {
if ($definition->getClass() == '\Drupal\text\TextProcessed') {
$data[$property] = $this->get($property)->getValue();
}
}
}
return $data;
}
/**
* {@inheritdoc}
*/
......
......@@ -102,71 +102,6 @@ public function testCrudAndUpdate() {
$this->assertEqual($entity->summary_field->summary_processed, $summary);
}
/**
* Tests that the processed values are cached.
*/
function testProcessedCache() {
// Use an entity type that has caching enabled.
$entity_type = 'entity_test_rev';
$this->createField($entity_type);
// Create an entity with a summary and a text format.
$entity = entity_create($entity_type);
$entity->summary_field->value = $value = $this->randomName();
$entity->summary_field->summary = $summary = $this->randomName();
$entity->summary_field->format = 'plain_text';
$entity->name->value = $this->randomName();
$entity->save();
// Check that the processed values are correctly computed.
$this->assertEqual($entity->summary_field->processed, $value);
$this->assertEqual($entity->summary_field->summary_processed, $summary);
// Load the entity and check that the field cache contains the expected
// data.
$entity = entity_load($entity_type, $entity->id());
$cache = \Drupal::cache('entity')->get("field:$entity_type:" . $entity->id());
$this->assertEqual($cache->data, array(
Language::LANGCODE_NOT_SPECIFIED => array(
'summary_field' => array(
0 => array(
'value' => $value,
'summary' => $summary,
'format' => 'plain_text',
'processed' => $value,
'summary_processed' => $summary,
),
),
),
));
// Inject fake processed values into the cache to make sure that these are
// used as-is and not re-calculated when the entity is loaded.
$data = array(
Language::LANGCODE_NOT_SPECIFIED => array(
'summary_field' => array(
0 => array(
'value' => $value,
'summary' => $summary,
'format' => 'plain_text',
'processed' => 'Cached processed value',
'summary_processed' => 'Cached summary processed value',
),
),
),
);
\Drupal::cache('entity')->set("field:$entity_type:" . $entity->id(), $data);
$entity = entity_load($entity_type, $entity->id(), TRUE);
$this->assertEqual($entity->summary_field->processed, 'Cached processed value');
$this->assertEqual($entity->summary_field->summary_processed, 'Cached summary processed value');
// Change the format, this should update the processed properties.
$entity->summary_field->format = 'no_filters';
$this->assertEqual($entity->summary_field->processed, $value);
$this->assertEqual($entity->summary_field->summary_processed, $summary);
}
/**
* Creates a text_with_summary field and field instance.
*
......
......@@ -158,20 +158,6 @@ function text_summary($text, $format = NULL, $size = NULL) {
return $summary;
}
/**
* Implements hook_filter_format_update().
*/
function text_filter_format_update($format) {
\Drupal::entityManager()->clearCachedFieldDefinitions();
}
/**
* Implements hook_filter_format_disable().
*/
function text_filter_format_disable($format) {
\Drupal::entityManager()->clearCachedFieldDefinitions();
}
/**
* Implements hook_field_formatter_info_alter().
*/
......
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