コード例 #1
0
ファイル: AddressItem.php プロジェクト: jokas/d8.dev
 /**
  * {@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;
 }
コード例 #2
0
 /**
  * {@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;
 }
コード例 #3
0
 /**
  * {@inheritdoc}
  */
 protected function addViolation($field, $message, $invalid_value, AddressFormatInterface $address_format)
 {
     $labels = LabelHelper::getFieldLabels($address_format);
     $label = $labels[$field];
     $this->context->buildViolation($message, ['@name' => $label])->atPath(FieldHelper::getPropertyName($field))->setInvalidValue($invalid_value)->addViolation();
 }
コード例 #4
0
 /**
  * Builds the subdivision form elements.
  *
  * @param array $form
  *   The form.
  * @param array $values
  *   The form values.
  * @param \Drupal\address\Entity\AddressFormatInterface $address_format
  *  The address format for the selected country.
  *
  * @return array
  *   The form with the added subdivision elements.
  */
 protected function buildSubdivisionElements(array $form, array $values, AddressFormatInterface $address_format)
 {
     $depth = $this->subdivisionRepository->getDepth($values['country_code']);
     if ($depth === 0) {
         // No predefined data found.
         return $form;
     }
     $labels = LabelHelper::getFieldLabels($address_format);
     $subdivision_fields = $address_format->getUsedSubdivisionFields();
     $current_depth = 1;
     foreach ($subdivision_fields as $index => $field) {
         $property = FieldHelper::getPropertyName($field);
         $parent_property = $index ? FieldHelper::getPropertyName($subdivision_fields[$index - 1]) : NULL;
         if ($parent_property && empty($values[$parent_property])) {
             // No parent value selected.
             break;
         }
         $parent_id = $parent_property ? $values[$parent_property] : NULL;
         $subdivisions = $this->subdivisionRepository->getList($values['country_code'], $parent_id);
         if (empty($subdivisions)) {
             break;
         }
         $form[$property] = ['#type' => 'select', '#title' => $labels[$field], '#options' => $subdivisions, '#default_value' => $values[$property], '#empty_option' => $this->t('- All -')];
         if ($current_depth < $depth) {
             $form[$property]['#ajax'] = ['callback' => [get_class($this), 'ajaxRefresh'], 'wrapper' => $form['#wrapper_id']];
         }
         $current_depth++;
     }
     return $form;
 }
コード例 #5
0
 /**
  * Builds the format-specific address elements.
  *
  * @param array $element
  *   The existing form element array.
  * @param array $values
  *   An array of address values, keyed by property name.
  *
  * @return array
  *   The modified form element array containing the format specific elements.
  */
 protected function addressElements(array $element, array $values)
 {
     $address_format = $this->addressFormatRepository->get($values['country_code']);
     $required_fields = $address_format->getRequiredFields();
     $labels = LabelHelper::getFieldLabels($address_format);
     foreach ($address_format->getGroupedFields() as $line_index => $line_fields) {
         if (count($line_fields) > 1) {
             // Used by the #pre_render callback to group fields inline.
             $element['container' . $line_index] = ['#type' => 'container', '#attributes' => ['class' => ['address-container-inline']]];
         }
         foreach ($line_fields as $field_index => $field) {
             $property = FieldHelper::getPropertyName($field);
             $class = str_replace('_', '-', $property);
             $element[$property] = ['#type' => 'textfield', '#title' => $labels[$field], '#default_value' => isset($values[$property]) ? $values[$property] : '', '#required' => in_array($field, $required_fields), '#size' => isset($this->sizeAttributes[$field]) ? $this->sizeAttributes[$field] : 60, '#attributes' => ['class' => [$class], 'autocomplete' => FieldHelper::getAutocompleteAttribute($field)]];
             if (count($line_fields) > 1) {
                 $element[$property]['#group'] = $line_index;
             }
         }
     }
     // Hide the label for the second address line.
     if (isset($element['address_line2'])) {
         $element['address_line2']['#title_display'] = 'invisible';
     }
     // Hide fields that have been disabled in the address field settings.
     $enabled_fields = array_filter($this->getFieldSetting('fields'));
     $disabled_fields = array_diff(AddressField::getAll(), $enabled_fields);
     foreach ($disabled_fields as $field) {
         $property = FieldHelper::getPropertyName($field);
         $element[$property]['#access'] = FALSE;
     }
     // Add predefined options to the created subdivision elements.
     $element = $this->processSubdivisionElements($element, $values, $address_format);
     return $element;
 }