You are here

public function Address::form in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/Address.php \Drupal\webform\Plugin\WebformElement\Address::form()

Gets the actual configuration webform array to be built.

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 An associative array contain the element's configuration webform without any default values.

Overrides WebformCompositeBase::form

File

src/Plugin/WebformElement/Address.php, line 334

Class

Address
Provides a 'address' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  // Get field overrider from element properties.
  $element_properties = $form_state
    ->get('element_properties');
  $field_overrides = $element_properties['field_overrides'];
  unset($element_properties['field_overrides']);
  $form_state
    ->set('element_properties', $element_properties);
  $form['address'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Address settings'),
  ];

  /**************************************************************************/

  // Copied from: \Drupal\address\Plugin\Field\FieldType\AddressItem::fieldSettingsForm

  /**************************************************************************/
  $languages = $this->languageManager
    ->getLanguages(LanguageInterface::STATE_ALL);
  $language_options = [];
  foreach ($languages as $langcode => $language) {
    if (!$language
      ->isLocked()) {
      $language_options[$langcode] = $language
        ->getName();
    }
  }
  $form['address']['available_countries'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Available countries'),
    '#description' => $this
      ->t('If no countries are selected, all countries will be available.'),
    '#options' => $this->addressCountryRepository
      ->getList(),
    '#multiple' => TRUE,
    '#size' => 10,
    '#select2' => TRUE,
  ];
  WebformElementHelper::process($form['address']['available_countries']);
  $form['address']['langcode_override'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Language override'),
    '#description' => $this
      ->t('Ensures entered addresses are always formatted in the same language.'),
    '#options' => $language_options,
    '#empty_option' => $this
      ->t('- No override -'),
    '#access' => $this->languageManager
      ->isMultilingual(),
  ];
  $form['address']['field_overrides_title'] = [
    '#type' => 'item',
    '#title' => $this
      ->t('Field overrides'),
    '#description' => $this
      ->t('Use field overrides to override the country-specific address format, forcing specific fields to always be hidden, optional, or required.'),
    '#access' => TRUE,
  ];
  $form['address']['field_overrides'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Field'),
      $this
        ->t('Override'),
    ],
    '#access' => TRUE,
  ];
  foreach (LabelHelper::getGenericFieldLabels() as $field_name => $label) {
    $override = isset($field_overrides[$field_name]) ? $field_overrides[$field_name] : '';
    $form['address']['field_overrides'][$field_name] = [
      '#access' => TRUE,
      'field_label' => [
        '#access' => TRUE,
        '#type' => 'markup',
        '#markup' => $label,
      ],
      'override' => [
        '#access' => TRUE,
        '#type' => 'select',
        '#default_value' => $override,
        '#options' => [
          FieldOverride::HIDDEN => $this
            ->t('Hidden'),
          FieldOverride::OPTIONAL => $this
            ->t('Optional'),
          FieldOverride::REQUIRED => $this
            ->t('Required'),
        ],
        '#empty_option' => $this
          ->t('- No override -'),
        '#parents' => [
          'properties',
          'field_overrides',
          $field_name,
        ],
      ],
    ];
  }
  return $form;
}