acquia_vwo-1.0.x-dev/src/Form/SettingsForm.php

src/Form/SettingsForm.php
<?php

namespace Drupal\acquia_vwo\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

// cspell:ignore vwoid
/**
 * Defines VWO Settings form.
 */
class SettingsForm extends ConfigFormBase {

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs a \Drupal\acquia_vwo\Form\SettingsForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
   *   The typed configuration manager.
   * @param \Drupal\Core\Extension\ModuleHandler $module_handler
   *   The module handler.
   */
  public function __construct(
    ConfigFactoryInterface $config_factory,
    TypedConfigManagerInterface $typed_config,
    ModuleHandler $module_handler,
  ) {
    parent::__construct($config_factory, $typed_config);
    $this->moduleHandler = $module_handler;
  }

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

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

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

    $this->moduleHandler->loadInclude('acquia_vwo', 'inc', 'acquia_vwo.help');
    $help = acquia_vwo_help_settings();
    $form['help'] = [
      '#markup' => '<p>' . implode('</p><p>', $help) . '</p>',
    ];

    $form['id_fieldset'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Account'),
    ];
    $form['id_fieldset']['id'] = [
      '#type' => 'textfield',
      '#title' => $this->t('VWO Account ID'),
      '#description' => implode('', [
        $this->t('Your numeric Account ID or placeholder "NONE". This is the value after <q>var _vis_opt_account_id =</q> in the VWO Smart Code.'),
        '<br /><strong>',
        $this->t('You can use the <a href=":url">Parse Account ID</a> tool to extract the Account ID from the full VWO Smart Code.', [
          ':url' => Url::fromRoute('acquia_vwo.settings.vwoid')->toString(),
        ]),
        '</strong>',
      ]),
      '#size' => 15,
      '#maxlength' => 20,
      '#required' => TRUE,
      '#config_target' => 'acquia_vwo.settings:id',
    ];

    $form['advanced'] = [
      '#type' => 'details',
      '#open' => TRUE,
      '#title' => $this->t('Script loading settings'),
      '#description' => implode('', [
        '<p>',
        $this->t('The VWO script will be loaded Asynchronously.'),
        '</p><p>',
        $this->t('Note that in normal circumstances, all the data and files that need to be download will get downloaded in 100-200 milliseconds, so the following timeouts are an absolute maximum threshold and can safely be kept as is.'),
        '</p><p>',
        $this->t('One possible side effect of decreasing these timeouts, would be users on slower internet connections reaching the timeout and as a result, missing out on becoming part of the test.'),
        '</p>',
      ]),
    ];

    $form['advanced']['timeout'] = [
      '#type' => 'number',
      '#title' => $this->t('A/B Test Download Timeout'),
      '#description' => $this->t('The maximum time in milliseconds the code snippet will wait for test settings to arrive from the VWO servers. If no settings arrive within this period, your original page will be displayed without tests. Default: 2000 ms.'),
      '#size' => 10,
      '#min' => 0,
      '#max' => 9999,
      '#required' => TRUE,
      '#config_target' => 'acquia_vwo.settings:loading.timeout',
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $vwoid = $form_state->getValue('id');
    if (!preg_match('/^\d+$/', $vwoid) && $vwoid != 'NONE') {
      $form_state->setErrorByName('id',
        $this->t('Your VWO ID must be numeric. If you have having issues locating it, please use the <a href=":parse_url">Extract Account Id tool</a>.', [
          ':parse_url' => Url::fromRoute('acquia_vwo.settings.vwoid')
            ->toString(),
        ])
      );
    }
  }

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

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc