public function FieldInstanceStorageController::loadByProperties

Implements Drupal\Core\Entity\EntityStorageControllerInterface::loadByProperties().

Overrides ConfigStorageController::loadByProperties

File

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

Class

FieldInstanceStorageController
Controller class for field instances.

Namespace

Drupal\field

Code

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