You are here

public function AddressItem::fieldSettingsForm in Address 8

Returns a form for the field-level settings.

Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow administrators to configure field-level settings.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the (entire) configuration form.

Return value

array The form definition for the field settings.

Overrides FieldItemBase::fieldSettingsForm

File

src/Plugin/Field/FieldType/AddressItem.php, line 168

Class

AddressItem
Plugin implementation of the 'address' field type.

Namespace

Drupal\address\Plugin\Field\FieldType

Code

public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
  $languages = \Drupal::languageManager()
    ->getLanguages(LanguageInterface::STATE_ALL);
  $language_options = [];
  foreach ($languages as $langcode => $language) {

    // Only list real languages (English, French, but not "Not specified").
    if (!$language
      ->isLocked()) {
      $language_options[$langcode] = $language
        ->getName();
    }
  }
  $element = $this
    ->countrySettingsForm($form, $form_state);
  $element['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,
    '#default_value' => $this
      ->getSetting('langcode_override'),
    '#empty_option' => $this
      ->t('- No override -'),
    '#access' => \Drupal::languageManager()
      ->isMultilingual(),
  ];
  $element['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 properties to always be hidden, optional, or required.'),
  ];
  $element['field_overrides'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Property'),
      $this
        ->t('Override'),
    ],
    '#element_validate' => [
      [
        get_class($this),
        'fieldOverridesValidate',
      ],
    ],
  ];
  $field_overrides = $this
    ->getFieldOverrides();
  foreach (LabelHelper::getGenericFieldLabels() as $field_name => $label) {
    $override = isset($field_overrides[$field_name]) ? $field_overrides[$field_name] : '';
    $element['field_overrides'][$field_name] = [
      'field_label' => [
        '#type' => 'markup',
        '#markup' => $label,
      ],
      'override' => [
        '#type' => 'select',
        '#options' => [
          FieldOverride::HIDDEN => $this
            ->t('Hidden'),
          FieldOverride::OPTIONAL => $this
            ->t('Optional'),
          FieldOverride::REQUIRED => $this
            ->t('Required'),
        ],
        '#default_value' => $override,
        '#empty_option' => $this
          ->t('- No override -'),
      ],
    ];
  }
  return $element;
}