You are here

function autofill_field_widget_third_party_settings_form in Autofill 8

Implements hook_field_widget_third_party_settings_form().

File

./autofill.module, line 33
Contains autofill.module.

Code

function autofill_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, array $form, FormStateInterface $form_state) {
  $element = [];
  if ($field_definition
    ->getType() === 'string') {
    $field_name = $field_definition
      ->getName();
    $available_source_fields = _autofill_get_available_source_fields_as_options($form, $field_name);
    $element['enabled'] = [
      '#type' => 'checkbox',
      '#title' => t('Enable Autofill from another field'),
      '#disabled' => !empty($available_source_fields) ? FALSE : TRUE,
      '#default_value' => !empty($available_source_fields) ? $plugin
        ->getThirdPartySetting('autofill', 'enabled') : NULL,
    ];

    // Check if there are available source fields.
    if (!empty($available_source_fields)) {
      $element['source_field'] = [
        '#type' => 'select',
        '#title' => t('Autofill source field'),
        '#default_value' => $plugin
          ->getThirdPartySetting('autofill', 'source_field'),
        '#options' => $available_source_fields,
        '#states' => [
          'visible' => [
            ':input[name="fields[' . $field_name . '][settings_edit_form][third_party_settings][autofill][enabled]"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
    }
    else {
      $element['source_field'] = [
        '#markup' => t('No source field available. Please create a text field from where this field should be autofilled.'),
      ];
    }
  }
  return $element;
}