You are here

function _autofill_get_available_source_fields_as_options in Autofill 8

Build an option list of available autofill source fields.

Parameters

array $form: The currently processed form.

string $current_field_name: The currently process field name.

Return value

array The field name and field label as a key/value pair.

1 call to _autofill_get_available_source_fields_as_options()
autofill_field_widget_third_party_settings_form in ./autofill.module
Implements hook_field_widget_third_party_settings_form().

File

./autofill.module, line 93
Contains autofill.module.

Code

function _autofill_get_available_source_fields_as_options(array $form, $current_field_name) {
  $options = [];
  $entity_type = $form['#entity_type'];
  $bundle = $form['#bundle'];
  $field_storage_definitions = \Drupal::service('entity_field.manager')
    ->getFieldStorageDefinitions($entity_type);

  // Filter the available fields by the support field type and make sure it is
  // not the currently processed field.
  $available_entity_fields = array_filter($field_storage_definitions, function (FieldStorageDefinitionInterface $field_storage) use ($current_field_name) {
    return $field_storage
      ->getName() !== $current_field_name && $field_storage
      ->getType() === 'string';
  });
  $form_fields = array_intersect_key($available_entity_fields, array_flip($form['#fields']));

  /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field */
  foreach ($form_fields as $field) {
    $label = $field
      ->getLabel();

    // Load labels from field_config for regular fields.
    $field_configs = \Drupal::entityTypeManager()
      ->getStorage('field_config')
      ->loadByProperties([
      'field_name' => $field
        ->getName(),
      'entity_type' => $entity_type,
      'bundle' => $bundle,
    ]);

    /** @var \Drupal\field\FieldConfigInterface $field_config */
    if ($field_config = array_pop($field_configs)) {
      $label = $field_config
        ->getLabel();
    }
    $options[$field
      ->getName()] = $label;
  }
  return $options;
}