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 extends ConfigFormBase {
protected $content;
public function __construct() {
$this->content = $this
->getContent();
}
public function getFormId() {
return 'tacjs_add_services';
}
protected function getEditableConfigNames() {
return [
'tacjs.settings',
];
}
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] = [
'#type' => 'checkbox',
'#title' => $value['about']['name'],
'#default_value' => $config
->get('services.' . $service),
];
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]['user_' . $match[1][$i]] = [
'#type' => 'textfield',
'#title' => $match[1][$i],
'#default_value' => $config
->get('user.' . $match[1][$i]),
'#title_display' => 'invisible',
'#attributes' => [
'placeholder' => $match[2][$i],
],
'#states' => [
'visible' => [
':input[name="service_' . $service . '"]' => [
'checked' => TRUE,
],
],
],
];
}
}
}
}
$form['service']['type_analytic']['user_multiplegtagUa']['#attributes']['placeholder'] = 'UA-XXXXXXXX-X,UA-XXXXXXXX-X,UA-XXXXXXXX-X';
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('tacjs.settings');
foreach ($this->content as $type => $services) {
foreach ($services as $service => $value) {
$form_state
->setValue('service_' . $service, $form_state
->getValue('service_' . $service) ? TRUE : FALSE);
}
}
$config
->save();
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$content = $this
->getContent();
$config = $this
->config('tacjs.settings');
foreach ($this->content as $type => $services) {
foreach ($services as $service => $value) {
$config
->set('services.' . $service, $form_state
->getValue('service_' . $service));
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++) {
$config
->set('user.' . $match[1][$i], $form_state
->getValue('user_' . $match[1][$i]));
}
}
}
}
$config
->save();
parent::submitForm($form, $form_state);
}
protected function getContent() {
$relative_path = dirname(__DIR__, 3);
$content_json_path = $relative_path . '/assets/data/content.json';
$content_json = file_get_contents($content_json_path);
return Json::decode($content_json);
}
}