commerce-8.x-2.8/modules/order/src/Plugin/Field/FieldWidget/BillingProfileWidget.php
modules/order/src/Plugin/Field/FieldWidget/BillingProfileWidget.php
<?php
namespace Drupal\commerce_order\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of 'commerce_billing_profile'.
*
* @FieldWidget(
* id = "commerce_billing_profile",
* label = @Translation("Billing information"),
* field_types = {
* "entity_reference_revisions"
* }
* )
*/
class BillingProfileWidget extends WidgetBase implements ContainerFactoryPluginInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new BillingProfileWidget object.
*
* @param string $plugin_id
* The plugin_id for the widget.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the widget is associated.
* @param array $settings
* The widget settings.
* @param array $third_party_settings
* Any third party settings.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
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('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $items[$delta]->getEntity();
$store = $order->getStore();
if (!$items[$delta]->isEmpty()) {
$profile = $items[$delta]->entity;
}
else {
$profile = $this->entityTypeManager->getStorage('profile')->create([
'type' => 'customer',
'uid' => $order->getCustomerId(),
]);
}
$element['#type'] = 'fieldset';
$element['profile'] = [
'#type' => 'commerce_profile_select',
'#default_value' => $profile,
'#default_country' => $store->getAddress()->getCountryCode(),
'#available_countries' => $store->getBillingCountries(),
];
// Workaround for massageFormValues() not getting $element.
$element['array_parents'] = [
'#type' => 'value',
'#value' => array_merge($element['#field_parents'], [$items->getName(), 'widget', $delta]),
];
return $element;
}
/**
* {@inheritdoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
$new_values = [];
foreach ($values as $delta => $value) {
$element = NestedArray::getValue($form, $value['array_parents']);
$new_values[$delta]['entity'] = $element['profile']['#profile'];
}
return $new_values;
}
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
$entity_type = $field_definition->getTargetEntityTypeId();
$field_name = $field_definition->getName();
return $entity_type == 'commerce_order' && $field_name == 'billing_profile';
}
}
