Skip to content
Snippets Groups Projects

added check for trash module, spacing updates

Open sagesolutions requested to merge issue/node_auto_expire-3471636:8.x-1.x into 8.x-1.x
2 files
+ 62
101
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -2,7 +2,10 @@
namespace Drupal\node_auto_expire\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
@@ -14,78 +17,73 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class NodeAutoExpireConfigForm extends ConfigFormBase {
/**
* State Interface.
*
* @var \Drupal\Core\Form\FormStateInterface
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
protected StateInterface $state;
/**
* Config Entity Storage.
*
* @var \Drupal\Core\Config\Entity\ConfigEntityStorage
*/
protected $nodeType;
protected ConfigEntityStorage $nodeType;
/**
* NodeAutoExpireConfigForm constructor.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
private ModuleHandlerInterface $moduleHandler;
/**
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typedConfigManager
* @param \Drupal\Core\State\StateInterface $state
* State interface injection.
* @param \Drupal\Core\Config\Entity\ConfigEntityStorage $node_type
* Config entity storage injection.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
*/
public function __construct(StateInterface $state, ConfigEntityStorage $node_type) {
public function __construct(ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typedConfigManager, StateInterface $state, ConfigEntityStorage $node_type, ModuleHandlerInterface $module_handler) {
parent::__construct($config_factory, $typedConfigManager);
$this->state = $state;
$this->nodeType = $node_type;
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
// Instantiates this form class.
return new static(
// Load the services required to construct this class.
$container->get('config.factory'),
$container->get('config.typed'),
$container->get('state'),
$container->get('entity_type.manager')->getStorage('node_type')
$container->get('entity_type.manager')->getStorage('node_type'),
$container->get('module_handler')
);
}
/**
* Implements getEditableConfigNames().
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
protected function getEditableConfigNames(): array {
return [
'node_auto_expire.settings',
];
}
/**
* Implements getFormId().
* {@inheritdoc}
*/
public function getFormId() {
public function getFormId(): string {
return 'node_auto_expire_settings';
}
/**
* Implements buildForm().
*
* @param array $form
* Comment about this variable.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Comment about this variable.
*
* @return array
* Comment about this variable.
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
public function buildForm(array $form, FormStateInterface $form_state): array {
$config = $this->config('node_auto_expire.settings');
$drupal_state = $this->state;
foreach ($this->nodeType->loadMultiple() as $type => $name) {
$code = $config->get('node_auto_expire_node_type') . $type;
$form['nodetypes'][$type] = [
@@ -107,6 +105,15 @@ class NodeAutoExpireConfigForm extends ConfigFormBase {
'#description' => $this->t('Allow to extend auto expire after each node update by the specified period.'),
];
if ($this->moduleHandler->moduleExists('trash')) {
$form['nodetypes'][$type][$code . '_purge_expired'] = [
'#type' => 'checkbox',
'#title' => $this->t('Purge expired nodes immediately'),
'#default_value' => $drupal_state->get($code . '_purge_expired', 0),
'#description' => $this->t('Purge expired nodes immediately instead of putting them into the trash.'),
];
}
$form['nodetypes'][$type][$code . '_d'] = [
'#type' => 'number',
'#title' => $this->t('Days'),
@@ -133,13 +140,11 @@ class NodeAutoExpireConfigForm extends ConfigFormBase {
'#default_value' => $drupal_state->get($code . '_p', $config->get('node_auto_expire_purge')),
'#description' => $this->t('The number of days after an item has expired when it will be purged from the database. Set to 0 (zero) for no purge.'),
];
}
$notification_types = ['warn', 'expired'];
foreach ($notification_types as $notification_type) {
$form['email'][$notification_type] = [
'#type' => 'details',
'#title' => ($notification_type == 'warn' ? $this->t('Expiration Warning') : $this->t('Expired')) . ' ' . $this->t('Notification Email'),
@@ -172,7 +177,6 @@ class NodeAutoExpireConfigForm extends ConfigFormBase {
]
),
];
}
$form['email']['cc'][$config->get('node_auto_expire_email') . 'bcc'] = [
@@ -185,31 +189,21 @@ class NodeAutoExpireConfigForm extends ConfigFormBase {
];
return parent::buildForm($form, $form_state);
}
/**
* Implement submitForm().
*
* @param array $form
* Comment about this variable.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Comment about this variable.
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state): void {
$form_settings = $form_state->getValues();
foreach ($form_settings as $name => $setting) {
if (strpos($name, 'auto_expire_') !== FALSE) {
$this->state->set($name, $setting);
}
}
parent::submitForm($form, $form_state);
}
}
Loading