View source
<?php
namespace Drupal\message\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\message\MessagePurgePluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MessageSettingsForm extends ConfigFormBase {
protected $entityTypeManager;
protected $purgeManager;
public function getFormId() {
return 'message_system_settings';
}
protected function getEditableConfigNames() {
return [
'message.settings',
];
}
public function defaultKeys() {
return [
'delete_on_entity_delete',
];
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('plugin.manager.message.purge'));
}
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, MessagePurgePluginManager $purge_manager) {
parent::__construct($config_factory);
$this->entityTypeManager = $entity_type_manager;
$this->purgeManager = $purge_manager;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('message.settings');
$form['settings'] = [
'#type' => 'fieldset',
'#title' => t('Purge settings'),
'#tree' => TRUE,
];
$form['settings']['purge_enable'] = [
'#type' => 'checkbox',
'#title' => t('Purge messages'),
'#description' => t('Configure how messages will be deleted.'),
'#default_value' => $config
->get('purge_enable'),
];
$this->purgeManager
->purgeSettingsForm($form, $form_state, $config
->get('purge_methods'));
$form['delete_on_entity_delete'] = [
'#title' => t('Auto delete messages referencing the following entities'),
'#type' => 'select',
'#multiple' => TRUE,
'#options' => $this
->getContentEntityTypes(),
'#default_value' => $config
->get('delete_on_entity_delete'),
'#description' => t('Messages that reference entities of these types will be deleted when the referenced entity gets deleted.'),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$config = $this
->config('message.settings');
foreach ($this
->defaultKeys() as $key) {
$config
->set($key, $form_state
->getValue($key));
}
$purge_enable = $form_state
->getValue([
'settings',
'purge_enable',
]);
$config
->set('purge_enable', $purge_enable);
$config
->set('purge_methods', $purge_enable ? $this->purgeManager
->getPurgeConfiguration($form, $form_state) : []);
$config
->save();
}
protected function getContentEntityTypes() {
$options = [];
foreach ($this->entityTypeManager
->getDefinitions() as $entity_id => $entity_type) {
if ($entity_type instanceof ContentEntityTypeInterface) {
$options[$entity_type
->id()] = $entity_type
->getLabel();
}
}
return $options;
}
}