commerce_amws-8.x-1.x-dev/src/Form/StoreForm.php
src/Form/StoreForm.php
<?php
namespace Drupal\commerce_amws\Form;
use Drupal\commerce_amws\Entity\StoreInterface as AmwsStoreInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines the Amazon MWS store add/edit form.
*/
class StoreForm extends EntityForm {
/**
* Constructs a ContentEntityForm object.
*
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger.
*/
public function __construct(MessengerInterface $messenger) {
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('messenger')
);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
/** @var \Drupal\commerce_amws\Entity\StoreInterface $store */
$store = $this->entity;
// Human label, machine name and description.
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#maxlength' => 255,
'#default_value' => $store->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $store->id(),
'#machine_name' => [
'exists' => '\Drupal\commerce_amws\Entity\Store::load',
],
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
];
$form['description'] = [
'#type' => 'textarea',
'#title' => $this->t('Description'),
'#default_value' => $store->getDescription(),
];
$form = $this->buildCredentialsForm($form, $store);
// Publication status.
$status = AmwsStoreInterface::STATUS_PUBLISHED;
if (!$store->isNew()) {
$status = $store->isPublished();
}
$form['status'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enabled'),
'#description' => $this->t('Only enabled stores will have their orders and products synchronized with Amazon MWS.'),
'#default_value' => $status,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$this->entity->save();
$this->messenger->addStatus($this->t(
'Saved the %label store.',
['%label' => $this->entity->label()]
));
$form_state->setRedirect('entity.commerce_amws_store.collection');
}
/**
* Builds the form elements related to the store credentials.
*
* @param array $form
* The form render array.
* @param \Drupal\commerce_amws\Entity\StoreInterface $store
* The Amazon MWS store.
*
* @return array
* The update form render array.
*/
protected function buildCredentialsForm(
array $form,
AmwsStoreInterface $store
) {
// Store credentials.
$form['credentials'] = [
'#type' => 'details',
'#title' => $this->t('Credentials'),
'#open' => TRUE,
];
$form['credentials']['seller_id'] = [
'#type' => 'textfield',
'#title' => $this->t('Seller ID'),
'#default_value' => $store->getSellerId(),
'#required' => TRUE,
];
$form['credentials']['marketplace_id'] = [
'#type' => 'textfield',
'#title' => $this->t('Marketplace ID'),
'#default_value' => $store->getMarketplaceId(),
'#required' => TRUE,
];
$form['credentials']['lwa_client_id'] = [
'#type' => 'textfield',
'#title' => $this->t('LWA Client ID'),
'#default_value' => $store->getLwaClientId(),
'#required' => TRUE,
];
$form['credentials']['security_warning'] = [
'#type' => 'inline_template',
'#template' => '<p>{{ text }}</p><p><code>$config[\'commerce_amws.commerce_amws_store.STORE_ID\'][\'lwa_client_secret\'] = \'LWA_CLIENT_SECRET\';</code><br /><code>$config[\'commerce_amws.commerce_amws_store.STORE_ID\'][\'lwa_refresh_token\'] = \'LWA_REFRESH_TOKEN\';</code><br /><code>$config[\'commerce_amws.commerce_amws_store.STORE_ID\'][\'aws_access_key_id\'] = \'AWS_ACCESS_KEY_ID\';</code><br /><code>$config[\'commerce_amws.commerce_amws_store.STORE_ID\'][\'aws_secret_access_key\'] = \'AWS_SECRET_ACCESS_KEY\';</code></p>',
'#context' => [
'text' => $this->t(
'For security reasons it is best that test/fake values are entered in
the following fields and in any exported configuration files. The
real credentials should be defined in your local settings file as
follows, where STORE_ID, LWA_CLIENT_SECRET, LWA_REFRESH_TOKEN,
AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are replaced with the
real values.'
),
],
];
$form['credentials']['lwa_client_secret'] = [
'#type' => 'textfield',
'#title' => $this->t('LWA Client Secret'),
'#default_value' => $store->getLwaClientSecret(),
'#required' => TRUE,
];
$form['credentials']['lwa_refresh_token'] = [
'#type' => 'textarea',
'#title' => $this->t('LWA Refresh Token'),
'#default_value' => $store->getLwaRefreshToken(),
'#required' => TRUE,
];
$form['credentials']['aws_access_key_id'] = [
'#type' => 'textfield',
'#title' => $this->t('AWS Access Key ID'),
'#default_value' => $store->getAwsAccessKeyId(),
'#required' => TRUE,
];
$form['credentials']['aws_secret_access_key'] = [
'#type' => 'textfield',
'#title' => $this->t('AWS Secret Access key'),
'#default_value' => $store->getAwsSecretAccessKey(),
'#required' => TRUE,
];
return $form;
}
}
