You are here

public static function TransactorBase::fieldExists in Transaction 8

Machine name exists callback for "inline" field creation.

Parameters

string $field_name: Field name to check.

array $form_element: Form element array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

Return value

bool TRUE if a field with the given name exists in the target entity type.

File

src/TransactorBase.php, line 441

Class

TransactorBase
Provides a base class for transactor plugins.

Namespace

Drupal\transaction

Code

public static function fieldExists($field_name, array $form_element, FormStateInterface $form_state) {

  // @todo Take field info from temporary values in form_state and check
  $transactor_field_name = substr($form_element['#name'], 0, strpos($form_element['#name'], '_field_name'));

  // Do not validate for non-required fields.
  if (($main_field_value = $form_state
    ->getValue($transactor_field_name)) !== NULL && $main_field_value !== '_create') {
    return FALSE;
  }
  if ($field_info = $form_state
    ->getTemporaryValue('field_info_' . $transactor_field_name)) {
    $field_prefix = \Drupal::service('config.factory')
      ->get('field_ui.settings')
      ->get('field_prefix') ?: 'field_';
    return FieldStorageConfig::loadByName($field_info['entity_type'], $field_prefix . $field_name) !== NULL;
  }
  return FALSE;
}