/** * {@inheritdoc} */ public function render(ResultRow $values) { $value = $this->getValue($values); if (!empty($this->options['display_name']) && !empty($value)) { $countries = $this->countryRepository->getList(); if (isset($countries[$value])) { $value = $countries[$value]; } } return $this->sanitizeValue($value); }
/** * {@inheritdoc} */ public function getValueOptions() { if (!isset($this->valueOptions)) { $entity_type_id = $this->getEntityType(); $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); $field_name = $this->getFieldName(); $available_countries = $this->getAvailableCountries($entity_type, $field_name); $countries = $this->countryRepository->getList(); if (!empty($available_countries)) { $countries = array_intersect_key($countries, $available_countries); } $this->valueOptions = $countries; } return $this->valueOptions; }
/** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { if ($value === null || $value === '') { return; } if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { throw new UnexpectedTypeException($value, 'string'); } $countries = $this->countryRepository->getList(); $value = (string) $value; if (!isset($countries[$value])) { if ($this->context instanceof \Symfony\Component\Validator\Context\ExecutionContextInterface) { $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->addViolation(); } else { $this->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->addViolation(); } } }
/** * Builds a renderable array for a single address item. * * @param \Drupal\address\AddressInterface $address * The address. * @param string $langcode * The language that should be used to render the field. * * @return array * A renderable array. */ protected function viewElement(AddressInterface $address, $langcode) { $country_code = $address->getCountryCode(); $countries = $this->countryRepository->getList(); $address_format = $this->addressFormatRepository->get($country_code, $address->getLocale()); $values = $this->getValues($address, $address_format); $element = ['#theme' => 'address_plain', '#recipient' => $values['recipient'], '#organization' => $values['organization'], '#address_line1' => $values['addressLine1'], '#address_line2' => $values['addressLine2'], '#postal_code' => $values['postalCode'], '#sorting_code' => $values['sortingCode'], '#administrative_area' => $values['administrativeArea'], '#locality' => $values['locality'], '#dependent_locality' => $values['dependentLocality'], '#country' => ['code' => $country_code, 'name' => $countries[$country_code]], '#cache' => ['contexts' => ['languages:' . LanguageInterface::TYPE_INTERFACE]]]; return $element; }
/** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { if (!$value instanceof AddressInterface) { throw new UnexpectedTypeException($value, 'AddressInterface'); } $address = $value; $countryCode = $address->getCountryCode(); if ($countryCode === NULL || $countryCode === '') { return; } $countries = $this->countryRepository->getList(); if (!isset($countries[$countryCode])) { $this->context->buildViolation($constraint->invalidMessage)->atPath('country_code')->setParameter('%value', $this->formatValue($countryCode))->addViolation(); return; } $availableCountries = $constraint->availableCountries; if (!empty($availableCountries) && !in_array($countryCode, $availableCountries)) { $this->context->buildViolation($constraint->notAvailableMessage)->atPath('country_code')->setParameter('%value', $this->formatValue($countryCode))->addViolation(); } }
/** * Builds the view for the given address. * * @param AddressInterface $address The address. * @param AddressFormatInterface $addressFormat The address format. * * @return array The view. */ protected function buildView(AddressInterface $address, AddressFormatInterface $addressFormat) { $countries = $this->countryRepository->getList($this->locale); $values = $this->getValues($address, $addressFormat); $view = []; $view['country'] = ['html_tag' => 'span', 'html_attributes' => ['class' => 'country'], 'value' => $countries[$address->getCountryCode()]]; foreach ($addressFormat->getUsedFields() as $field) { // The constant is more suitable as a class than the value since // it's snake_case and not camelCase. $class = str_replace('_', '-', strtolower(AddressField::getKey($field))); $view[$field] = ['html_tag' => 'span', 'html_attributes' => ['class' => $class], 'value' => $values[$field]]; } return $view; }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $field_name = $this->fieldDefinition->getName(); $id_prefix = implode('-', array_merge($element['#field_parents'], [$field_name])); $wrapper_id = Html::getUniqueId($id_prefix . '-ajax-wrapper'); $item = $items[$delta]; $full_country_list = $this->countryRepository->getList(); $country_list = $full_country_list; $available_countries = $item->getAvailableCountries(); if (!empty($available_countries)) { $country_list = array_intersect_key($country_list, $available_countries); } // If the form has been rebuilt via AJAX, use the values from user input. // $form_state->getValues() can't be used here because it's empty due to // #limit_validaiton_errors. $parents = array_merge($element['#field_parents'], [$field_name, $delta]); $values = NestedArray::getValue($form_state->getUserInput(), $parents, $has_input); if (!$has_input) { $values = $item->isEmpty() ? $this->getInitialValues($country_list) : $item->toArray(); } $country_code = $values['country_code']; if (!empty($country_code) && !isset($country_list[$country_code])) { // This item's country is no longer available. Add it back to the top // of the list to ensure all data is displayed properly. The validator // can then prevent the save and tell the user to change the country. $missingElement = [$country_code => $full_country_list[$country_code]]; $country_list = $missingElement + $country_list; } // Calling initializeLangcode() every time, and not just when the field // is empty, ensures that the langcode can be changed on subsequent // edits (because the entity or interface language changed, for example). $langcode = $item->initializeLangcode(); $element += ['#type' => 'details', '#collapsible' => TRUE, '#open' => TRUE, '#prefix' => '<div id="' . $wrapper_id . '">', '#suffix' => '</div>', '#pre_render' => [['Drupal\\Core\\Render\\Element\\Details', 'preRenderDetails'], ['Drupal\\Core\\Render\\Element\\Details', 'preRenderGroup'], [get_class($this), 'groupElements']], '#after_build' => [[get_class($this), 'clearValues']], '#attached' => ['library' => ['address/form']], '#wrapper_id' => $wrapper_id]; $element['langcode'] = ['#type' => 'hidden', '#value' => $langcode]; // Hide the country dropdown when there is only one possible value. if (count($country_list) == 1 && $this->fieldDefinition->isRequired()) { $country_code = key($available_countries); $element['country_code'] = ['#type' => 'hidden', '#value' => $country_code]; } else { $element['country_code'] = ['#type' => 'select', '#title' => $this->t('Country'), '#options' => $country_list, '#default_value' => $country_code, '#required' => $element['#required'], '#limit_validation_errors' => [], '#ajax' => ['callback' => [get_class($this), 'ajaxRefresh'], 'wrapper' => $wrapper_id], '#attributes' => ['class' => ['country'], 'autocomplete' => 'country'], '#weight' => -100]; if (!$element['#required']) { $element['country_code']['#empty_value'] = ''; } } if (!empty($country_code)) { $element = $this->addressElements($element, $values); } return $element; }
/** * Builds a renderable array for a single address item. * * @param \Drupal\address\AddressInterface $address * The address. * @param string $langcode * The language that should be used to render the field. * * @return array * A renderable array. */ protected function viewElement(AddressInterface $address, $langcode) { $country_code = $address->getCountryCode(); $countries = $this->countryRepository->getList(); $address_format = $this->addressFormatRepository->get($country_code, $address->getLocale()); $values = $this->getValues($address, $address_format); $element = []; $element['address_format'] = ['#type' => 'value', '#value' => $address_format]; $element['country_code'] = ['#type' => 'value', '#value' => $country_code]; $element['country'] = ['#type' => 'html_tag', '#tag' => 'span', '#attributes' => ['class' => ['country']], '#value' => Html::escape($countries[$country_code]), '#placeholder' => '%country']; foreach ($address_format->getUsedFields() as $field) { $property = FieldHelper::getPropertyName($field); $class = str_replace('_', '-', $property); $element[$property] = ['#type' => 'html_tag', '#tag' => 'span', '#attributes' => ['class' => [$class]], '#value' => Html::escape($values[$field]), '#placeholder' => '%' . $field]; } return $element; }
/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form = parent::buildConfigurationForm($form, $form_state); $values = $form_state->getUserInput(); if ($values) { $values += $this->defaultConfiguration(); } else { $values = $this->configuration; } $wrapper_id = Html::getUniqueId('zone-members-ajax-wrapper'); $form += ['#prefix' => '<div id="' . $wrapper_id . '">', '#suffix' => '</div>', '#after_build' => [[get_class($this), 'clearValues']], '#wrapper_id' => $wrapper_id]; $form['country_code'] = ['#type' => 'select', '#title' => $this->t('Country'), '#options' => $this->countryRepository->getList(), '#default_value' => $values['country_code'], '#limit_validation_errors' => [], '#required' => TRUE, '#ajax' => ['callback' => [get_class($this), 'ajaxRefresh'], 'wrapper' => $wrapper_id]]; if (!empty($values['country_code'])) { $address_format = $this->addressFormatRepository->get($values['country_code']); $form = $this->buildSubdivisionElements($form, $values, $address_format); $form = $this->buildPostalCodeElements($form, $values, $address_format); } return $form; }
public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('countryCode', 'choice', ['choices' => $this->countryRepository->getList(), 'required' => true]); $builder->addEventSubscriber(new GenerateAddressFieldsSubscriber($this->addressFormatRepository, $this->subdivisionRepository)); }