class SetupForm in Acquia Connector 8
Same name and namespace in other branches
- 8.2 src/Form/SetupForm.php \Drupal\acquia_connector\Form\SetupForm
- 3.x src/Form/SetupForm.php \Drupal\acquia_connector\Form\SetupForm
Acquia Connector setup form.
@package Drupal\acquia_connector\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\acquia_connector\Form\SetupForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of SetupForm
1 string reference to 'SetupForm'
File
- src/
Form/ SetupForm.php, line 20
Namespace
Drupal\acquia_connector\FormView source
class SetupForm extends ConfigFormBase {
/**
* The Acquia client.
*
* @var \Drupal\acquia_connector\Client
*/
protected $client;
/**
* Constructs a \Drupal\system\ConfigFormBase object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\acquia_connector\Client $client
* The Acquia client.
*/
public function __construct(ConfigFactoryInterface $config_factory, Client $client) {
$this->configFactory = $config_factory;
$this->client = $client;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('acquia_connector.client'));
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'acquia_connector.settings',
'acquia_search.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'acquia_connector_automatic_setup_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$storage = $form_state
->getStorage();
if (empty($storage['choose'])) {
return $this
->buildSetupForm($form_state);
}
else {
return $this
->buildChooseForm($form_state);
}
}
/**
* Build setup form.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*
* @return array
* Form.
*/
protected function buildSetupForm(FormStateInterface &$form_state) {
$form = [
'#prefix' => $this
->t('Log in or <a href=":url">configure manually</a> to connect your site to the Acquia Subscription.', [
':url' => Url::fromRoute('acquia_connector.credentials')
->toString(),
]),
'email' => [
'#type' => 'textfield',
'#title' => $this
->t('Enter the email address you use to login to the Acquia Subscription:'),
'#required' => TRUE,
],
'pass' => [
'#type' => 'password',
'#title' => $this
->t('Enter your Acquia Subscription password:'),
'#description' => $this
->t('Your password will not be stored locally and will be sent securely to Acquia.com. <a href=":url" target="_blank">Forgot password?</a>', [
':url' => Url::fromUri('https://accounts.acquia.com/user/password')
->getUri(),
]),
'#size' => 32,
'#required' => TRUE,
],
'actions' => [
'#type' => 'actions',
'continue' => [
'#type' => 'submit',
'#value' => $this
->t('Next'),
],
'signup' => [
'#markup' => $this
->t('Need a subscription? <a href=":url">Get one</a>.', [
':url' => Url::fromUri('https://www.acquia.com/acquia-cloud-free')
->getUri(),
]),
],
],
];
return $form;
}
/**
* Build choose form.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*
* @return array
* Form.
*/
protected function buildChooseForm(FormStateInterface &$form_state) {
$options = [];
$storage = $form_state
->getStorage();
foreach ($storage['response']['subscription'] as $credentials) {
$options[] = $credentials['name'];
}
asort($options);
$form = [
'#prefix' => $this
->t('You have multiple subscriptions available.'),
'subscription' => [
'#type' => 'select',
'#title' => $this
->t('Available subscriptions'),
'#options' => $options,
'#description' => $this
->t('Choose from your available subscriptions.'),
'#required' => TRUE,
],
'continue' => [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$storage = $form_state
->getStorage();
if (!isset($storage['choose'])) {
try {
$response = $this->client
->getSubscriptionCredentials($form_state
->getValue('email'), $form_state
->getValue('pass'));
} catch (ConnectorException $e) {
// Set form error to prevent switching to the next page.
if ($e
->isCustomized()) {
$form_state
->setErrorByName('', $e
->getCustomMessage());
}
else {
$this
->getLogger('acquia connector')
->error($e
->getMessage());
$form_state
->setErrorByName('', $this
->t("Can't connect to the Acquia Subscription."));
}
}
if (!empty($response)) {
$storage['response'] = $response;
}
}
$form_state
->setStorage($storage);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_data = $form_state
->getStorage();
if (isset($form_data['choose']) && isset($form_data['response']['subscription'][$form_state
->getValue('subscription')])) {
$config = $this
->config('acquia_connector.settings');
$sub = $form_data['response']['subscription'][$form_state
->getValue('subscription')];
$config
->set('subscription_name', $sub['name'])
->save();
$storage = new Storage();
$storage
->setKey($sub['key']);
$storage
->setIdentifier($sub['identifier']);
}
else {
$this
->automaticStartSubmit($form_state);
}
// Don't set message or redirect if multistep.
if (!$form_state
->getErrors() && $form_state
->isRebuilding() === FALSE) {
// Check subscription and send a heartbeat to Acquia via XML-RPC.
// Our status gets updated locally via the return data.
$subscription = new Subscription();
$subscription_data = $subscription
->update();
// Redirect to the path without the suffix.
if ($subscription_data) {
$form_state
->setRedirect('acquia_connector.settings');
}
if ($subscription_data['active']) {
$this
->messenger()
->addStatus($this
->t('<h3>Connection successful!</h3>You are now connected to Acquia Cloud. Please enter a name for your site to begin sending profile data.'));
drupal_flush_all_caches();
}
}
}
/**
* Submit automatically if one subscription found.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*/
protected function automaticStartSubmit(FormStateInterface &$form_state) {
$config = $this
->config('acquia_connector.settings');
$storage = $form_state
->getStorage();
if (empty($storage['response']['subscription'])) {
$this
->messenger()
->addError($this
->t('No subscriptions were found for your account.'));
}
elseif (count($storage['response']['subscription']) > 1) {
// Multistep form for choosing from available subscriptions.
$storage['choose'] = TRUE;
// Force rebuild with next step.
$form_state
->setRebuild(TRUE);
$form_state
->setStorage($storage);
}
else {
// One subscription so set id/key pair.
$sub = $storage['response']['subscription'][0];
$config
->set('subscription_name', $sub['name'])
->save();
$storage = new Storage();
$storage
->setKey($sub['key']);
$storage
->setIdentifier($sub['identifier']);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
SetupForm:: |
protected | property | The Acquia client. | |
SetupForm:: |
protected | function | Submit automatically if one subscription found. | |
SetupForm:: |
protected | function | Build choose form. | |
SetupForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
SetupForm:: |
protected | function | Build setup form. | |
SetupForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
SetupForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
SetupForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SetupForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
SetupForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
SetupForm:: |
public | function |
Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |