FieldConfigStorage.php 5.06 KB
Newer Older
1
2
3
4
<?php

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

namespace Drupal\field;

10
use Drupal\Component\Uuid\UuidInterface;
11
use Drupal\Core\Config\Config;
12
use Drupal\Core\Config\Entity\ConfigEntityStorage;
13
use Drupal\Core\Entity\EntityManagerInterface;
14
use Drupal\Core\Entity\EntityTypeInterface;
15
use Drupal\Core\Entity\Query\QueryFactory;
16
use Drupal\Core\Language\LanguageManagerInterface;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
use Drupal\Core\Config\ConfigFactoryInterface;
19
20
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Extension\ModuleHandler;
21
use Drupal\Core\State\StateInterface;
22
23
24
25

/**
 * Controller class for fields.
 */
26
class FieldConfigStorage extends ConfigEntityStorage {
27
28
29
30
31
32
33
34
35
36
37

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandler
   */
  protected $moduleHandler;

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

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

  /**
50
   * Constructs a FieldConfigStorage object.
51
   *
52
53
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
54
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
55
56
57
   *   The config factory service.
   * @param \Drupal\Core\Config\StorageInterface $config_storage
   *   The config storage service.
58
59
   * @param \Drupal\Component\Uuid\UuidInterface $uuid_service
   *   The UUID service.
60
61
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
62
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
63
64
65
   *   The entity manager.
   * @param \Drupal\Core\Extension\ModuleHandler $module_handler
   *   The module handler.
66
   * @param \Drupal\Core\State\StateInterface $state
67
68
   *   The state key value store.
   */
69
70
  public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, StorageInterface $config_storage, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, EntityManagerInterface $entity_manager, ModuleHandler $module_handler, StateInterface $state) {
    parent::__construct($entity_type, $config_factory, $config_storage, $uuid_service, $language_manager);
71
72
73
74
75
76
77
78
    $this->entityManager = $entity_manager;
    $this->moduleHandler = $module_handler;
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
79
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
80
    return new static(
81
      $entity_type,
82
83
      $container->get('config.factory'),
      $container->get('config.storage'),
84
      $container->get('uuid'),
85
      $container->get('language_manager'),
86
      $container->get('entity.manager'),
87
88
89
90
91
92
93
94
95
96
      $container->get('module_handler'),
      $container->get('state')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function loadByProperties(array $conditions = array()) {
    // Include deleted instances if specified in the $conditions parameters.
97
    $include_deleted = isset($conditions['include_deleted']) ? $conditions['include_deleted'] : FALSE;
98
99
    unset($conditions['include_deleted']);

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    $fields = array();

    // Get fields stored in configuration. If we are explicitly looking for
    // deleted fields only, this can be skipped, because they will be retrieved
    // from state below.
    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'];
        $fields = $this->loadMultiple(array($id));
      }
      else {
        // No specific ID, we need to examine all existing fields.
        $fields = $this->loadMultiple();
      }
115
116
117
    }

    // Merge deleted fields (stored in state) if needed.
118
    if ($include_deleted || !empty($conditions['deleted'])) {
119
120
      $deleted_fields = $this->state->get('field.field.deleted') ?: array();
      foreach ($deleted_fields as $id => $config) {
121
        $fields[$id] = $this->create($config);
122
123
124
125
126
127
128
129
130
131
      }
    }

    // Collect matching fields.
    $matching_fields = array();
    foreach ($fields as $field) {
      foreach ($conditions as $key => $value) {
        // Extract the actual value against which the condition is checked.
        switch ($key) {
          case 'field_name';
132
            $checked_value = $field->name;
133
134
            break;

135
136
137
138
          case 'uuid';
            $checked_value = $field->uuid();
            break;

139
140
141
142
143
144
145
146
147
148
149
150
151
          default:
            $checked_value = $field->$key;
            break;
        }

        // 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.
152
      $key = $include_deleted ? $field->uuid() : $field->id();
153
154
155
156
157
158
159
      $matching_fields[$key] = $field;
    }

    return $matching_fields;

  }
}