You are here

protected function TransactorBase::fieldReferenceSettingsFormField in Transaction 8

Builds a form field to reference a field.

Parameters

array $field_info: An array with the form field info:

  • name: The form field machine name.
  • title: The form field title.
  • description: (optional) The form field description.
  • required: (opional) TRUE if the field is required.
  • entity_type: The entity type of the target field.
  • type: The type of the target field.
  • settings: An array with settings of the target field. Empty array for no settings.

string $default_value: The form field default value.

array $options: (optional) A list of existent fields in the target entity. Same-type fields found in the target entity type by default.

Return value

array The field reference form field definition.

2 calls to TransactorBase::fieldReferenceSettingsFormField()
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.

File

src/TransactorBase.php, line 359

Class

TransactorBase
Provides a base class for transactor plugins.

Namespace

Drupal\transaction

Code

protected function fieldReferenceSettingsFormField(array $field_info, $default_value, array $options = NULL) {

  // Search for same-type fields in the target entity type if no options
  // provided.
  if (!is_array($options)) {
    $options = $this
      ->getAvailableFields($field_info['entity_type'], $field_info['type'], [], $field_info['settings']);
  }
  $result = [];
  $result[$field_info['name']] = [
    '#type' => 'select',
    '#title' => $field_info['title'],
    '#description' => $field_info['description'],
    '#default_value' => $default_value,
    '#required' => $field_info['required'],
    '#options' => $options,
    '#empty_option' => $this
      ->t('- None -'),
  ];

  // Add an option to create a new field if current user is allowed to do so.
  if ($this->currentUser
    ->hasPermission('administer ' . $field_info['entity_type'] . ' fields')) {
    $result[$field_info['name']]['#options']['_create'] = $this
      ->t('- Create -');
    $states = [
      'visible' => [
        ':input[name="' . $field_info['name'] . '"]' => [
          'value' => '_create',
        ],
      ],
      'required' => [
        ':input[name="' . $field_info['name'] . '"]' => [
          'value' => '_create',
        ],
      ],
    ];
    $machine_maxlength = FieldStorageConfig::NAME_MAX_LENGTH - strlen($this->fieldPrefix);
    $result[$field_info['name'] . '_new'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Create a new field for @name', [
        '@name' => $field_info['title'],
      ]),
      '#states' => $states,
      $field_info['name'] . '_label' => [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Label'),
        '#default_value' => $field_info['title'],
        '#size' => 15,
        '#states' => $states,
      ],
      $field_info['name'] . '_field_name' => [
        '#type' => 'machine_name',
        '#size' => 15,
        '#description' => $this
          ->t('A unique machine-readable name containing letters, numbers, and underscores.'),
        '#required' => FALSE,
        // Calculate characters depending on the length of the field prefix
        // setting. Maximum length is 32.
        '#maxlength' => $machine_maxlength,
        '#machine_name' => [
          'source' => [
            $field_info['entity_type'] == 'transaction' ? 'transaction_fields' : 'target_fields',
            $field_info['name'] . '_new',
            $field_info['name'] . '_label',
          ],
          'exists' => '\\Drupal\\transaction\\TransactorBase::fieldExists',
        ],
        '#states' => $states,
        '#field_prefix' => '<span dir="ltr">' . $this->fieldPrefix,
        '#field_suffix' => '</span>&lrm;',
      ],
    ];
  }
  return $result;
}