class FieldInstanceStorageController

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.

Hierarchy

Expanded class hierarchy of FieldInstanceStorageController

File

drupal/core/modules/field/lib/Drupal/field/FieldInstanceStorageController.php, line 27
Contains \Drupal\field\FieldInstanceStorageController.

Namespace

Drupal\field
View source
class FieldInstanceStorageController extends ConfigStorageController {

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

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManager
   */
  protected $entityManager;

  /**
   * The state keyvalue collection.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
   */
  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.
   * @param \Drupal\Core\Entity\EntityManager $entity_manager
   *   The entity manager.
   * @param \Drupal\Core\Extension\ModuleHandler $module_handler
   *   The module handler.
   * @param \Drupal\Core\KeyValueStore\KeyValueStoreInterface $state
   *   The state key value store.
   */
  public function __construct($entity_type, array $entity_info, ConfigFactory $config_factory, StorageInterface $config_storage, EntityManager $entity_manager, ModuleHandler $module_handler, KeyValueStoreInterface $state) {
    parent::__construct($entity_type, $entity_info, $config_factory, $config_storage);
    $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'), $container
      ->get('plugin.manager.entity'), $container
      ->get('module_handler'), $container
      ->get('state'));
  }

  /**
   * {@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);
  }

  /**
   * {@inheritdoc}
   */
  public function loadByProperties(array $conditions = array()) {

    // Include instances of inactive fields if specified in the
    // $conditions parameters.
    $include_inactive = $conditions['include_inactive'];
    unset($conditions['include_inactive']);

    // Include deleted instances if specified in the $conditions parameters.
    $include_deleted = $conditions['include_deleted'];
    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.
      $instances = $this->entityManager
        ->getStorageController($this->entityType)
        ->load(array(
        $conditions['entity_type'] . '.' . $conditions['bundle'] . '.' . $conditions['field_name'],
      ));
    }
    else {

      // No specific ID, we need to examine all existing instances.
      $instances = $this->entityManager
        ->getStorageController($this->entityType)
        ->load();
    }

    // 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);
      }
    }

    // Translate "do not include inactive fields" into actual conditions.
    if (!$include_inactive) {
      $conditions['field.active'] = TRUE;
      $conditions['field.storage.active'] = TRUE;
    }

    // Collect matching instances.
    $matching_instances = array();
    foreach ($instances as $instance) {

      // Only include instances on unknown entity types if 'include_inactive'.
      if (!$include_inactive && !$this->entityManager
        ->getDefinition($instance->entity_type)) {
        continue;
      }

      // 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':
            $checked_value = $field->id;
            break;
          case 'field.active':
            $checked_value = $field->active;
            break;
          case 'field.storage.active':
            $checked_value = $field->storage['active'];
            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;
        }
      }
      $this->moduleHandler
        ->invokeAll('field_read_instance', $instance);
      $matching_instances[] = $instance;
    }
    return $matching_instances;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigStorageController::$configFactory protected property The config factory service.
ConfigStorageController::$configStorage protected property The config storage service.
ConfigStorageController::$statusKey protected property Name of the entity's status key or FALSE if a status is not supported.
ConfigStorageController::$uuidKey protected property Name of the entity's UUID property. Overrides EntityStorageControllerBase::$uuidKey
ConfigStorageController::attachLoad protected function Attaches data to entities upon loading. 4
ConfigStorageController::buildQuery protected function Builds the query to load the entity.
ConfigStorageController::create public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::create(). Overrides EntityStorageControllerInterface::create 2
ConfigStorageController::delete public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::delete(). Overrides EntityStorageControllerInterface::delete
ConfigStorageController::deleteRevision public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::deleteRevision(). Overrides EntityStorageControllerInterface::deleteRevision
ConfigStorageController::getConfigPrefix public function Returns the config prefix used by the configuration entity type.
ConfigStorageController::getFieldDefinitions public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::getFieldDefinitions(). Overrides EntityStorageControllerInterface::getFieldDefinitions
ConfigStorageController::getIDFromConfigName public static function Extracts the configuration entity ID from the full configuration name.
ConfigStorageController::getQueryServicename public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::getQueryServicename(). Overrides EntityStorageControllerInterface::getQueryServicename
ConfigStorageController::importCreate public function Create configuration upon synchronizing configuration changes. 1
ConfigStorageController::importUpdate public function Updates configuration upon synchronizing configuration changes. 1
ConfigStorageController::invokeHook protected function Invokes a hook on behalf of the entity.
ConfigStorageController::load public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::load(). Overrides EntityStorageControllerInterface::load 2
ConfigStorageController::loadRevision public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::loadRevision(). Overrides EntityStorageControllerInterface::loadRevision
ConfigStorageController::postDelete protected function Acts on deleted entities before the delete hook is invoked. 5
ConfigStorageController::postSave protected function Acts on a saved entity before the insert or update hook is invoked. 7
ConfigStorageController::preDelete protected function Acts on entities before they are deleted. 3
ConfigStorageController::preSave protected function Acts on an entity before the presave hook is invoked. 6
ConfigStorageController::save public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::save(). Overrides EntityStorageControllerInterface::save
EntityStorageControllerBase::$cache protected property Whether this entity type should use the static cache. 1
EntityStorageControllerBase::$entityCache protected property Static cache of entities.
EntityStorageControllerBase::$entityInfo protected property Array of information about the entity.
EntityStorageControllerBase::$entityType protected property Entity type for this controller instance.
EntityStorageControllerBase::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
EntityStorageControllerBase::$idKey protected property Name of the entity's ID field in the entity database table.
EntityStorageControllerBase::cacheGet protected function Gets entities from the static cache.
EntityStorageControllerBase::cacheSet protected function Stores entities in the static entity cache.
EntityStorageControllerBase::loadUnchanged public function Loads an unchanged entity from the database. Overrides EntityStorageControllerInterface::loadUnchanged
EntityStorageControllerBase::resetCache public function Resets the internal, static entity cache. Overrides EntityStorageControllerInterface::resetCache 3
FieldInstanceStorageController::$entityManager protected property The entity manager.
FieldInstanceStorageController::$moduleHandler protected property The module handler.
FieldInstanceStorageController::$state protected property The state keyvalue collection.
FieldInstanceStorageController::createInstance public static function Instantiates a new instance of this entity controller. Overrides ConfigStorageController::createInstance
FieldInstanceStorageController::importDelete public function Delete configuration upon synchronizing configuration changes. Overrides ConfigStorageController::importDelete
FieldInstanceStorageController::loadByProperties public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::loadByProperties(). Overrides ConfigStorageController::loadByProperties
FieldInstanceStorageController::__construct public function Constructs a FieldInstanceStorageController object. Overrides ConfigStorageController::__construct