commerce_amws-8.x-1.x-dev/modules/shipping/src/Form/SettingsForm.php
modules/shipping/src/Form/SettingsForm.php
<?php
namespace Drupal\commerce_amws_shipping\Form;
use Drupal\commerce_amws_shipping\ShipmentService;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\ConfigFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Amazon MWS shipping settings form.
*/
class SettingsForm extends ConfigFormBase {
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The shipping method storage.
*
* @var \Drupal\commerce_shipping\ShippingMethodStorageInterface
*/
protected $shippingMethodStorage;
/**
* Constructs a new SettingsForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity_type_manager.
*/
public function __construct(
ConfigFactoryInterface $config_factory,
EntityFieldManagerInterface $entity_field_manager,
EntityTypeManagerInterface $entity_type_manager
) {
parent::__construct($config_factory);
$this->entityFieldManager = $entity_field_manager;
$this->shippingMethodStorage = $entity_type_manager->getStorage('commerce_shipping_method');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('entity_field.manager'),
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'commerce_amws_shipping_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['commerce_amws_shipping.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('commerce_amws_shipping.settings');
$form = $this->buildShippingMethodForm($form, $config);
$form = $this->buildPhoneFieldForm($form, $config);
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Store as NULL if we have no selection.
$phone_field = $form_state->getValue('phone_field_machine_name');
if (!$phone_field) {
$phone_field = NULL;
}
$this->config('commerce_amws_shipping.settings')
->set(
'shipping_method_id',
$form_state->getValue('shipping_method_id')
)
->set(
'phone_field_machine_name',
$phone_field
)
->save();
parent::submitForm($form, $form_state);
}
/**
* Builds the form element for selecting the shipping method.
*
* @param array $form
* The form render array.
* @param \Drupal\Core\Config\Config $config
* The configuration object.
*
* @return array
* The updated form render array.
*/
protected function buildShippingMethodForm(array $form, Config $config) {
$method_options = [];
$methods = $this->shippingMethodStorage->loadMultiple();
foreach ($methods as $method) {
$method_options[$method->id()] = $method->label();
}
$form['shipping_method_id'] = [
'#type' => 'select',
'#title' => $this->t('Shipping method'),
'#description' => $this->t(
'The shipping method that will be used for imported Amazon MWS orders.'
),
'#default_value' => $config->get('shipping_method_id'),
'#options' => $method_options,
'#required' => TRUE,
];
return $form;
}
/**
* Builds the form element for selecting the phone field.
*
* @param array $form
* The form render array.
* @param \Drupal\Core\Config\Config $config
* The configuration object.
*
* @return array
* The updated form render array.
*/
protected function buildPhoneFieldForm(array $form, Config $config) {
// Get eligible field options. These are all `telephone` fields on the
// customer profile type.
$field_name_options = ['' => 'Please select an option'];
$fields = $this->entityFieldManager->getFieldDefinitions(
'profile',
ShipmentService::DEFAULT_SHIPPING_PROFILE_TYPE
);
foreach ($fields as $field) {
if ($field->getType() !== 'telephone') {
continue;
}
$field_name = $field->getName();
if (isset($field_name_options[$field_name])) {
continue;
}
$field_name_options[$field_name] = $field->getLabel() . ' (' . $field_name . ')';
}
$form['phone_field_machine_name'] = [
'#type' => 'select',
'#title' => $this->t('Phone field'),
'#description' => $this->t(
'The field to use for storing the shipping address phone, when available
in Amazon MWS orders. Note that only the Customer profile is currently
supported.'
),
'#options' => $field_name_options,
'#default_value' => $config->get('phone_field_machine_name'),
];
return $form;
}
}
