You are here

class AdminForm in Evercurrent 8

Same name and namespace in other branches
  1. 8.2 src/Form/AdminForm.php \Drupal\evercurrent\Form\AdminForm

Class AdminForm.

@package Drupal\evercurrent\Form

Hierarchy

Expanded class hierarchy of AdminForm

1 string reference to 'AdminForm'
evercurrent.routing.yml in ./evercurrent.routing.yml
evercurrent.routing.yml

File

src/Form/AdminForm.php, line 20
Contains Drupal\evercurrent\Form\AdminForm.

Namespace

Drupal\evercurrent\Form
View source
class AdminForm extends ConfigFormBase {

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('evercurrent.admin_config');
    $form['send'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable sending update reports'),
      '#description' => $this
        ->t('Check this to enable sending information about available updates to the Ricochet Maintenance server.'),
      '#default_value' => $config
        ->get('send'),
      '#weight' => 1,
    ];
    $form['target_address'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Server URL'),
      '#description' => $this
        ->t('The target environment URL'),
      '#maxlength' => 300,
      '#size' => 40,
      '#default_value' => $config
        ->get('target_address'),
      '#weight' => 2,
    ];
    $form['key'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('API Key'),
      '#description' => $this
        ->t('The API key for this site. It should contain only lower case letters and numbers.
If you have development and staging environments,
you should not store the API key in this field, but in your production environment\'s settings.php as follows:
<i>$settings["evercurrent_environment_token"] = "myapikey";</i>
This is important if you are using different environments. See this module\'s documentation for more information.'),
      '#maxlength' => 32,
      '#size' => 32,
      '#default_value' => $config
        ->get('key'),
      '#weight' => 4,
    ];
    $form['details'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Advanced'),
      '#open' => FALSE,
      '#weight' => 5,
    ];
    $form['details']['listen'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Listen for new API key'),
      '#description' => $this
        ->t('If set, the module will listen for an API key sent from the Maintenance server. Once it has received an API key, it will immediately attempt to send updates to the maintenance server using this API key. If this update succeeds, the API key will be saved. When it is saved, the listening will be automatically stopped.'),
      '#default_value' => $config
        ->get('listen'),
    ];
    $form['details']['interval'] = [
      '#type' => 'select',
      '#title' => t('Report frequency'),
      '#description' => t('The frequency for sending updates to the server. Use this if your cron runs very often.'),
      '#default_value' => $config
        ->get('interval'),
      '#options' => [
        0 => t('Every time Cron runs'),
        3600 => t('Every hour'),
        3600 * 12 => t('Every 12 hours'),
        60 * 60 * 24 => t('Every 24 hours'),
      ],
    ];
    $settings_token = Settings::get('evercurrent_environment_token', NULL);
    if ($settings_token) {
      $form['override'] = array(
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Override API key stored in settings.php'),
        '#description' => $this
          ->t("An API key '<b>%key</b>' has been detected in your site's settings.php file.\nIf you want to override that key, check this box. The API key in the 'API key' field below will then be used instead.", array(
          '%key' => $settings_token,
        )),
        '#default_value' => $config
          ->get('override'),
        '#weight' => 3,
      );
      $form['key']['#states'] = array(
        'disabled' => array(
          ':input[name="override"]' => array(
            'checked' => FALSE,
          ),
        ),
      );
    }
    $form['send_now'] = array(
      '#type' => 'checkbox',
      '#title' => t('Send update report when saving configuration'),
      '#description' => t('Check this to attempt sending updates to the server immediately after you have saved this form.'),
      '#weight' => 10,
    );
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $this
      ->config('evercurrent.admin_config')
      ->set('send', $form_state
      ->getValue('send'))
      ->set('listen', $form_state
      ->getValue('listen'))
      ->set('target_address', $form_state
      ->getValue('target_address'))
      ->set('key', $form_state
      ->getValue('key'))
      ->set('interval', $form_state
      ->getValue('interval'))
      ->set('override', $form_state
      ->getValue('override'))
      ->save();
    if ($form_state
      ->getValue('send_now') == TRUE) {
      drupal_set_message('Attempting to contact server..');
      $updateHelper = \Drupal::service('evercurrent.update_helper');
      $result = $updateHelper
        ->sendUpdates(TRUE, NULL, TRUE);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdminForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
AdminForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
AdminForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AdminForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
AdminForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
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. 1
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. Overrides UrlGeneratorTrait::redirect
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.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
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. 29
MessengerTrait::messenger public function Gets the messenger. 29
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.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.