View source
<?php
namespace Drupal\fieldblock\Form;
use Drupal\Core\Block\BlockManager;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\fieldblock\Controller\FieldBlockController;
use Drupal\fieldblock\BlockEntityStorage;
class FieldBlockConfigForm extends ConfigFormBase {
protected $entityTypeManager;
protected $storage;
protected $fieldblockController;
protected $moduleHandler;
protected $blockManager;
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entityTypeManager, BlockEntityStorage $storage, ModuleHandlerInterface $moduleHandler, BlockManager $blockManager) {
parent::__construct($config_factory);
$this->entityTypeManager = $entityTypeManager;
$this->storage = $storage;
$this->fieldblockController = new FieldBlockController();
$this->moduleHandler = $moduleHandler;
$this->blockManager = $blockManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('fieldblock.block_storage'), $container
->get('module_handler'), $container
->get('plugin.manager.block'));
}
protected function getEditableConfigNames() {
return [
'fieldblock.settings',
];
}
public function getFormId() {
return 'field_block_config_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$enabled = $this->fieldblockController
->getEnabledEntityTypes();
$form['enabled_entity_types'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Enable Entity Types'),
'#options' => $this
->getEntityTypeLabels(),
'#description' => $this
->t('Select the Entity Types to expose as field blocks.'),
'#default_value' => $enabled,
];
$orphaned_types = $this
->getOrphanedEntityTypes($enabled);
$cleanup_options = [];
$entity_type_definitions = $this->entityTypeManager
->getDefinitions();
foreach ($orphaned_types as $entity_type) {
if (isset($entity_type_definitions[$entity_type]) && $entity_type_definitions[$entity_type] instanceof ContentEntityTypeInterface) {
$cleanup_options[$entity_type] = $entity_type_definitions[$entity_type]
->getLabel();
}
else {
$cleanup_options[$entity_type] = $this
->t('Missing entity type: @type', [
'@type' => $entity_type,
]);
}
}
if (!empty($cleanup_options)) {
$form['cleanup'] = [
'#type' => 'checkboxes',
'#required' => FALSE,
'#title' => t('Clean up remaining field blocks of removed entity types'),
'#description' => t('These entity types no longer exist, but one or more of their field blocks still do. Select the entity type(s) of which the field block(s) must be removed.'),
'#default_value' => [],
'#options' => $cleanup_options,
];
}
return parent::buildForm($form, $form_state);
}
protected function getAllEntityTypes() {
return array_keys($this->entityTypeManager
->getDefinitions());
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$clear_cache = FALSE;
$previous_entity_types = $this->fieldblockController
->getEnabledEntityTypes();
$new_entity_types = $form_state
->getValue('enabled_entity_types');
if ($previous_entity_types != $new_entity_types) {
$clear_cache = TRUE;
}
if ($cleanup = $form_state
->getValue('cleanup')) {
foreach ($cleanup as $entity_type => $value) {
if ($value !== 0) {
$this->storage
->deleteBlocksForEntityType($entity_type);
$clear_cache = TRUE;
$this
->messenger()
->addStatus($this
->t('Remaining field blocks for the %type entity have been deleted.', [
'%type' => $entity_type,
]));
}
else {
if (in_array($entity_type, $this
->getAllEntityTypes())) {
$new_entity_types[$entity_type] = $entity_type;
}
}
}
}
$this
->config('fieldblock.settings')
->set('enabled_entity_types', $new_entity_types)
->save();
if ($clear_cache) {
if ($this->moduleHandler
->moduleExists('block')) {
$this->blockManager
->clearCachedDefinitions();
}
}
}
protected function getEntityTypeLabels() {
$definitions = $this->entityTypeManager
->getDefinitions();
$labels = [];
foreach ($definitions as $definition) {
if ($this->fieldblockController
->isFieldBlockCompatible($definition)) {
$labels[$definition
->id()] = $definition
->getLabel();
}
}
return $labels;
}
protected function getOrphanedEntityTypes(array $enabled_entity_types) {
$orphaned_types = [];
$entity_types_used = $this->storage
->getEntityTypesUsed();
$all_entity_types = $this
->getAllEntityTypes();
foreach ($entity_types_used as $used_type) {
if (!in_array($used_type, $all_entity_types) || !in_array($used_type, $enabled_entity_types)) {
$orphaned_types[] = $used_type;
}
}
return $orphaned_types;
}
}