ModerationSidebarSettingsForm.php in Moderation Sidebar 8
File
src/Form/ModerationSidebarSettingsForm.php
View source
<?php
namespace Drupal\moderation_sidebar\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
class ModerationSidebarSettingsForm extends ConfigFormBase {
protected $entityTypeManager;
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($config_factory);
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.manager'));
}
public function getFormId() {
return 'moderation_sidebar_settings';
}
protected function getEditableConfigNames() {
return [
'moderation_sidebar.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('moderation_sidebar.settings');
$workflows = $this->entityTypeManager
->getStorage('workflow')
->loadMultiple();
foreach ($workflows as $key => $workflow) {
$workflow_form_key = $key . '_workflow';
$form[$workflow_form_key] = [
'#type' => 'details',
'#title' => $this
->t('Disabled @workflow transitions', [
'@workflow' => $workflow
->label(),
]),
'#description' => $this
->t('Select transitions, which should be disabled in the Moderation Sidebar, when the @workflow workflow is in use.', [
'@workflow' => $workflow
->label(),
]),
'#open' => TRUE,
'#tree' => TRUE,
'#parents' => [
'workflows',
$workflow_form_key,
],
];
$transitions = $workflow
->getTypePlugin()
->getTransitions();
$transitions = array_map(function ($transition) {
return $transition
->label();
}, $transitions);
$form[$workflow_form_key]['disabled_transitions'] = [
'#type' => 'checkboxes',
'#options' => $transitions,
'#default_value' => $config
->get('workflows.' . $workflow_form_key . '.disabled_transitions') ?: [],
];
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->configFactory
->getEditable('moderation_sidebar.settings')
->set('workflows', $form_state
->getValue('workflows'))
->save();
parent::submitForm($form, $form_state);
}
}