You are here

class ResourceHintsConfigForm in Resource Hints 8

Configure resource hints for this site.

Hierarchy

Expanded class hierarchy of ResourceHintsConfigForm

1 file declares its use of ResourceHintsConfigForm
resource_hints.module in ./resource_hints.module
Module hooks for the resource hints module.
1 string reference to 'ResourceHintsConfigForm'
resource_hints.routing.yml in ./resource_hints.routing.yml
resource_hints.routing.yml

File

src/Form/ResourceHintsConfigForm.php, line 11

Namespace

Drupal\resource_hints\Form
View source
class ResourceHintsConfigForm extends ConfigFormBase {
  const OUTPUT_LINK_HEADER = 0;
  const OUTPUT_LINK_ELEMENT = 1;
  const DNS_PREFETCH_ENABLED = 'on';
  const DNS_PREFETCH_DISABLED = 'off';

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('resource_hints.settings');
    $form['dns_prefetch'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('DNS Prefetch'),
      '#open' => TRUE,
    ];
    $form['dns_prefetch']['dns_prefetch_output'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Output type'),
      '#options' => [
        self::OUTPUT_LINK_HEADER => $this
          ->t('Link Header'),
        self::OUTPUT_LINK_ELEMENT => $this
          ->t('Link Element'),
      ],
      '#default_value' => $config
        ->get('dns_prefetch_output'),
      '#description' => $this
        ->t('Resource hints can be output as an HTTP Link header or HTML link element'),
    ];
    $form['dns_prefetch']['dns_prefetch_resources'] = [
      '#type' => 'textarea',
      '#default_value' => implode(PHP_EOL, $config
        ->get('dns_prefetch_resources')),
      '#title' => $this
        ->t('Resources'),
      '#description' => $this
        ->t('The DNS resources you wish to be prefetched. Enter one resource per line.'),
    ];
    $form['dns_prefetch']['dns_prefetch_control'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('DNS Prefetch Control'),
      '#options' => [
        self::DNS_PREFETCH_ENABLED => $this
          ->t('Enabled'),
        self::DNS_PREFETCH_DISABLED => $this
          ->t('Disabled'),
      ],
      '#default_value' => $config
        ->get('dns_prefetch_control'),
      '#description' => $this
        ->t('By default browsers will not use DNS prefetching when a page is served via HTTPS, you must explicitly enable prefetching for HTTPS. Disabling prefetching will prevent browsers using prefetching and any inline attempts to enable it will be ignored.'),
    ];
    $form['preconnect'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Preconnect'),
      '#open' => TRUE,
    ];
    $form['preconnect']['preconnect_output'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Output type'),
      '#options' => [
        self::OUTPUT_LINK_HEADER => $this
          ->t('Link Header'),
        self::OUTPUT_LINK_ELEMENT => $this
          ->t('Link Element'),
      ],
      '#default_value' => $config
        ->get('preconnect_output'),
      '#description' => $this
        ->t('Resource hints can be output as an HTTP Link header or HTML link element'),
    ];
    $form['preconnect']['preconnect_resources'] = [
      '#type' => 'textarea',
      '#default_value' => implode(PHP_EOL, $config
        ->get('preconnect_resources')),
      '#title' => $this
        ->t('Resources'),
      '#description' => $this
        ->t('The resources you wish to be preconnected. Enter one resource per line.'),
    ];
    $form['prefetch'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Prefetch'),
      '#open' => TRUE,
    ];
    $form['prefetch']['prefetch_output'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Output type'),
      '#options' => [
        self::OUTPUT_LINK_HEADER => $this
          ->t('Link Header'),
        self::OUTPUT_LINK_ELEMENT => $this
          ->t('Link Element'),
      ],
      '#default_value' => $config
        ->get('prefetch_output'),
      '#description' => $this
        ->t('Resource hints can be output as an HTTP Link header or HTML link element'),
    ];
    $form['prefetch']['prefetch_resources'] = [
      '#type' => 'textarea',
      '#default_value' => implode(PHP_EOL, $config
        ->get('prefetch_resources')),
      '#title' => $this
        ->t('Resources'),
      '#description' => $this
        ->t('The resources you wish to be prefetched. Enter one resource per line.'),
    ];
    $form['prerender'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Prerender'),
      '#open' => TRUE,
    ];
    $form['prerender']['prerender_output'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Output type'),
      '#options' => [
        self::OUTPUT_LINK_HEADER => $this
          ->t('Link Header'),
        self::OUTPUT_LINK_ELEMENT => $this
          ->t('Link Element'),
      ],
      '#default_value' => $config
        ->get('prerender_output'),
      '#description' => $this
        ->t('Resource hints can be output as an HTTP Link header or HTML link element'),
    ];
    $form['prerender']['prerender_resources'] = [
      '#type' => 'textarea',
      '#default_value' => implode(PHP_EOL, $config
        ->get('prerender_resources')),
      '#title' => $this
        ->t('Resources'),
      '#description' => $this
        ->t('The resources you wish to be prerendered. Enter one resource per line.'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $dns_prefetch_resources = explode(PHP_EOL, $form_state
      ->getValue('dns_prefetch_resources'));
    $preconnect_resources = explode(PHP_EOL, $form_state
      ->getValue('preconnect_resources'));
    $prefetch_resources = explode(PHP_EOL, $form_state
      ->getValue('prefetch_resources'));
    $prerender_resources = explode(PHP_EOL, $form_state
      ->getValue('prerender_resources'));
    $config = \Drupal::service('config.factory')
      ->getEditable('resource_hints.settings');
    $config
      ->set('dns_prefetch_resources', $dns_prefetch_resources)
      ->set('dns_prefetch_output', $form_state
      ->getValue('dns_prefetch_output'))
      ->set('dns_prefetch_control', $form_state
      ->getValue('dns_prefetch_control'))
      ->set('preconnect_resources', $preconnect_resources)
      ->set('preconnect_output', $form_state
      ->getValue('preconnect_output'))
      ->set('prefetch_resources', $prefetch_resources)
      ->set('prefetch_output', $form_state
      ->getValue('prefetch_output'))
      ->set('prerender_resources', $prerender_resources)
      ->set('prerender_output', $form_state
      ->getValue('prerender_output'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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.
ResourceHintsConfigForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
ResourceHintsConfigForm::DNS_PREFETCH_DISABLED constant
ResourceHintsConfigForm::DNS_PREFETCH_ENABLED constant
ResourceHintsConfigForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
ResourceHintsConfigForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ResourceHintsConfigForm::OUTPUT_LINK_ELEMENT constant
ResourceHintsConfigForm::OUTPUT_LINK_HEADER constant
ResourceHintsConfigForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
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.