WorkflowConfigTransitionRoleForm.php in Workflow 8
File
src/Form/WorkflowConfigTransitionRoleForm.php
View source
<?php
namespace Drupal\workflow\Form;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
class WorkflowConfigTransitionRoleForm extends WorkflowConfigTransitionFormBase {
protected $entitiesKey = 'workflow_state';
protected $type = 'permission';
public function buildHeader() {
$header = [];
$workflow = $this->workflow;
$states = $workflow
->getStates($all = 'CREATION');
if ($states) {
$header['label_new'] = $this
->t('From \\ To');
foreach ($states as $state) {
if (!$state
->isCreationState()) {
$header[$state
->id()] = $this
->t('@label', [
'@label' => $state
->label(),
]);
}
}
}
return $header;
}
public function buildRow(EntityInterface $entity) {
$row = [];
$workflow = $this->workflow;
if ($workflow) {
$from_state = $entity;
$from_sid = $from_state
->id();
$states = $workflow
->getStates($all = 'CREATION');
if ($states) {
$type_id = $workflow
->id();
$roles = workflow_get_user_role_names("create {$type_id} workflow_transition");
$allow_all_roles = [];
foreach ($states as $state) {
$row['to'] = [
'#type' => 'value',
'#markup' => $this
->t('@label', [
'@label' => $from_state
->label(),
]),
];
foreach ($states as $to_state) {
if ($to_state
->isCreationState()) {
continue;
}
if ($state
->id() != $from_state
->id()) {
continue;
}
$to_sid = $to_state
->id();
$config_transitions = $workflow
->getTransitionsByStateId($from_sid, $to_sid);
if (!($config_transition = reset($config_transitions))) {
$config_transition = $workflow
->createTransition($from_sid, $to_sid);
}
$stay_on_this_state = !$config_transition
->hasStateChange();
$row[$to_sid]['workflow_config_transition'] = [
'#type' => 'value',
'#value' => $config_transition,
];
$row[$to_sid]['roles'] = [
'#type' => 'checkboxes',
'#options' => $stay_on_this_state ? [] : $roles,
'#disabled' => $stay_on_this_state,
'#default_value' => $stay_on_this_state ? $allow_all_roles : $config_transition->roles,
];
}
}
}
}
return $row;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
if (count($form_state
->getValue($this->entitiesKey)) < 2) {
$form_state
->setErrorByName('id', $this
->t('Please create at least one other state.'));
return;
}
$creation_state_id = $this->workflow
->getCreationState()
->id();
$author_has_permission = FALSE;
foreach ($form_state
->getValue($this->entitiesKey) as $from_sid => $to_data) {
foreach ($to_data as $to_sid => $transition_data) {
if (empty($transition_data['roles'][WORKFLOW_ROLE_AUTHOR_RID])) {
continue;
}
if ($from_sid == $creation_state_id && $from_sid != $to_sid) {
$author_has_permission = TRUE;
break;
}
}
}
if (!$author_has_permission) {
$creation_state = $this->workflow
->getCreationState();
$form_state
->setErrorByName('id', $this
->t('Please give the author permission to go from %creation to at least one state!', [
'%creation' => $creation_state
->label(),
]));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state
->getValue($this->entitiesKey) as $from_sid => $to_data) {
foreach ($to_data as $transition_data) {
if (isset($transition_data['workflow_config_transition'])) {
$config_transition = $transition_data['workflow_config_transition'];
$config_transition->roles = $transition_data['roles'];
$config_transition
->save();
}
else {
}
}
}
$this
->messenger()
->addStatus($this
->t('The workflow was updated.'));
}
}