WorkflowConfigTransitionFormBase.php in Workflow 8
File
src/Form/WorkflowConfigTransitionFormBase.php
View source
<?php
namespace Drupal\workflow\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class WorkflowConfigTransitionFormBase extends ConfigFormBase {
protected $entitiesKey = 'entities';
protected $type;
protected $entities = [];
protected $workflow;
protected $logger;
public function __construct(ConfigFactoryInterface $config_factory, LoggerInterface $logger) {
parent::__construct($config_factory);
$this->logger = $logger;
$this->workflow = workflow_url_get_workflow();
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('logger.factory')
->get('workflow'));
}
public function getFormId() {
return 'workflow_config_transition_' . $this->type . '_form';
}
protected function getEditableConfigNames() {
return [];
}
public function load() {
$entities = [];
$entity_type = $this->entitiesKey;
$workflow = $this->workflow;
$states = $workflow
->getStates($all = 'CREATION');
if ($states) {
switch ($entity_type) {
case 'workflow_state':
foreach ($states as $from_state) {
$from_sid = $from_state
->id();
$entities[$from_sid] = $from_state;
}
break;
case 'workflow_config_transition':
foreach ($states as $from_state) {
$from_sid = $from_state
->id();
foreach ($states as $to_state) {
$to_sid = $to_state
->id();
if ($to_state
->isCreationState()) {
continue;
}
if ($to_sid != $from_sid) {
}
$config_transitions = $workflow
->getTransitionsByStateId($from_sid, $to_sid);
if (!($config_transition = reset($config_transitions))) {
$config_transition = $workflow
->createTransition($from_sid, $to_sid);
}
$entities[] = $config_transition;
}
}
break;
default:
$this
->messenger()
->addError($this
->t('Improper type provided in load method.'));
$this->logger
->notice('Improper type provided in load method.', []);
}
}
return $entities;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = [];
if (!$this->workflow) {
return $form;
}
$form[$this->entitiesKey] = [
'#type' => 'table',
'#header' => $this
->buildHeader(),
'#sticky' => TRUE,
'#empty' => $this
->t('There is no @label yet.', [
'@label' => 'Transition',
]),
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'weight',
],
],
];
$this->entities = $this
->load();
$delta = 10;
if (!empty($this->weightKey)) {
$count = count($this->entities);
if ($count > 20) {
$delta = ceil($count / 2);
}
}
foreach ($this->entities as $entity) {
$row = $this
->buildRow($entity);
if (isset($row['label'])) {
$row['label'] = [
'#markup' => $row['label'],
];
}
if (isset($row['weight'])) {
$row['weight']['#delta'] = $delta;
}
$form[$this->entitiesKey][$entity
->id()] = $row;
}
$form = parent::buildForm($form, $form_state);
return $form;
}
}