View source
<?php
namespace Drupal\google_tag\Form;
use Drupal\Core\Condition\ConditionInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Executable\ExecutableManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContainerForm extends EntityForm {
use ContainerTrait;
protected $conditionManager;
protected $contextRepository;
public function getFormId() {
return 'google_tag_container';
}
public function __construct(ExecutableManagerInterface $condition_manager, ContextRepositoryInterface $context_repository) {
$this->conditionManager = $condition_manager;
$this->contextRepository = $context_repository;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.condition'), $container
->get('context.repository'));
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$container = $this->container = $this->entity;
$this->prefix = '';
$form_state
->setTemporaryValue('gathered_contexts', $this->contextRepository
->getAvailableContexts());
$form['label'] = [
'#type' => 'textfield',
'#title' => 'Label',
'#default_value' => $container
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $container
->id(),
'#required' => TRUE,
'#machine_name' => [
'exists' => [
$this,
'containerExists',
],
'replace_pattern' => '[^a-z0-9_.]+',
],
];
$form['settings'] = [
'#type' => 'vertical_tabs',
'#title' => $this
->t('Container settings'),
'#description' => $this
->t('The settings affecting the snippet contents for this container.'),
'#attributes' => [
'class' => [
'google-tag',
],
],
];
$form['conditions'] = [
'#type' => 'vertical_tabs',
'#title' => $this
->t('Insertion conditions'),
'#description' => $this
->t('The snippet insertion conditions for this container.'),
'#attributes' => [
'class' => [
'google-tag',
],
],
'#attached' => [
'library' => [
'google_tag/drupal.settings_form',
],
],
];
$form['general'] = $this
->generalFieldset($form_state);
$form['advanced'] = $this
->advancedFieldset($form_state);
$form['path'] = $this
->pathFieldset($form_state);
$form['role'] = $this
->roleFieldset($form_state);
$form['status'] = $this
->statusFieldset($form_state);
$form += $this
->conditionsForm([], $form_state);
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => 'Save',
];
$form['actions']['delete'] = [
'#type' => 'submit',
'#value' => 'Delete',
];
return $form;
}
public function generalFieldset(FormStateInterface &$form_state) {
$container = $this->entity;
$fieldset = [
'#type' => 'details',
'#title' => $this
->t('General'),
'#group' => 'settings',
];
$fieldset['container_id'] = [
'#type' => 'textfield',
'#title' => $this
->t('Container ID'),
'#description' => $this
->t('The ID assigned by Google Tag Manager (GTM) for this website container. To get a container ID, <a href="https://tagmanager.google.com/">sign up for GTM</a> and create a container for your website.'),
'#default_value' => $container
->get('container_id'),
'#attributes' => [
'placeholder' => [
'GTM-xxxxxx',
],
],
'#size' => 12,
'#maxlength' => 15,
'#required' => TRUE,
];
$fieldset['weight'] = [
'#type' => 'weight',
'#title' => 'Weight',
'#default_value' => $container
->get('weight'),
];
return $fieldset;
}
protected function conditionsForm(array $form, FormStateInterface $form_state) {
$conditions = $this->entity
->getInsertionConditions();
$definitions = $this->conditionManager
->getFilteredDefinitions('google_tag', $form_state
->getTemporaryValue('gathered_contexts'), [
'google_tag_container' => $this->entity,
]);
ksort($definitions);
$form_state
->setTemporaryValue('filtered_conditions', array_keys($definitions));
foreach ($definitions as $condition_id => $definition) {
if ($conditions
->has($condition_id)) {
$condition = $conditions
->get($condition_id);
}
else {
$condition = $this->conditionManager
->createInstance($condition_id, []);
}
$form_state
->set([
'conditions',
$condition_id,
], $condition);
$form[$condition_id] = $this
->conditionFieldset($condition, $form_state);
}
return $form;
}
public function conditionFieldset(ConditionInterface $condition, FormStateInterface $form_state) {
$fieldset = [
'#type' => 'details',
'#title' => $condition
->getPluginDefinition()['label'],
'#group' => 'conditions',
'#tree' => TRUE,
] + $condition
->buildConfigurationForm([], $form_state);
return $fieldset;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$this
->validateFormValues($form, $form_state);
parent::validateForm($form, $form_state);
$this
->validateConditionsForm($form, $form_state);
}
protected function validateConditionsForm(array $form, FormStateInterface $form_state) {
$condition_ids = $form_state
->getTemporaryValue('filtered_conditions');
foreach ($condition_ids as $condition_id) {
$condition = $form_state
->get([
'conditions',
$condition_id,
]);
$condition
->validateConfigurationForm($form[$condition_id], SubformState::createForSubform($form[$condition_id], $form, $form_state));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this
->submitConditionsForm($form, $form_state);
}
protected function submitConditionsForm(array $form, FormStateInterface $form_state) {
$condition_ids = $form_state
->getTemporaryValue('filtered_conditions');
foreach ($condition_ids as $condition_id) {
$values = $form_state
->getValue($condition_id);
$condition = $form_state
->get([
'conditions',
$condition_id,
]);
$condition
->submitConfigurationForm($form[$condition_id], SubformState::createForSubform($form[$condition_id], $form, $form_state));
$configuration = $condition
->getConfiguration();
$this->entity
->setInsertionCondition($condition_id, $configuration);
}
}
public function save(array $form, FormStateInterface $form_state) {
parent::save($form, $form_state);
global $_google_tag_display_message;
$_google_tag_display_message = TRUE;
$manager = \Drupal::service('google_tag.container_manager');
$manager
->createAssets($this->entity);
$form_state
->setRedirect('entity.google_tag_container.collection');
}
public function containerExists($value, array $element, FormStateInterface $form_state) {
$container = $form_state
->getFormObject()
->getEntity();
return (bool) $this->entityTypeManager
->getStorage($container
->getEntityTypeId())
->getQuery()
->condition($container
->getEntityType()
->getKey('id'), $value)
->execute();
}
}