View source
<?php
namespace Drupal\recently_read\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\recently_read\Entity\RecentlyReadType;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Cache\CacheBackendInterface;
class RecentlyReadConfigForm extends ConfigFormBase {
protected $configFactory;
protected $entityTypeManager;
protected $cache;
public function __construct(ConfigFactoryInterface $configFactory, EntityTypeManagerInterface $entityTypeManager, CacheBackendInterface $cacheBackendInterface) {
parent::__construct($configFactory);
$this->entityTypeManager = $entityTypeManager;
$this->cache = $cacheBackendInterface;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('cache.default'));
}
protected function getEditableConfigNames() {
return [
'delete_config',
'delete_time',
];
}
public function getFormId() {
return 'recently_read_config';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->configFactory
->get('recently_read.configuration');
$recentlyReadEntityTypes = [];
$entityTypes = $this->entityTypeManager
->getDefinitions();
foreach ($entityTypes as $entityType) {
if ($entityType
->getGroup() === 'content') {
$recentlyReadEntityTypes[$entityType
->id()] = $entityType
->getLabel();
}
}
$enabledEntityTypes = array_keys(RecentlyReadType::loadMultiple());
$form['entity_types'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Entity types'),
'#options' => $recentlyReadEntityTypes,
'#default_value' => $enabledEntityTypes,
];
$form['delete_config'] = [
'#type' => 'radios',
'#title' => 'Records delete options',
'#options' => [
'time' => $this
->t('Time based'),
'count' => $this
->t('Count based'),
'never' => $this
->t('Never'),
],
'#default_value' => $config
->get('delete_config'),
];
$delete_time_options = [
'-1 hours' => '1 hours',
'-1 day' => '1 day',
'-1 week' => '1 Week',
'-1 month' => '1 Month',
'-1 year' => '1 Year',
];
$form['delete_time'] = [
'#type' => 'select',
'#title' => $this
->t('Delete records older then'),
'#description' => $this
->t('When cron is executed, delete records that are older then selected value.'),
'#options' => $delete_time_options,
'#default_value' => $config
->get('delete_time'),
'#states' => [
'visible' => [
':input[name="delete_config"]' => [
'value' => 'time',
],
],
],
];
$form['count'] = [
'#type' => 'number',
'#title' => $this
->t('Max records'),
'#description' => $this
->t('Allowed number of records per user or session (if user is anonymous). Older records will be removed.'),
'#default_value' => $config
->get('count'),
'#states' => [
'visible' => [
':input[name="delete_config"]' => [
'value' => 'count',
],
],
'required' => [
':input[name="delete_config"]' => [
'value' => 'count',
],
],
],
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$selectedEntityTypes = $form_state
->getValue('entity_types');
$selectedEntityTypes = array_filter($selectedEntityTypes);
$enabledEntityTypes = array_keys(RecentlyReadType::loadMultiple());
$toAdd = array_diff($selectedEntityTypes, $enabledEntityTypes);
$toRemove = array_diff($enabledEntityTypes, $selectedEntityTypes);
foreach ($toAdd as $entityType) {
RecentlyReadType::create([
'id' => $entityType,
'label' => $this->entityTypeManager
->getDefinition($entityType)
->getLabel(),
])
->save();
}
foreach ($toRemove as $entityType) {
$loadedRecentlyReadType = RecentlyReadType::load($entityType);
if ($loadedRecentlyReadType) {
$loadedRecentlyReadType
->delete();
}
}
$config = $this->configFactory
->getEditable('recently_read.configuration');
$config
->set('delete_config', $form_state
->getValue('delete_config'));
$config
->set('delete_time', $form_state
->getValue('delete_time'));
$config
->set('count', $form_state
->getValue('count'));
$config
->save();
$this->cache
->invalidateAll();
}
}