You are here

public function PcaAddressSettingsForm::buildForm in Loqate 2.x

Same name and namespace in other branches
  1. 8 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 50

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>',
  ];
  $group_class = 'field-mapping-order-weight';
  $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'),
      ],
      'weight' => [
        'data' => $this
          ->t('Weight'),
      ],
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => $group_class,
        'hidden' => TRUE,
      ],
    ],
    '#empty' => $this
      ->t('No address fields found.'),
  ];
  $rows = [];
  foreach (PcaAddressElement::getConstants() as $field_name) {
    $default_values = [
      'field' => '',
      'mode' => PcaAddressMode::DEFAULT,
      'enabled' => FALSE,
      'weight' => 0,
    ];
    foreach ($config
      ->get(self::PCA_FIELDS) as $i => $field_map) {
      if ($field_map['element'] === $field_name) {
        $default_values['field'] = $field_map['field'];
        $default_values['mode'] = $field_map['mode'];
        $default_values['enabled'] = $field_map['enabled'];
        $default_values['weight'] = $i;
        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'],
        ],
      ],
      'enabled' => [
        'data' => [
          '#type' => 'checkbox',
          '#default_value' => $default_values['enabled'],
        ],
      ],
      'weight' => [
        '#type' => 'weight',
        '#title_display' => 'invisible',
        '#default_value' => $default_values['weight'],
        '#attributes' => [
          'class' => [
            $group_class,
          ],
        ],
      ],
    ];

    // Add the weight attr.
    $rows[$field_name]['#weight'] = $default_values['weight'];

    // Add the draggable class.
    $rows[$field_name]['#attributes']['class'][] = 'draggable';
  }

  // Sort by weight & add rows to tree.
  uasort($rows, [
    SortArray::class,
    'sortByWeightElement',
  ]);
  $form['field_mapping'][self::PCA_FIELDS] += $rows;
  return parent::buildForm($form, $form_state);
}