FieldInstanceStorageController.php 5.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

/**
 * @file
 * Contains \Drupal\field\FieldInstanceStorageController.
 */

namespace Drupal\field;

use Drupal\Core\Config\Config;
use Drupal\Core\Config\Entity\ConfigStorageController;
12
use Drupal\Core\Entity\EntityManagerInterface;
13
use Drupal\Core\Entity\Query\QueryFactory;
14 15
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactory;
16
use Drupal\Component\Uuid\UuidInterface;
17 18
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Extension\ModuleHandler;
19
use Drupal\Core\KeyValueStore\StateInterface;
20 21 22 23 24 25 26 27 28 29 30

/**
 * Controller class for field instances.
 *
 * Note: the class take no special care about importing instances after their
 * field in importCreate(), since this is guaranteed by the alphabetical order
 * (field.field.* entries are processed before field.instance.* entries).
 * @todo Revisit after http://drupal.org/node/1944368.
 */
class FieldInstanceStorageController extends ConfigStorageController {

31 32 33 34 35 36 37 38 39 40
  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandler
   */
  protected $moduleHandler;

  /**
   * The entity manager.
   *
41
   * @var \Drupal\Core\Entity\EntityManagerInterface
42 43 44 45 46 47
   */
  protected $entityManager;

  /**
   * The state keyvalue collection.
   *
48
   * @var \Drupal\Core\KeyValueStore\StateInterface
49 50 51 52 53 54 55 56 57 58 59 60 61 62
   */
  protected $state;

  /**
   * Constructs a FieldInstanceStorageController object.
   *
   * @param string $entity_type
   *   The entity type for which the instance is created.
   * @param array $entity_info
   *   An array of entity info for the entity type.
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   The config factory service.
   * @param \Drupal\Core\Config\StorageInterface $config_storage
   *   The config storage service.
63 64 65 66
   * @param \Drupal\Core\Entity\Query\QueryFactory $entity_query_factory
   *   The entity query factory.
   * @param \Drupal\Component\Uuid\UuidInterface $uuid_service
   *   The UUID service.
67
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
68 69 70
   *   The entity manager.
   * @param \Drupal\Core\Extension\ModuleHandler $module_handler
   *   The module handler.
71
   * @param \Drupal\Core\KeyValueStore\StateInterface $state
72 73
   *   The state key value store.
   */
74
  public function __construct($entity_type, array $entity_info, ConfigFactory $config_factory, StorageInterface $config_storage, QueryFactory $entity_query_factory, UuidInterface $uuid_service, EntityManagerInterface $entity_manager, ModuleHandler $module_handler, StateInterface $state) {
75
    parent::__construct($entity_type, $entity_info, $config_factory, $config_storage, $entity_query_factory, $uuid_service);
76 77 78 79 80 81 82 83 84 85 86 87 88 89
    $this->entityManager = $entity_manager;
    $this->moduleHandler = $module_handler;
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
    return new static(
      $entity_type,
      $entity_info,
      $container->get('config.factory'),
      $container->get('config.storage'),
90
      $container->get('entity.query'),
91
      $container->get('uuid'),
92
      $container->get('entity.manager'),
93 94 95 96 97
      $container->get('module_handler'),
      $container->get('state')
    );
  }

98 99 100 101 102 103 104 105 106 107 108 109 110
  /**
   * {@inheritdoc}
   */
  public function importDelete($name, Config $new_config, Config $old_config) {
    // If the field has been deleted in the same import, the instance will be
    // deleted by then, and there is nothing left to do. Just return TRUE so
    // that the file does not get written to active store.
    if (!$old_config->get()) {
      return TRUE;
    }
    return parent::importDelete($name, $new_config, $old_config);
  }

111 112 113 114 115
  /**
   * {@inheritdoc}
   */
  public function loadByProperties(array $conditions = array()) {
    // Include deleted instances if specified in the $conditions parameters.
116
    $include_deleted = isset($conditions['include_deleted']) ? $conditions['include_deleted'] : FALSE;
117 118 119 120 121
    unset($conditions['include_deleted']);

    // Get instances stored in configuration.
    if (isset($conditions['entity_type']) && isset($conditions['bundle']) && isset($conditions['field_name'])) {
      // Optimize for the most frequent case where we do have a specific ID.
122 123
      $id = $conditions['entity_type'] . '.' . $conditions['bundle'] . '.' . $conditions['field_name'];
      $instances = $this->entityManager->getStorageController($this->entityType)->loadMultiple(array($id));
124 125 126
    }
    else {
      // No specific ID, we need to examine all existing instances.
127
      $instances = $this->entityManager->getStorageController($this->entityType)->loadMultiple();
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    }

    // Merge deleted instances (stored in state) if needed.
    if ($include_deleted) {
      $deleted_instances = $this->state->get('field.instance.deleted') ?: array();
      foreach ($deleted_instances as $id => $config) {
        $instances[$id] = $this->entityManager->getStorageController($this->entityType)->create($config);
      }
    }

    // Collect matching instances.
    $matching_instances = array();
    foreach ($instances as $instance) {
      // Some conditions are checked against the field.
      $field = $instance->getField();

      // Only keep the instance if it matches all conditions.
      foreach ($conditions as $key => $value) {
        // Extract the actual value against which the condition is checked.
        switch ($key) {
          case 'field_name':
149
            $checked_value = $field->name;
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
            break;

          case 'field_id':
            $checked_value = $instance->field_uuid;
            break;

          default:
            $checked_value = $instance->$key;
            break;
        }

        // Skip to the next instance as soon as one condition does not match.
        if ($checked_value != $value) {
          continue 2;
        }
      }

      $matching_instances[] = $instance;
    }

    return $matching_instances;
  }

173
}