You are here

TomeStaticCronSettingsForm.php in Tome 8

File

modules/tome_static/modules/tome_static_cron/src/Form/TomeStaticCronSettingsForm.php
View source
<?php

namespace Drupal\tome_static_cron\Form;

use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Configures Tome Static Cron settings for this site.
 *
 * @internal
 */
class TomeStaticCronSettingsForm extends ConfigFormBase {

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $config = $this
      ->config('tome_static_cron.settings');
    $form['base_url'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Base URL'),
      '#description' => $this
        ->t('The base URL to use for static cron runs.'),
      '#default_value' => $config
        ->get('base_url'),
      '#required' => TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if (!UrlHelper::isValid($form_state
      ->getValue('base_url'), TRUE)) {
      $form_state
        ->setError($form['base_url'], $this
        ->t('The provided URL is not valid.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $config = $this
      ->config('tome_static_cron.settings');
    $config
      ->set('base_url', $form_state
      ->getValue('base_url'));
    $config
      ->save();
  }

}

Classes

Namesort descending Description
TomeStaticCronSettingsForm Configures Tome Static Cron settings for this site.