View source
<?php
namespace Drupal\google_analytics_counter\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\google_analytics_counter\GoogleAnalyticsCounterCustomFieldGeneratorInterface;
use Drupal\google_analytics_counter\GoogleAnalyticsCounterAppManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
class GoogleAnalyticsCounterConfigureTypesForm extends ConfigFormBase {
protected $configFactory;
protected $messenger;
protected $appManager;
protected $customField;
public function __construct(ConfigFactoryInterface $config_factory, MessengerInterface $messenger, GoogleAnalyticsCounterAppManagerInterface $app_manager, GoogleAnalyticsCounterCustomFieldGeneratorInterface $custom_field) {
parent::__construct($config_factory);
$this->configFactory = $config_factory;
$this->messenger = $messenger;
$this->appManager = $app_manager;
$this->customField = $custom_field;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('messenger'), $container
->get('google_analytics_counter.app_manager'), $container
->get('google_analytics_counter.custom_field_generator'));
}
public function getFormId() {
return 'google_analytics_counter_configure_types_form';
}
protected function getEditableConfigNames() {
return [
'google_analytics_counter.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state, $options = NULL) {
$config = $this
->config('google_analytics_counter.settings');
$form['gac_custom_field_storage_status'] = [
'#type' => 'details',
'#title' => $this
->t('Custom field storage information'),
'#open' => TRUE,
];
$form['gac_custom_field_storage_status']['gac_type_remove_storage'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Remove the custom field'),
'#description' => $this
->t('Removes the custom Google Analytics Counter field from the system completely.'),
'#default_value' => $config
->get("general_settings.gac_type_remove_storage"),
];
$form['gac_content_types'] = [
'#type' => 'details',
'#title' => $this
->t('Content types'),
'#description' => $this
->t('Check the content types to add the custom Google Analytics Counter field to.'),
'#open' => TRUE,
];
$content_types = \Drupal::service('entity.manager')
->getStorage('node_type')
->loadMultiple();
foreach ($content_types as $machine_name => $content_type) {
$form['gac_content_types']["gac_type_{$machine_name}"] = [
'#type' => 'checkbox',
'#title' => $content_type
->label(),
'#default_value' => $config
->get("general_settings.gac_type_{$machine_name}"),
'#states' => [
'disabled' => [
':input[name="gac_type_remove_storage"]' => [
'checked' => TRUE,
],
],
],
];
}
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('google_analytics_counter.settings');
$config_factory = $this
->configFactory();
$values = $form_state
->cleanValues()
->getValues();
$config
->set('general_settings.gac_type_remove_storage', $values['gac_type_remove_storage'])
->save();
foreach ($values as $key => $value) {
if ($key == 'gac_type_remove_storage') {
continue;
}
$type = \Drupal::service('entity.manager')
->getStorage('node_type')
->load(substr($key, 9));
if ($values['gac_type_remove_storage'] == FALSE && $value == 1) {
$this->customField
->gacPreAddField($type, $key, $value);
}
else {
if ($values['gac_type_remove_storage'] == FALSE && $value == 0) {
$this->customField
->gacPreDeleteField($type, $key);
$config_factory
->getEditable('google_analytics_counter.settings')
->set("general_settings.{$key}", NULL)
->save();
}
else {
if ($values['gac_type_remove_storage'] == TRUE) {
$this->customField
->gacPreDeleteField($type, $key);
$this->customField
->gacDeleteFieldStorage();
$this->customField
->gacChangeConfigToNull();
}
}
}
}
parent::submitForm($form, $form_state);
}
}