Commit d42db88c authored by Frank Mably's avatar Frank Mably
Browse files

Issue #3317075: Small refactoring to allow simpler implementation of a...

Issue #3317075: Small refactoring to allow simpler implementation of a multi-domain version of TacJS
parent b49b2f63
Loading
Loading
Loading
Loading
+37 −6
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\tacjs\Form\Steps;

use Drupal\Core\Config\Config;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
@@ -78,10 +79,22 @@ class AddServices extends ConfigFormBase {
  }

  /**
   * {@inheritdoc}
   * Get configuration.
   * @return \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('tacjs.settings');
  protected function getConfig() {
    return $this->config('tacjs.settings');
  }

  /**
   * Add elements to form.
   *
   * @param array $form
   * @param \Drupal\Core\Config\Config $config
   *
   * @return void
   */
  protected function addFormElements(array &$form, Config $config) {

    foreach ($this->languageManager->getLanguages() as $key => $value) {
      $langcodes[$key] = $value->getName();
@@ -171,6 +184,15 @@ class AddServices extends ConfigFormBase {
    // Workaround for #3204238: Uncaught TypeError null user
    // See https://www.drupal.org/project/tacjs/issues/3204238
    $form['service']['type_api']['user_googleFonts']['#attributes']['placeholder'] = 'My Font, My Other:300,700';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->getConfig();

    $this->addFormElements($form, $config);

    return parent::buildForm($form, $form_state);
  }
@@ -200,10 +222,10 @@ class AddServices extends ConfigFormBase {
  }

  /**
   * {@inheritdoc}
   * Process submitted configuration.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->config('tacjs.settings');
  protected function processSubmit(FormStateInterface $form_state, Config $config) {

    $userSettings = [];

    foreach ($this->content as $services) {
@@ -235,6 +257,15 @@ class AddServices extends ConfigFormBase {
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->getConfig();

    $this->processSubmit($form_state, $config);

    $config->save();

+47 −8
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\tacjs\Form\Steps;

use Drupal\Core\Config\Config;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

@@ -27,10 +28,22 @@ class EditTexts extends ConfigFormBase {
  }

  /**
   * {@inheritdoc}
   * Get configuration.
   * @return \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('tacjs.settings');
  protected function getConfig() {
    return $this->config('tacjs.settings');
  }

  /**
   * Add elements to form.
   *
   * @param array $form
   * @param \Drupal\Core\Config\Config $config
   *
   * @return void
   */
  protected function addFormElements(array &$form, Config $config) {

    $form['middleBarHead'] = [
      '#type' => 'textarea',
@@ -395,17 +408,26 @@ class EditTexts extends ConfigFormBase {
      '#default_value' => $config->get('texts.mandatoryText'),
      '#placeholder' => $this->t('This site uses cookies necessary for its proper functioning which cannot be deactivated.'),
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->config('tacjs.settings');
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->getConfig();

    $this->addFormElements($form, $config);

    return parent::buildForm($form, $form_state);
  }

    $elements = [
  /**
   * Get config key to form_state element mappings.
   *
   * @return string[]
   */
  protected function getConfigElementMappings(): array {
    return [
      'middleBarHead' => 'middleBarHead',
      'adblock' => 'adblock',
      'adblock_call' => 'adblock_call',
@@ -459,6 +481,14 @@ class EditTexts extends ConfigFormBase {
      'mandatoryTitle' => 'mandatoryTitle',
      'mandatoryText' => 'mandatoryText',
    ];
  }

  /**
   * Process submitted configuration.
   */
  protected function processSubmit(FormStateInterface $form_state, Config $config) {

    $elements = $this->getConfigElementMappings();

    foreach ($elements as $key => $element) {
      if ($form_state->getValue($element)) {
@@ -468,6 +498,15 @@ class EditTexts extends ConfigFormBase {
        $config->clear('texts.' . $key);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->getConfig();

    $this->processSubmit($form_state, $config);

    $config->save();

+39 −8
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\tacjs\Form\Steps;

use Drupal\Core\Config\Config;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

@@ -27,10 +28,22 @@ class ManageDialog extends ConfigFormBase {
  }

  /**
   * {@inheritdoc}
   * Get configuration.
   * @return \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('tacjs.settings');
  protected function getConfig() {
    return $this->config('tacjs.settings');
  }

  /**
   * Add elements to form.
   *
   * @param array $form
   * @param \Drupal\Core\Config\Config $config
   *
   * @return void
   */
  protected function addFormElements(array &$form, Config $config) {

    $form['privacyUrl'] = [
      '#type' => 'textfield',
@@ -251,6 +264,15 @@ class ManageDialog extends ConfigFormBase {
      '#min' => 1,
      '#max' => 365,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->getConfig();

    $this->addFormElements($form, $config);

    return parent::buildForm($form, $form_state);
  }
@@ -281,10 +303,9 @@ class ManageDialog extends ConfigFormBase {
  }

  /**
   * {@inheritdoc}
   * Process submitted configuration.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->config('tacjs.settings');
  protected function processSubmit(FormStateInterface $form_state, Config $config) {

    $config
      ->set('dialog.privacyUrl', $form_state->getValue('privacyUrl'))
@@ -314,8 +335,18 @@ class ManageDialog extends ConfigFormBase {
      ->set('dialog.mandatoryCta', $form_state->getValue('mandatoryCta'))
      ->set('dialog.closePopup', $form_state->getValue('closePopup'))
      ->set('dialog.customCloserId', $form_state->getValue('customCloserId'))
      ->set('expire', $form_state->getValue('expire'))
      ->save();
      ->set('expire', $form_state->getValue('expire'));
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->getConfig();

    $this->processSubmit($form_state, $config);

    $config->save();

    parent::submitForm($form, $form_state);
  }