FieldConfigStorageBase.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace Drupal\Core\Field;
  3. use Drupal\Core\Config\Entity\ConfigEntityStorage;
  4. use Drupal\Core\Entity\EntityInterface;
  5. /**
  6. * Base storage class for field config entities.
  7. */
  8. abstract class FieldConfigStorageBase extends ConfigEntityStorage {
  9. /**
  10. * The field type plugin manager.
  11. *
  12. * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
  13. */
  14. protected $fieldTypeManager;
  15. /**
  16. * {@inheritdoc}
  17. */
  18. protected function mapFromStorageRecords(array $records) {
  19. foreach ($records as $id => &$record) {
  20. $class = $this->fieldTypeManager->getPluginClass($record['field_type']);
  21. if (empty($class)) {
  22. $config_id = $this->getPrefix() . $id;
  23. throw new \RuntimeException("Unable to determine class for field type '{$record['field_type']}' found in the '$config_id' configuration");
  24. }
  25. $record['settings'] = $class::fieldSettingsFromConfigData($record['settings']);
  26. }
  27. return parent::mapFromStorageRecords($records);
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected function mapToStorageRecord(EntityInterface $entity) {
  33. $record = parent::mapToStorageRecord($entity);
  34. $class = $this->fieldTypeManager->getPluginClass($record['field_type']);
  35. $record['settings'] = $class::fieldSettingsToConfigData($record['settings']);
  36. return $record;
  37. }
  38. }