Commit daad7b1c authored by alexpott's avatar alexpott

Issue #2571533 by damiankloip, yched: Allow setting custom storage on FieldStorageConfig

parent fa0af0b3
......@@ -15,3 +15,4 @@ cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: true
custom_storage: false
......@@ -15,3 +15,4 @@ cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: true
custom_storage: false
......@@ -50,6 +50,9 @@ field.storage.*.*:
persist_with_no_fields:
type: boolean
label: 'Persist field storage with no fields'
custom_storage:
type: boolean
label: 'Enable custom storage'
field.field.*.*.*:
type: field_config_base
......
<?php
/**
* @file
* Post update functions for Field.
*/
use Drupal\field\Entity\FieldStorageConfig;
/**
* @addtogroup updates-8.0.0-beta
* @{
*/
/**
* Re-save all field storage config objects to add 'custom_storage' property.
*/
function field_post_update_save_custom_storage_property() {
foreach (FieldStorageConfig::loadMultiple() as $field_storage_config) {
$field_storage_config->save();
}
return t('All field storage configuration objects re-saved.');
}
/**
* @} End of "addtogroup updates-8.0.0-beta".
*/
......@@ -42,6 +42,7 @@
* "translatable",
* "indexes",
* "persist_with_no_fields",
* "custom_storage",
* }
* )
*/
......@@ -160,6 +161,13 @@ class FieldStorageConfig extends ConfigEntityBase implements FieldStorageConfigI
*/
protected $persist_with_no_fields = FALSE;
/**
* A boolean indicating whether or not the field item uses custom storage.
*
* @var bool
*/
public $custom_storage = FALSE;
/**
* The custom storage indexes for the field data storage.
*
......@@ -436,6 +444,7 @@ public function getSchema() {
$schema = $class::schema($this);
// Fill in default values for optional entries.
$schema += array(
'columns' => array(),
'unique keys' => array(),
'indexes' => array(),
'foreign keys' => array(),
......@@ -455,7 +464,7 @@ public function getSchema() {
* {@inheritdoc}
*/
public function hasCustomStorage() {
return FALSE;
return $this->custom_storage;
}
/**
......
......@@ -10,6 +10,7 @@
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Field\FieldException;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
......@@ -137,6 +138,44 @@ function testCreateField() {
// TODO: test other failures.
}
/**
* Test creating a field with custom storage set.
*/
public function testCreateFieldCustomStorage() {
$field_name = Unicode::strtolower($this->randomMachineName());
\Drupal::state()->set('field_test_custom_storage', $field_name);
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'test_field',
'custom_storage' => TRUE,
]);
$field_storage->save();
$field = FieldConfig::create([
'field_name' => $field_storage->getName(),
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
]);
$field->save();
\Drupal::entityManager()->clearCachedFieldDefinitions();
// Check that no table has been created for the field.
$this->assertFalse(\Drupal::database()->schema()->tableExists('entity_test__' . $field_storage->getName()));
// Save an entity with a value in the custom storage field and verify no
// data is retrieved on load.
$entity = EntityTest::create(['name' => $this->randomString(), $field_name => 'Test value']);
$this->assertIdentical('Test value', $entity->{$field_name}->value, 'The test value is set on the field.');
$entity->save();
$entity = EntityTest::load($entity->id());
$this->assertNull($entity->{$field_name}->value, 'The loaded entity field value is NULL.');
}
/**
* Test reading back a field definition.
*/
......
......@@ -17,3 +17,4 @@ dependencies:
- entity_test
- text
persist_with_no_fields: false
custom_storage: false
......@@ -17,3 +17,4 @@ dependencies:
- entity_test
- text
persist_with_no_fields: false
custom_storage: false
......@@ -18,3 +18,4 @@ dependencies:
- entity_test
- text
persist_with_no_fields: false
custom_storage: false
......@@ -18,3 +18,4 @@ dependencies:
- entity_test
- text
persist_with_no_fields: false
custom_storage: false
......@@ -16,3 +16,4 @@ cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
......@@ -16,3 +16,4 @@ cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
......@@ -14,3 +14,4 @@ cardinality: 1
translatable: false
indexes: { }
persist_with_no_fields: false
custom_storage: false
......@@ -15,3 +15,4 @@ cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: true
custom_storage: false
......@@ -23,3 +23,4 @@ cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
......@@ -59,8 +59,11 @@ public function testPostUpdate() {
$this->assertIdentical($updates, \Drupal::state()->get('post_update_test_execution', []));
$key_value = \Drupal::keyValue('post_update');
array_unshift($updates, 'views_post_update_update_cacheability_metadata');
array_unshift($updates, 'block_post_update_disable_blocks_with_missing_contexts');
$updates = array_merge([
'block_post_update_disable_blocks_with_missing_contexts',
'field_post_update_save_custom_storage_property',
'views_post_update_update_cacheability_metadata',
], $updates);
$this->assertEqual($updates, $key_value->get('existing_updates'));
$this->drupalGet('update.php/selection');
......
......@@ -16,3 +16,4 @@ cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
......@@ -27,3 +27,4 @@ indexes:
target_id:
- target_id
persist_with_no_fields: false
custom_storage: false
......@@ -16,3 +16,4 @@ cardinality: -1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
......@@ -27,3 +27,4 @@ indexes:
target_id:
- target_id
persist_with_no_fields: false
custom_storage: false
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