FieldStorageConfigStorage.php 5.86 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\field\FieldStorageConfigStorage.
6 7 8 9
 */

namespace Drupal\field;

10
use Drupal\Component\Uuid\UuidInterface;
11
use Drupal\Core\Config\Entity\ConfigEntityStorage;
12
use Drupal\Core\Entity\EntityInterface;
13
use Drupal\Core\Entity\EntityManagerInterface;
14
use Drupal\Core\Entity\EntityTypeInterface;
15
use Drupal\Core\Field\FieldTypePluginManagerInterface;
16
use Drupal\Core\Language\LanguageManagerInterface;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
use Drupal\Core\Config\ConfigFactoryInterface;
19
use Drupal\Core\Extension\ModuleHandlerInterface;
20
use Drupal\Core\State\StateInterface;
21 22

/**
23
 * Controller class for "field storage" configuration entities.
24
 */
25
class FieldStorageConfigStorage extends ConfigEntityStorage {
26 27 28 29

  /**
   * The module handler.
   *
30
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
31 32 33 34 35 36
   */
  protected $moduleHandler;

  /**
   * The entity manager.
   *
37
   * @var \Drupal\Core\Entity\EntityManagerInterface
38 39 40 41 42 43
   */
  protected $entityManager;

  /**
   * The state keyvalue collection.
   *
44
   * @var \Drupal\Core\State\StateInterface
45 46 47
   */
  protected $state;

48 49 50 51 52 53 54
  /**
   * The field type plugin manager.
   *
   * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
   */
  protected $fieldTypeManager;

55
  /**
56
   * Constructs a FieldStorageConfigStorage object.
57
   *
58 59
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
60
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
61
   *   The config factory service.
62 63
   * @param \Drupal\Component\Uuid\UuidInterface $uuid_service
   *   The UUID service.
64 65
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
66
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
67
   *   The entity manager.
68
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
69
   *   The module handler.
70
   * @param \Drupal\Core\State\StateInterface $state
71
   *   The state key value store.
72 73
   * @param \Drupal\Component\Plugin\PluginManagerInterface\FieldTypePluginManagerInterface
   *   The field type plugin manager.
74
   */
75
  public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, StateInterface $state, FieldTypePluginManagerInterface $field_type_manager) {
76
    parent::__construct($entity_type, $config_factory, $uuid_service, $language_manager);
77 78 79
    $this->entityManager = $entity_manager;
    $this->moduleHandler = $module_handler;
    $this->state = $state;
80
    $this->fieldTypeManager = $field_type_manager;
81 82 83 84 85
  }

  /**
   * {@inheritdoc}
   */
86
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
87
    return new static(
88
      $entity_type,
89
      $container->get('config.factory'),
90
      $container->get('uuid'),
91
      $container->get('language_manager'),
92
      $container->get('entity.manager'),
93
      $container->get('module_handler'),
94 95
      $container->get('state'),
      $container->get('plugin.manager.field.field_type')
96 97 98 99 100 101 102
    );
  }

  /**
   * {@inheritdoc}
   */
  public function loadByProperties(array $conditions = array()) {
103
    // Include deleted fields if specified in the $conditions parameters.
104
    $include_deleted = isset($conditions['include_deleted']) ? $conditions['include_deleted'] : FALSE;
105 106
    unset($conditions['include_deleted']);

107
    /** @var \Drupal\field\FieldStorageConfigInterface[] $storages */
108
    $storages = array();
109

110 111 112
    // Get field storages living in configuration. If we are explicitly looking
    // for deleted storages only, this can be skipped, because they will be
    // retrieved from state below.
113 114 115 116
    if (empty($conditions['deleted'])) {
      if (isset($conditions['entity_type']) && isset($conditions['field_name'])) {
        // Optimize for the most frequent case where we do have a specific ID.
        $id = $conditions['entity_type'] . $conditions['field_name'];
117
        $storages = $this->loadMultiple(array($id));
118 119
      }
      else {
120 121
        // No specific ID, we need to examine all existing storages.
        $storages = $this->loadMultiple();
122
      }
123 124
    }

125
    // Merge deleted field storages (living in state) if needed.
126
    if ($include_deleted || !empty($conditions['deleted'])) {
127 128 129
      $deleted_storages = $this->state->get('field.storage.deleted') ?: array();
      foreach ($deleted_storages as $id => $config) {
        $storages[$id] = $this->create($config);
130 131 132 133
      }
    }

    // Collect matching fields.
134 135
    $matches = array();
    foreach ($storages as $field) {
136 137
      foreach ($conditions as $key => $value) {
        // Extract the actual value against which the condition is checked.
138
        $checked_value = $field->get($key);
139 140 141 142 143 144 145 146
        // Skip to the next field as soon as one condition does not match.
        if ($checked_value != $value) {
          continue 2;
        }
      }

      // When returning deleted fields, key the results by UUID since they can
      // include several fields with the same ID.
147
      $key = $include_deleted ? $field->uuid() : $field->id();
148
      $matches[$key] = $field;
149 150
    }

151
    return $matches;
152
  }
153 154 155 156 157 158 159

  /**
   * {@inheritdoc}
   */
  protected function mapFromStorageRecords(array $records) {
    foreach ($records as &$record) {
      $class = $this->fieldTypeManager->getPluginClass($record['type']);
160
      $record['settings'] = $class::storageSettingsFromConfigData($record['settings']);
161 162 163 164 165 166 167 168 169 170
    }
    return parent::mapFromStorageRecords($records);
  }

  /**
   * {@inheritdoc}
   */
  protected function mapToStorageRecord(EntityInterface $entity) {
    $record = parent::mapToStorageRecord($entity);
    $class = $this->fieldTypeManager->getPluginClass($record['type']);
171
    $record['settings'] = $class::storageSettingsToConfigData($record['settings']);
172 173 174
    return $record;
  }

175
}