View source
<?php
namespace Drupal\tawk_to\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Executable\ExecutableManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TawkToExtraSettingsForm extends ConfigFormBase {
use StringTranslationTrait;
protected $manager;
protected $language;
protected $contextRepository;
public function __construct(ConfigFactoryInterface $configFactory, ExecutableManagerInterface $manager, ContextRepositoryInterface $contextRepository, LanguageManagerInterface $language) {
parent::__construct($configFactory);
$this->manager = $manager;
$this->contextRepository = $contextRepository;
$this->language = $language;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('plugin.manager.condition'), $container
->get('context.repository'), $container
->get('language_manager'));
}
public function getFormId() {
return 'tawk_to_extra_settings_form';
}
protected function getEditableConfigNames() {
return [
'tawk_to.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form_state
->setTemporaryValue('gathered_contexts', $this->contextRepository
->getAvailableContexts());
$form['#tree'] = TRUE;
$form['visibility'] = $this
->buildVisibilityInterface([], $form_state);
$form['user'] = $this
->buildUserInfoInterface([], $form_state);
return parent::buildForm($form, $form_state);
}
protected function buildVisibilityInterface(array $form, FormStateInterface $form_state) {
$form['visibility_tabs'] = [
'#type' => 'vertical_tabs',
'#title' => $this
->t('Visibility'),
'#parents' => [
'visibility_tabs',
],
];
$visibility = $this
->config('tawk_to.settings')
->get('visibility');
foreach ($this->manager
->getDefinitionsForContexts($form_state
->getTemporaryValue('gathered_contexts')) as $condition_id => $definition) {
if ($condition_id == 'entity_bundle:webform_submission') {
continue;
}
if ($condition_id == 'current_theme') {
continue;
}
if ($condition_id == 'gtag_language') {
continue;
}
if ($condition_id == 'language' && !$this->language
->isMultilingual()) {
continue;
}
$condition = $this->manager
->createInstance($condition_id, isset($visibility[$condition_id]) ? $visibility[$condition_id] : []);
$form_state
->set([
'conditions',
$condition_id,
], $condition);
$condition_form = $condition
->buildConfigurationForm([], $form_state);
$condition_form['#type'] = 'details';
$condition_form['#title'] = $condition
->getPluginDefinition()['label'];
$condition_form['#group'] = 'visibility_tabs';
$form[$condition_id] = $condition_form;
}
if (isset($form['node_type'])) {
$form['node_type']['#title'] = $this
->t('Content types');
$form['node_type']['bundles']['#title'] = $this
->t('Content types');
$form['node_type']['negate']['#type'] = 'value';
$form['node_type']['negate']['#title_display'] = 'invisible';
$form['node_type']['negate']['#value'] = $form['node_type']['negate']['#default_value'];
}
if (isset($form['user_role'])) {
$form['user_role']['#title'] = $this
->t('Roles');
unset($form['user_role']['roles']['#description']);
$form['user_role']['negate']['#type'] = 'value';
$form['user_role']['negate']['#value'] = $form['user_role']['negate']['#default_value'];
}
if (isset($form['request_path'])) {
$form['request_path']['#title'] = $this
->t('Pages');
$form['request_path']['negate']['#type'] = 'radios';
$form['request_path']['negate']['#default_value'] = (int) $form['request_path']['negate']['#default_value'];
$form['request_path']['negate']['#title_display'] = 'invisible';
$form['request_path']['negate']['#options'] = [
$this
->t('Show for the listed pages'),
$this
->t('Hide for the listed pages'),
];
}
if (isset($form['language'])) {
$form['language']['negate']['#type'] = 'value';
$form['language']['negate']['#value'] = $form['language']['negate']['#default_value'];
}
return $form;
}
protected function buildUserInfoInterface(array $form, FormStateInterface $form_state) {
$settings = $this
->config('tawk_to.settings');
$form['user'] = [
'#type' => 'fieldset',
'#title' => $this
->t('User info settings'),
];
$form['user']['show_user_name'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show user name in the widget'),
'#default_value' => $settings
->get('show_user_name'),
];
$form['user']['user_name'] = [
'#type' => 'textfield',
'#title' => $this
->t('User name in the widget'),
'#description' => $this
->t('You can use tokens. E.g. [current-user:name].'),
'#default_value' => $settings
->get('user_name'),
'#states' => [
'visible' => [
':input[name*=show_user_name]' => [
'checked' => TRUE,
],
],
],
];
$form['user']['show_user_email'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show user email in the widget'),
'#default_value' => $settings
->get('show_user_name'),
];
$form['user']['user_email'] = [
'#type' => 'textfield',
'#title' => $this
->t('User email in the widget'),
'#description' => $this
->t('You can use tokens. E.g. [current-user:mail].'),
'#default_value' => $settings
->get('user_email'),
'#states' => [
'visible' => [
':input[name*=show_user_email]' => [
'checked' => TRUE,
],
],
],
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->submitVisibility($form, $form_state);
$this
->submitUserInfo($form, $form_state);
return parent::submitForm($form, $form_state);
}
protected function submitVisibility(array $form, FormStateInterface $form_state) {
$visibility = [];
foreach ($form_state
->getValue('visibility') as $condition_id => $values) {
$condition = $form_state
->get([
'conditions',
$condition_id,
]);
$condition
->submitConfigurationForm($form['visibility'][$condition_id], SubformState::createForSubform($form['visibility'][$condition_id], $form, $form_state));
if ($condition instanceof ContextAwarePluginInterface) {
$contextMapping = isset($values['context_mapping']) ? $values['context_mapping'] : [];
$condition
->setContextMapping($contextMapping);
}
$conditionConfiguration = $condition
->getConfiguration();
$visibility[$condition_id] = $conditionConfiguration;
}
$this
->config('tawk_to.settings')
->set('visibility', $visibility)
->save();
}
protected function submitUserInfo(array $form, FormStateInterface $form_state) {
$visibility = [];
foreach ($form_state
->getValue('user')['user'] as $key => $value) {
$this
->config('tawk_to.settings')
->set($key, $value)
->save();
}
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$this
->validateVisibility($form, $form_state);
}
protected function validateVisibility(array $form, FormStateInterface $form_state) {
foreach ($form_state
->getValue('visibility') as $conditionId => $values) {
if (array_key_exists('negate', $values)) {
$form_state
->setValue([
'visibility',
$conditionId,
'negate',
], (bool) $values['negate']);
}
$condition = $form_state
->get([
'conditions',
$conditionId,
]);
$condition
->validateConfigurationForm($form['visibility'][$conditionId], SubformState::createForSubform($form['visibility'][$conditionId], $form, $form_state));
}
}
}