diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php index 57e4243..34640d4 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php @@ -235,7 +235,7 @@ protected function doSave($id, EntityInterface $entity) { } // Retrieve the desired properties and set them in config. - foreach ($entity->toArray() as $key => $value) { + foreach ($this->preSaveData($entity->toArray(), $entity) as $key => $value) { $config->set($key, $value); } $config->save(); @@ -244,6 +244,21 @@ protected function doSave($id, EntityInterface $entity) { } /** + * Alter data for the storage environment right before saving. + * + * @param array $data + * Raw configuration data being saved. + * @param \Drupal\Core\Entity\EntityInterface $entity + * Entity being saved. + * + * @return array + * Data to save with any modifications necessary for storage performed. + */ + protected function preSaveData($data, EntityInterface $entity) { + return $data; + } + + /** * {@inheritdoc} */ protected function has($id, EntityInterface $entity) { diff --git a/core/lib/Drupal/Core/Field/FieldItemBase.php b/core/lib/Drupal/Core/Field/FieldItemBase.php index 296c468..114b2ee 100644 --- a/core/lib/Drupal/Core/Field/FieldItemBase.php +++ b/core/lib/Drupal/Core/Field/FieldItemBase.php @@ -255,4 +255,24 @@ public function instanceSettingsForm(array $form, array &$form_state) { return array(); } + /** + * {@inheritdoc} + */ + public function preSaveSettings(array &$settings) { } + + /** + * {@inheritdoc} + */ + public function postLoadSettings(array &$settings) { } + + /** + * {@inheritdoc} + */ + public function preSaveInstanceSettings(array &$settings) { } + + /** + * {@inheritdoc} + */ + public function postLoadInstanceSettings(array &$settings) { } + } diff --git a/core/lib/Drupal/Core/Field/FieldItemInterface.php b/core/lib/Drupal/Core/Field/FieldItemInterface.php index ee27996..d70d366 100644 --- a/core/lib/Drupal/Core/Field/FieldItemInterface.php +++ b/core/lib/Drupal/Core/Field/FieldItemInterface.php @@ -183,6 +183,48 @@ public function view($display_options = array()); public function preSave(); /** + * Defines behavior for transforming field settings before being saved. + * + * May be used to alter settings to transform them to a storage-friendly form. + * + * @param array $settings + * Field settings. + */ + public function preSaveSettings(array &$settings); + + /** + * Defines custom behavior for transforming field settings when loading. + * + * May be used to alter settings to transform them from a storage-friendly + * form. + * + * @param array $settings + * Field settings. + */ + public function postLoadSettings(array &$settings); + + /** + * Defines behavior for transforming instance settings before being saved. + * + * May be used to alter settings to transform them to a storage-friendly form. + * + * @param array $settings + * Field settings. + */ + public function preSaveInstanceSettings(array &$settings); + + /** + * Defines custom behavior for transforming instance settings when loading. + * + * May be used to alter settings to transform them from a storage-friendly + * form. + * + * @param array $settings + * Field settings. + */ + public function postLoadInstanceSettings(array &$settings); + + /** * Defines custom insert behavior for field values. * * This method is called during the process of inserting an entity, just diff --git a/core/modules/field/src/Entity/FieldConfig.php b/core/modules/field/src/Entity/FieldConfig.php index af025e3..767e075 100644 --- a/core/modules/field/src/Entity/FieldConfig.php +++ b/core/modules/field/src/Entity/FieldConfig.php @@ -720,7 +720,7 @@ public function getUniqueStorageIdentifier() { /** * Helper to retrieve the field item class. */ - protected function getFieldItemClass() { + public function getFieldItemClass() { $type_definition = \Drupal::typedDataManager() ->getDefinition('field_item:' . $this->getType()); return $type_definition['class']; diff --git a/core/modules/field/src/FieldConfigStorage.php b/core/modules/field/src/FieldConfigStorage.php index 8419424..76566c9 100644 --- a/core/modules/field/src/FieldConfigStorage.php +++ b/core/modules/field/src/FieldConfigStorage.php @@ -10,6 +10,7 @@ use Drupal\Component\Uuid\UuidInterface; use Drupal\Core\Config\Config; use Drupal\Core\Config\Entity\ConfigEntityStorage; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\Query\QueryFactory; @@ -156,4 +157,35 @@ public function loadByProperties(array $conditions = array()) { return $matching_fields; } + + /** + * {@inheritdoc} + */ + protected function postLoad(array &$entities) { + foreach ($entities as $entity) { + // @todo This needs to modify the entity, so works off of the entity + // settings as it believes at the same. Because this came from storage + // it would technically be the same as the output of preSaveData() but + // philosophically it feels far from it. + if (is_array($entity->settings)) { + $field_class = $entity->getFieldItemClass(); + $field_class::postLoadSettings($entity->settings); + } + } + + parent::postLoad($entities); + } + + /** + * {@inheritdoc} + */ + protected function preSaveData($data, EntityInterface $entity) { + // @todo This looks ugly as it assumes a settings key which is not codified + // on this level otherwise. Also this works in a very different level, + // not using the entity anymore to avoid modifying it vs. load. + $field_class = $entity->getFieldItemClass(); + $data['settings'] = $field_class::preSaveSettings($data['settings']); + return $data; + } + } diff --git a/core/modules/options/src/Plugin/Field/FieldType/ListItemAllowedValuesTrait.php b/core/modules/options/src/Plugin/Field/FieldType/ListItemAllowedValuesTrait.php index d10e303..375e8ce 100644 --- a/core/modules/options/src/Plugin/Field/FieldType/ListItemAllowedValuesTrait.php +++ b/core/modules/options/src/Plugin/Field/FieldType/ListItemAllowedValuesTrait.php @@ -13,6 +13,24 @@ trait ListItemAllowedValuesTrait { /** + * Defined in \Drupal\Core\Field\FieldItemInterface. + */ + public static function preSaveSettings(array &$settings) { + if (isset($settings['allowed_values'])) { + $settings['allowed_values'] = static::structureAllowedValues($settings['allowed_values']); + } + } + + /** + * Defined in \Drupal\Core\Field\FieldItemInterface. + */ + public static function postLoadSettings(array &$settings) { + if (isset($settings['allowed_values'])) { + $settings['allowed_values'] = static::simplifyAllowedValues($settings['allowed_values']); + } + } + + /** * Simplify allowed values to a key-value array from the structured array. * * @param array $structured_values @@ -23,7 +43,7 @@ * Allowed values were the array key is the 'value' value, the value is * the 'label' value. */ - public static function simplifyAllowedValues(array $structured_values) { + protected static function simplifyAllowedValues(array $structured_values) { $values = array(); foreach ($structured_values as $item) { if (is_array($item['label'])) { @@ -47,7 +67,7 @@ public static function simplifyAllowedValues(array $structured_values) { * Array of items with a 'value' and 'label' key each for the allowed * values. */ - public static function structureAllowedValues(array $values) { + protected static function structureAllowedValues(array $values) { $structured_values = array(); foreach ($values as $value => $label) { if (is_array($label)) {