示例#1
0
 /**
  * {@inheritdoc}
  */
 public function fieldSettingsForm(array $form, FormStateInterface $form_state)
 {
     $languages = \Drupal::languageManager()->getLanguages(LanguageInterface::STATE_ALL);
     $languageOptions = [];
     foreach ($languages as $langcode => $language) {
         // Only list real languages (English, French, but not "Not specified").
         if (!$language->isLocked()) {
             $languageOptions[$langcode] = $language->getName();
         }
     }
     $element = [];
     $element['available_countries'] = ['#type' => 'select', '#title' => $this->t('Available countries'), '#description' => $this->t('If no countries are selected, all countries will be available.'), '#options' => \Drupal::service('address.country_repository')->getList(), '#default_value' => $this->getSetting('available_countries'), '#multiple' => TRUE, '#size' => 10];
     $element['fields'] = ['#type' => 'checkboxes', '#title' => $this->t('Used fields'), '#description' => $this->t('Note: an address used for postal purposes needs all of the above fields.'), '#default_value' => $this->getSetting('fields'), '#options' => LabelHelper::getGenericFieldLabels(), '#required' => TRUE];
     $element['langcode_override'] = ['#type' => 'select', '#title' => $this->t('Language override'), '#description' => $this->t('Ensures entered addresses are always formatted in the same language.'), '#options' => $languageOptions, '#default_value' => $this->getSetting('langcode_override'), '#empty_option' => $this->t('- No override -'), '#access' => \Drupal::languageManager()->isMultilingual()];
     return $element;
 }
 /**
  * {@inheritdoc}
  */
 public function form(array $form, FormStateInterface $form_state)
 {
     $form = parent::form($form, $form_state);
     $address_format = $this->entity;
     $country_code = $address_format->getCountryCode();
     if ($country_code == 'ZZ') {
         $form['countryCode'] = ['#type' => 'item', '#title' => $this->t('Country'), '#markup' => $this->t('Generic')];
     } else {
         $form['countryCode'] = ['#type' => 'select', '#title' => $this->t('Country'), '#default_value' => $country_code, '#required' => TRUE, '#options' => $this->countryRepository->getList(), '#disabled' => !$address_format->isNew()];
     }
     $form['format'] = ['#type' => 'textarea', '#title' => $this->t('Format'), '#description' => $this->t('Available tokens: @tokens', ['@tokens' => implode(', ', AddressField::getTokens())]), '#default_value' => $address_format->getFormat(), '#required' => TRUE];
     $form['requiredFields'] = ['#type' => 'checkboxes', '#title' => t('Required fields'), '#options' => LabelHelper::getGenericFieldLabels(), '#default_value' => $address_format->getRequiredFields()];
     $form['uppercaseFields'] = ['#type' => 'checkboxes', '#title' => t('Uppercase fields'), '#description' => t('Uppercased on envelopes to facilitate automatic post handling.'), '#options' => LabelHelper::getGenericFieldLabels(), '#default_value' => $address_format->getUppercaseFields()];
     $form['postalCodePattern'] = ['#type' => 'textfield', '#title' => $this->t('Postal code pattern'), '#description' => $this->t('Regular expression used to validate postal codes.'), '#default_value' => $address_format->getPostalCodePattern()];
     $form['postalCodePrefix'] = ['#type' => 'textfield', '#title' => $this->t('Postal code prefix'), '#description' => $this->t('Added to postal codes when formatting an address for international mailing.'), '#default_value' => $address_format->getPostalCodePrefix(), '#size' => 5];
     $form['postalCodeType'] = ['#type' => 'select', '#title' => $this->t('Postal code type'), '#default_value' => $address_format->getPostalCodeType(), '#options' => LabelHelper::getPostalCodeLabels(), '#empty_value' => ''];
     $form['dependentLocalityType'] = ['#type' => 'select', '#title' => $this->t('Dependent locality type'), '#default_value' => $address_format->getDependentLocalityType(), '#options' => LabelHelper::getDependentLocalityLabels(), '#empty_value' => ''];
     $form['localityType'] = ['#type' => 'select', '#title' => $this->t('Locality type'), '#default_value' => $address_format->getLocalityType(), '#options' => LabelHelper::getLocalityLabels(), '#empty_value' => ''];
     $form['administrativeAreaType'] = ['#type' => 'select', '#title' => $this->t('Administrative area type'), '#default_value' => $address_format->getAdministrativeAreaType(), '#options' => LabelHelper::getAdministrativeAreaLabels(), '#empty_value' => ''];
     return $form;
 }