/**
  * Implements hook_form().
  *
  * {@inheritdoc}
  *
  * Add a "three dimensional" (state, role, permission type) configuration
  * interface to the workflow edit form.
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     // Get the Workflow from the page.
     /* @var $workflow \Drupal\workflow\Entity\Workflow */
     if (!($workflow = workflow_ui_url_get_workflow())) {
         // Leave this page immediately.
         return $form;
     }
     $form = array('#tree' => TRUE);
     $form['#wid'] = $workflow->id();
     // A list of role names keyed by role ID, including the 'author' role.
     // Only get the roles with proper permission + Author role.
     $type_id = $workflow->id();
     $roles = workflow_get_user_role_names("create {$type_id} workflow_transition");
     // Add a table for every workflow state.
     foreach ($workflow->getStates($all = TRUE) as $sid => $state) {
         if ($state->isCreationState()) {
             // No need to set perms on creation.
             continue;
         }
         $view = $update = $delete = array();
         $count = 0;
         foreach (workflow_access_get_workflow_access_by_sid($sid) as $rid => $access) {
             $count++;
             $view[$rid] = $access['grant_view'] ? $rid : 0;
             $update[$rid] = $access['grant_update'] ? $rid : 0;
             $delete[$rid] = $access['grant_delete'] ? $rid : 0;
         }
         // Allow view grants by default for anonymous and authenticated users,
         // if no grants were set up earlier.
         if (!$count) {
             $view = array(AccountInterface::ANONYMOUS_ROLE => AccountInterface::ANONYMOUS_ROLE, AccountInterface::AUTHENTICATED_ROLE => AccountInterface::AUTHENTICATED_ROLE);
         }
         // @todo: better tables using a #theme function instead of direct #prefixing.
         $form[$sid] = array('#type' => 'fieldset', '#title' => $state->label(), '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE);
         $form[$sid]['view'] = array('#type' => 'checkboxes', '#options' => $roles, '#default_value' => $view, '#title' => t('Roles who can view posts in this state'), '#prefix' => '<table width="100%" style="border: 0;"><tbody style="border: 0;"><tr><td>', '#suffix' => "</td>");
         $form[$sid]['update'] = array('#type' => 'checkboxes', '#options' => $roles, '#default_value' => $update, '#title' => t('Roles who can edit posts in this state'), '#prefix' => "<td>", '#suffix' => "</td>");
         $form[$sid]['delete'] = array('#type' => 'checkboxes', '#options' => $roles, '#default_value' => $delete, '#title' => t('Roles who can delete posts in this state'), '#prefix' => "<td>", '#suffix' => "</td></tr></tbody></table>");
     }
     return parent::buildForm($form, $form_state);
 }
 /**
  * {@inheritdoc}
  */
 public function render()
 {
     // Get the Workflow from the page.
     /* @var $workflow \Drupal\workflow\Entity\Workflow */
     if (!($workflow = workflow_ui_url_get_workflow())) {
         return;
     }
     $form_type = workflow_ui_url_get_form_type();
     switch ($form_type) {
         case 'transition_roles':
             $form = new WorkflowConfigTransitionRoleForm('permission', $workflow);
             return $this->formBuilder()->getForm($form);
         case 'transition_labels':
             $form = new WorkflowConfigTransitionLabelForm('label', $workflow);
             return $this->formBuilder()->getForm($form);
         default:
             drupal_set_message(t('Improper form type provided.'), 'error');
             \Drupal::logger('workflow_ui')->notice('Improper form type provided.', []);
             return;
     }
     //    return parent::render();
 }
 /**
  * {@inheritdoc}
  *
  * Overrides DraggableListBuilder::submitForm().
  * The WorkflowState entities are always saved.
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     //  parent::submitForm($form, $form_state);
     // Get the Workflow from the page.
     /* @var $workflow \Drupal\workflow\Entity\Workflow */
     if (!($workflow = workflow_ui_url_get_workflow())) {
         return;
     }
     // The default min_weight is -10. Work with it.
     $creation_weight = -11;
     $maxweight = $minweight = -9;
     foreach ($form_state->getValue($this->entitiesKey) as $sid => $value) {
         if (isset($this->entities[$sid])) {
             /* @var $state WorkflowState */
             $state = $this->entities[$sid];
             // Is the new state name empty?
             if (empty($value['label_new'])) {
                 // No new state entered, so skip it.
                 continue;
             }
             // Does user want to deactivate the state (reassign current content)?
             if ($sid && $value['status'] == 0 && $state->isActive()) {
                 $new_sid = $value['reassign'];
                 $new_state = WorkflowState::load($new_sid);
                 $args = ['%workflow' => $workflow->label(), '%old_state' => $state->label(), '%new_state' => isset($new_state) ? $new_state->label() : ''];
                 if ($value['count'] > 0) {
                     if ($form['#last_mohican']) {
                         $new_sid = NULL;
                         // Do not reassign to new state.
                         $message = 'Removing workflow states from content in the %workflow.';
                         drupal_set_message($this->t($message, $args));
                     } else {
                         // Prepare the state delete function.
                         $message = 'Reassigning content from %old_state to %new_state.';
                         drupal_set_message($this->t($message, $args));
                     }
                 }
                 // Delete the old state without orphaning content, move them to the new state.
                 $state->deactivate($new_sid);
                 $message = $this->t('Deactivated workflow state %old_state in %workflow.', $args);
                 \Drupal::logger('workflow')->notice($message, []);
                 drupal_set_message($message);
             }
             // Set a proper weight to the new state.
             $maxweight = max($maxweight, $state->get($this->weightKey));
             // Is this a new state?
             if ($sid == 'placeholder' && empty(!$value['label_new'])) {
                 // New state, add it.
                 $state->set('id', $value['id']);
                 // Set a proper weight to the new state.
                 $state->set($this->weightKey, $maxweight + 1);
             } elseif ($value['sysid'] == WORKFLOW_CREATION_STATE) {
                 // Set a proper weight to the creation state.
                 $state->set($this->weightKey, $creation_weight);
             } else {
                 $state->set($this->weightKey, $value['weight']);
             }
             $state->set('label', $value['label_new']);
             $state->set('status', $value['status']);
             $state->save();
         }
     }
     drupal_set_message(t('The Workflow states have been updated.'));
     return;
 }