You are here

protected function TransactorBase::getAvailableFields in Transaction 8

Search for fields of a given type in a given entity type.

Parameters

string $entity_type_id: The entity type ID to search in.

string $field_type: A field type, @see https://www.drupal.org/node/2302735 for a list.

string|string[] $bundles: (optional) Limit to selected bundle or bundles.

array $settings_match: (optional) A keyed name/value array of settings the field must match.

Return value

string[] An array with the names of matching fields keyed the field id.

3 calls to TransactorBase::getAvailableFields()
TransactorBase::buildTargetFieldsForm in src/TransactorBase.php
Build configuration form fields to the target entity.
TransactorBase::buildTransactionFieldsForm in src/TransactorBase.php
Build configuration form fields to the transaction.
TransactorBase::fieldReferenceSettingsFormField in src/TransactorBase.php
Builds a form field to reference a field.

File

src/TransactorBase.php, line 286

Class

TransactorBase
Provides a base class for transactor plugins.

Namespace

Drupal\transaction

Code

protected function getAvailableFields($entity_type_id, $field_type, $bundles = [], array $settings_match = []) {
  $options = [];
  if (!empty($bundles)) {

    // Params adjustment.
    if (is_string($bundles)) {
      $bundles = [
        $bundles,
      ];
    }
    $fields_by_type = $this->fieldManager
      ->getFieldMapByFieldType($field_type);

    // We have filter by bundle but there are no fields of the given type in
    // any bundle of the entity type.
    if (!isset($fields_by_type[$entity_type_id])) {
      return $options;
    }
  }

  // Iterate over the storage definitions of the entity fields.
  foreach ($this->fieldManager
    ->getFieldStorageDefinitions($entity_type_id) as $field_name => $field_storage) {
    if ($field_storage
      ->getType() != $field_type) {
      continue;
    }

    // Filter by bundle.
    foreach ($bundles as $bundle) {
      if (!isset($fields_by_type[$entity_type_id]['bundles'][$bundle])) {
        continue 2;
      }

      // Load the field config for the first bundle.
      if (!isset($field_config)) {
        $field_config = FieldConfig::loadByName($entity_type_id, $bundle, $field_name);
      }
    }

    // Filter by settings.
    foreach ($settings_match as $key => $value) {
      if ($field_storage
        ->getSetting($key) != $value) {
        continue 2;
      }
    }
    $options[$field_name] = isset($field_config) ? $field_config
      ->label() . ' (' . $field_name . ')' : $field_name;
    unset($field_config);
  }
  return $options;
}