You are here

AddServices.php in TacJS 8.6

File

src/Form/Steps/AddServices.php
View source
<?php

namespace Drupal\tacjs\Form\Steps;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Unicode;

/**
 * Class AddServices.
 *
 * @package Drupal\tacjs\Form
 */
class AddServices extends ConfigFormBase {

  /**
   * The tarteaucitron.js services.
   *
   * @var array
   */
  protected $content;

  /**
   * Constructs a new AddServices.
   *
   */
  public function __construct() {
    $this->content = $this
      ->getContent();
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('tacjs.settings');
    $form['services'] = [
      '#type' => 'vertical_tabs',
      '#title' => $this
        ->t('Add Services'),
      '#title_display' => 'invisible',
    ];
    foreach ($this->content as $type => $services) {
      $form['service']['type_' . $type] = [
        '#type' => 'details',
        '#title' => Unicode::ucfirst($type),
        '#group' => 'services',
      ];
      foreach ($services as $service => $value) {
        $form['service']['type_' . $type]['service_' . $service . '_status'] = [
          '#type' => 'checkbox',
          '#title' => $value['about']['name'],
          '#default_value' => $config
            ->get('services.' . $service . '.status'),
        ];
        $form['service']['type_' . $type]['details_' . $service] = [
          '#type' => 'fieldset',
          '#title' => 'Configuration',
          '#states' => [
            'visible' => [
              ':input[name="service_' . $service . '_status"]' => [
                'checked' => TRUE,
              ],
            ],
          ],
        ];
        $form['service']['type_' . $type]['details_' . $service]['service_' . $service . '_readMore'] = [
          '#type' => 'textarea',
          '#title' => $this
            ->t('More info'),
          '#default_value' => $config
            ->get('services.' . $service . '.readMore'),
        ];
        $form['service']['type_' . $type]['details_' . $service]['service_' . $service . '_needConsent'] = [
          '#type' => 'checkbox',
          '#title' => $this
            ->t('Need consent'),
          '#default_value' => $config
            ->get('services.' . $service . '.needConsent'),
        ];
        if (preg_match_all('/tarteaucitron.user.([A-z]+)[\\s]+=[\\s]+[\\]?[\\"]?[\']?###([^.]+)###/m', $value['code']['js'] ?: (is_array($value['code']['html']) ? '' : $value['code']['html']), $match)) {
          for ($i = 0; $i < count($match[1]); $i++) {
            $form['service']['type_' . $type]['details_' . $service]['user_' . $match[1][$i]] = [
              '#type' => 'textfield',
              '#title' => $match[1][$i],
              '#default_value' => $config
                ->get('user.' . $match[1][$i]),
              '#title_display' => 'invisible',
              '#maxlength' => 255,
              '#attributes' => [
                'placeholder' => $match[2][$i],
              ],
            ];
          }
        }
      }
    }

    // Workaround for #3170835: Uncaught TypeError null user
    // See https://github.com/AmauriC/tarteaucitron.js/pull/394
    $form['service']['type_analytic']['user_multiplegtagUa']['#attributes']['placeholder'] = 'UA-XXXXXXXX-X, UA-XXXXXXXX-X, UA-XXXXXXXX-X';

    // 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';
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    foreach ($this->content as $type => $services) {
      foreach ($services as $service => $value) {
        $form_state
          ->setValue('service_' . $service . '_status', $form_state
          ->getValue('service_' . $service . '_status') ? TRUE : FALSE);
        $form_state
          ->setValue('service_' . $service . '_needConsent', $form_state
          ->getValue('service_' . $service . '_needConsent') ? TRUE : FALSE);
        if (preg_match_all('/tarteaucitron.user.([A-z]+)[\\s]+=[\\s]+[\\]?[\\"]?[\']?###([^.]+)###/m', $value['code']['js'] ?: (is_array($value['code']['html']) ? '' : $value['code']['html']), $match)) {
          for ($i = 0; $i < count($match[1]); $i++) {
            $form_state
              ->setValue('user_' . $match[1][$i], $form_state
              ->getValue('user_' . $match[1][$i]) ?: NULL);
          }
        }
      }
    }
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('tacjs.settings');
    foreach ($this->content as $type => $services) {
      foreach ($services as $service => $value) {
        $config
          ->set('services.' . $service, [
          "status" => $form_state
            ->getValue('service_' . $service . '_status'),
          "needConsent" => $form_state
            ->getValue('service_' . $service . '_needConsent'),
          "readMore" => $form_state
            ->getValue('service_' . $service . '_readMore'),
          "readmoreLink" => $form_state
            ->getValue('service_' . $service . '_readmoreLink'),
        ]);
        if (preg_match_all('/tarteaucitron.user.([A-z]+)[\\s]+=[\\s]+[\\]?[\\"]?[\']?###([^.]+)###/m', $value['code']['js'] ?: (is_array($value['code']['html']) ? '' : $value['code']['html']), $match)) {
          for ($i = 0; $i < count($match[1]); $i++) {
            if ($form_state
              ->getValue('user_' . $match[1][$i])) {
              $config
                ->set('user.' . $match[1][$i], $form_state
                ->getValue('user_' . $match[1][$i]));
            }
            else {
              $config
                ->clear('user.' . $match[1][$i]);
            }
          }
        }
      }
    }
    $config
      ->save();
    parent::submitForm($form, $form_state);
  }

  /**
   * Helper function for the tacjs.add_services form.
   *
   * Default tarteaucitron.js services.
   *
   * You can copy content directly from the following URL:
   * https://opt-out.ferank.eu/json.php
   *
   * @return array
   */
  protected function getContent() {

    // Get the module base path.
    $relative_path = dirname(__DIR__, 3);

    // Get the content.json file from the base path.
    $content_json_path = $relative_path . '/assets/data/content.json';

    // Load file content.
    $content_json = file_get_contents($content_json_path);
    return Json::decode($content_json);
  }

}

Classes

Namesort descending Description
AddServices Class AddServices.