public function FieldStorageController::loadByProperties

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

Overrides ConfigStorageController::loadByProperties

File

drupal/core/modules/field/lib/Drupal/field/FieldStorageController.php, line 88
Contains \Drupal\field\FieldStorageController.

Class

FieldStorageController
Controller class for fields.

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 fields stored in configuration.
  if (isset($conditions['field_name'])) {

    // Optimize for the most frequent case where we do have a specific ID.
    $fields = $this->entityManager
      ->getStorageController($this->entityType)
      ->load(array(
      $conditions['field_name'],
    ));
  }
  else {

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

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

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

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

    // When returning deleted fields, key the results by UUID since they can
    // include several fields with the same ID.
    $key = $include_deleted ? $field->uuid : $field->id;
    $matching_fields[$key] = $field;
  }
  return $matching_fields;
}