You are here

public function PcaAddressSettingsForm::buildForm in Loqate 8

Same name and namespace in other branches
  1. 2.x src/Form/PcaAddressSettingsForm.php \Drupal\loqate\Form\PcaAddressSettingsForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/PcaAddressSettingsForm.php, line 49

Class

PcaAddressSettingsForm
Class PcaAddressSettingsForm.

Namespace

Drupal\loqate\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('loqate.settings');
  $form['field_mapping'] = [
    '#title' => $this
      ->t('Field mapping'),
    '#type' => 'details',
    '#open' => TRUE,
  ];
  $doc_link = Link::fromTextAndUrl('Mapping your fields', Url::fromUri('https://www.loqate.com/resources/support/setup-guides/advanced-setup-guide/#mapping_fields'));
  $doc_markup = $this
    ->t('These settings are used as the fields object when instantiating a pca.Address object. See @link for more details about the field mapping format.', [
    '@link' => $doc_link
      ->toString(),
  ]);
  $form['field_mapping']['description'] = [
    '#markup' => '<p>' . $doc_markup . '</p>',
  ];
  $form['field_mapping'][self::PCA_FIELDS] = [
    '#type' => 'table',
    '#header' => [
      'element' => [
        'data' => $this
          ->t('Element'),
      ],
      'field' => [
        'data' => $this
          ->t('Field'),
      ],
      'mode' => [
        'data' => $this
          ->t('Mode'),
      ],
      'enabled' => [
        'data' => $this
          ->t('Enabled'),
      ],
    ],
    '#empty' => $this
      ->t('No address fields found.'),
  ];
  $rows = [];
  foreach (PcaAddressElement::getConstants() as $field_name) {
    $default_values = [];
    foreach ($config
      ->get(self::PCA_FIELDS) as $field_map) {
      if ($field_map['element'] === $field_name) {
        $default_values['field'] = $field_map['field'];
        $default_values['mode'] = $field_map['mode'];
        $default_values['enabled'] = TRUE;
        break;
      }
    }
    $rows[$field_name] = [
      'element' => [
        'data' => [
          '#type' => 'markup',
          '#markup' => $field_name,
        ],
      ],
      'field' => [
        'data' => [
          '#type' => 'select',
          '#options' => [
            '' => $this
              ->t('- None -'),
          ] + array_combine(PcaAddressField::getConstants(), PcaAddressField::getConstants()),
          '#default_value' => $default_values['field'] ?? '',
        ],
      ],
      'mode' => [
        'data' => [
          '#type' => 'select',
          '#options' => [
            PcaAddressMode::NONE => $this
              ->t('NONE'),
            PcaAddressMode::SEARCH => $this
              ->t('SEARCH'),
            PcaAddressMode::POPULATE => $this
              ->t('POPULATE'),
            PcaAddressMode::DEFAULT => $this
              ->t('DEFAULT'),
            PcaAddressMode::PRESERVE => $this
              ->t('PRESERVE'),
            PcaAddressMode::COUNTRY => $this
              ->t('COUNTRY'),
          ],
          '#default_value' => $default_values['mode'] ?? PcaAddressMode::DEFAULT,
        ],
      ],
      'enabled' => [
        'data' => [
          '#type' => 'checkbox',
          '#default_value' => $default_values['enabled'] ?? FALSE,
        ],
      ],
    ];
  }
  $form['field_mapping'][self::PCA_FIELDS] += $rows;
  return parent::buildForm($form, $form_state);
}