ReplicateUISettingsForm.php in Replicate UI 8
File
src/Form/ReplicateUISettingsForm.php
View source
<?php
namespace Drupal\replicate_ui\Form;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteBuilderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ReplicateUISettingsForm extends ConfigFormBase {
protected $entityTypeManager;
protected $routerBuilder;
public function __construct(EntityTypeManagerInterface $entityTypeManager, ConfigFactoryInterface $config_factory, RouteBuilderInterface $router_builder) {
parent::__construct($config_factory);
$this->entityTypeManager = $entityTypeManager;
$this->routerBuilder = $router_builder;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('config.factory'), $container
->get('router.builder'));
}
protected function getEditableConfigNames() {
return [
'replicate_ui.settings',
];
}
public function getFormId() {
return 'replicate_ui__settings';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$content_entity_types = array_filter($this->entityTypeManager
->getDefinitions(), function (EntityTypeInterface $entity_type) {
return $entity_type instanceof ContentEntityTypeInterface;
});
$form['entity_types'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Replicate entity types'),
'#description' => $this
->t('Enable replicate for the following entity types'),
'#options' => array_map(function (EntityTypeInterface $entity_type) {
return $entity_type
->getLabel();
}, $content_entity_types),
'#default_value' => $this
->config('replicate_ui.settings')
->get('entity_types'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this
->config('replicate_ui.settings')
->set('entity_types', array_values(array_filter($form_state
->getValue('entity_types'))))
->save();
$this->routerBuilder
->setRebuildNeeded();
Cache::invalidateTags([
'entity_types',
'views_data',
]);
}
}