You are here

class SpiChangeForm in Acquia Connector 3.x

Same name and namespace in other branches
  1. 8.2 src/Form/SpiChangeForm.php \Drupal\acquia_connector\Form\SpiChangeForm
  2. 8 src/Form/SpiChangeForm.php \Drupal\acquia_connector\Form\SpiChangeForm

Change SPI Data form.

@package Drupal\acquia_connector\Form

Hierarchy

Expanded class hierarchy of SpiChangeForm

1 string reference to 'SpiChangeForm'
acquia_connector.routing.yml in ./acquia_connector.routing.yml
acquia_connector.routing.yml

File

src/Form/SpiChangeForm.php, line 16

Namespace

Drupal\acquia_connector\Form
View source
class SpiChangeForm extends ConfigFormBase {

  /**
   * The state interface.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * Constructs a \Drupal\acquia_connector\Form object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\State\StateInterface $state
   *   The State handler.
   */
  public function __construct(ConfigFactoryInterface $config_factory, StateInterface $state) {
    parent::__construct($config_factory);
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('state'));
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'acquia_connector.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'acquia_connector_spi_change_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('acquia_connector.settings');
    $blocked = $config
      ->get('spi.blocked');
    $acquia_hosted = \Drupal::service('acquia_connector.spi')
      ->checkAcquiaHosted();
    $environment_change = \Drupal::service('acquia_connector.spi')
      ->checkEnvironmentChange();
    if (!$environment_change && !$blocked) {
      $form['#markup'] = $this
        ->t("<h2>No changes detected</h2><p>This form is used to address changes in your site's environment. No changes are currently detected.</p>");
      return $form;
    }
    elseif ($blocked) {
      $form['env_change_action'] = [
        '#type' => 'checkboxes',
        '#title' => $this
          ->t('The Acquia Connector is disabled and is not sending site profile data to Acquia Cloud for evaluation.'),
        '#options' => [
          'unblock' => $this
            ->t('Enable this site and send data to Acquia Cloud.'),
        ],
        '#required' => TRUE,
      ];
    }
    else {
      $env_changes = $config
        ->get('spi.environment_changes');
      $off_acquia_hosting = array_key_exists('acquia_hosted', $env_changes) && !$acquia_hosted;
      $form['env'] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('<strong>The following changes have been detected in your site environment:</strong>'),
        '#description' => [
          '#theme' => 'item_list',
          '#items' => $env_changes,
        ],
      ];
      $form['env_change_action'] = [
        '#type' => 'radios',
        '#title' => $this
          ->t('How would you like to proceed?'),
        '#options' => [
          'block' => $this
            ->t('Disable this site from sending profile data to Acquia Cloud.'),
          'update' => $this
            ->t('Update existing site with these changes.'),
          'create' => $this
            ->t('Track this as a new site on Acquia Cloud.'),
        ],
        '#required' => TRUE,
        '#default_value' => $config
          ->get('spi.environment_changed_action'),
      ];
      $form['identification'] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Site Identification'),
        '#collapsible' => FALSE,
        '#states' => [
          'visible' => [
            ':input[name="env_change_action"]' => [
              'value' => 'create',
            ],
          ],
        ],
      ];
      $form['identification']['site'] = [
        '#prefix' => '<div class="acquia-identification">',
        '#suffix' => '</div>',
        '#weight' => -2,
      ];
      $form['identification']['site']['name'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Name'),
        '#maxlength' => 255,
        '#required' => TRUE,
        '#default_value' => $this->state
          ->get('spi.site_name'),
      ];
      $form['identification']['site']['machine_name'] = [
        '#type' => 'machine_name',
        '#title' => $this
          ->t('Machine name'),
        '#maxlength' => 255,
        '#required' => TRUE,
        '#machine_name' => [
          'exists' => [
            $this,
            'exists',
          ],
          'source' => [
            'identification',
            'site',
            'name',
          ],
        ],
        '#default_value' => $this->state
          ->get('spi.site_machine_name'),
      ];
      if ($acquia_hosted) {
        $form['identification']['site']['name']['#disabled'] = TRUE;
        $form['identification']['site']['machine_name']['#disabled'] = TRUE;
        $form['identification']['site']['machine_name']['#default_value'] = \Drupal::service('acquia_connector.spi')
          ->getAcquiaHostedMachineName();
      }
      elseif ($off_acquia_hosting) {
        unset($form['env_change_action']['#options']['block']);
        unset($form['env_change_action']['#options']['update']);
        unset($form['env_change_action']['#states']);
        unset($form['identification']['site']['name']['#default_value']);
        unset($form['identification']['site']['machine_name']['#default_value']);
        $form['env_change_action']['#default_value'] = 'create';
        $form['env_change_action']['#access'] = FALSE;
      }
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * Determines if the machine name already exists.
   *
   * @return bool
   *   FALSE.
   */
  public function exists() {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $config = $this
      ->configFactory()
      ->getEditable('acquia_connector.settings');
    if (isset($values['env_change_action']['unblock']) && $values['env_change_action']['unblock'] == 'unblock') {
      $config
        ->set('spi.environment_changed_action', $values['env_change_action']['unblock'])
        ->save();
    }
    else {
      $config
        ->set('spi.environment_changed_action', $values['env_change_action'])
        ->save();
    }
    if ($values['env_change_action'] == 'create') {
      $this->state
        ->set('spi.site_name', $values['name']);
      $this->state
        ->set('spi.site_machine_name', $values['machine_name']);
    }
    parent::submitForm($form, $form_state);

    // Send information as soon as the key/identifier pair is submitted.
    $response = \Drupal::service('acquia_connector.spi')
      ->sendFullSpi(ACQUIA_CONNECTOR_ACQUIA_SPI_METHOD_CREDS);
    \Drupal::service('acquia_connector.spi')
      ->spiProcessMessages($response);
    $form_state
      ->setRedirect('system.status');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SpiChangeForm::$state protected property The state interface.
SpiChangeForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SpiChangeForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SpiChangeForm::exists public function Determines if the machine name already exists.
SpiChangeForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SpiChangeForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SpiChangeForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SpiChangeForm::__construct public function Constructs a \Drupal\acquia_connector\Form object. Overrides ConfigFormBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.