SettingsForm.php in Build Hooks 8.2
File
src/Form/SettingsForm.php
View source
<?php
namespace Drupal\build_hooks\Form;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
class SettingsForm extends ConfigFormBase {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager, ConfigFactoryInterface $config_factory) {
parent::__construct($config_factory);
$this->entityTypeManager = $entityTypeManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('config.factory'));
}
protected function getEditableConfigNames() {
return [
'build_hooks.settings',
];
}
public function getFormId() {
return 'build_hooks_settings_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('build_hooks.settings');
$form['logged_entity_types'] = [
'#type' => 'checkboxes',
'#options' => $this
->getContentEntityTypes(),
'#default_value' => $config
->get('logging.entity_types'),
'#title' => $this
->t('Loggable entities'),
'#description' => $this
->t('What entities should the system consider when logging "changes" for an environment?'),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$config = $this
->config('build_hooks.settings');
$config
->set('logging.entity_types', array_values(array_filter($form_state
->getValue('logged_entity_types'))));
$config
->save();
}
private function getContentEntityTypes() {
$content_entity_types = [];
$allEntityTypes = $this->entityTypeManager
->getDefinitions();
foreach ($allEntityTypes as $entity_type_id => $entity_type) {
if ($entity_type_id === 'build_hooks_deployment') {
continue;
}
if ($entity_type instanceof ContentEntityTypeInterface) {
$content_entity_types[$entity_type_id] = $entity_type
->getLabel();
}
}
return $content_entity_types;
}
}