View source
<?php
namespace Drupal\address\Plugin\Field\FieldWidget;
use CommerceGuys\Addressing\Country\CountryRepositoryInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\Element;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Validator\ConstraintViolationInterface;
class AddressDefaultWidget extends WidgetBase implements ContainerFactoryPluginInterface {
protected $countryRepository;
protected $eventDispatcher;
protected $configFactory;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, CountryRepositoryInterface $country_repository, EventDispatcherInterface $event_dispatcher, ConfigFactoryInterface $config_factory) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->countryRepository = $country_repository;
$this->eventDispatcher = $event_dispatcher;
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container
->get('address.country_repository'), $container
->get('event_dispatcher'), $container
->get('config.factory'));
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$item = $items[$delta];
$value = $item
->toArray();
$value['langcode'] = $item
->initializeLangcode();
$element += [
'#type' => 'details',
'#open' => TRUE,
];
$element['address'] = [
'#type' => 'address',
'#default_value' => $value,
'#required' => $this->fieldDefinition
->isRequired(),
'#available_countries' => $item
->getAvailableCountries(),
'#field_overrides' => $item
->getFieldOverrides(),
];
if ($this
->isDefaultValueWidget($form_state)) {
$element['address']['#after_build'][] = [
get_class($this),
'makeFieldsOptional',
];
}
return $element;
}
public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
$error_element = NestedArray::getValue($element['address'], $violation->arrayPropertyPath);
return is_array($error_element) ? $error_element : FALSE;
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
$new_values = [];
foreach ($values as $delta => $value) {
$new_values[$delta] = $value['address'];
}
return $new_values;
}
public static function makeFieldsOptional(array $element, FormStateInterface $form_state) {
foreach (Element::getVisibleChildren($element) as $key) {
if (!empty($element[$key]['#required'])) {
$element[$key]['#required'] = FALSE;
}
}
return $element;
}
}