/** * {@inheritdoc} * * Builds a row for the following table: * Transitions, for example: * 18 => array( * 20 => array( * 'author' => 1, * 1 => 0, * 2 => 1, * ) * ) * means the transition from state 18 to state 20 can be executed by * the content author or a user in role 2. The $transitions array should * contain ALL transitions for the workflow. */ public function buildRow(EntityInterface $entity) { $row = array(); $workflow = $this->workflow; if ($workflow) { // Each $entity is a from-state. /* @var $entity \Drupal\workflow\Entity\WorkflowState */ $from_state = $entity; $from_sid = $from_state->id(); $states = $workflow->getStates($all = 'CREATION'); if ($states) { // Only get the roles with proper permission + Author role. $type_id = $workflow->id(); $roles = workflow_get_user_role_names("create {$type_id} workflow_transition"); // Prepare default value for 'stay_on_this_state'. $allow_all_roles = []; // array_combine (array_keys($roles) , array_keys($roles)); foreach ($states as $state) { $row['to'] = ['#type' => 'value', '#markup' => t('@label', array('@label' => $from_state->label()))]; foreach ($states as $to_state) { // Don't allow transition TO (creation). if ($to_state->isCreationState()) { continue; } // Only allow transitions from $from_state. if ($state->id() != $from_state->id()) { continue; } $to_sid = $to_state->id(); $stay_on_this_state = $to_sid == $from_sid; // Load existing config_transitions. Create if not found. $config_transitions = $workflow->getTransitionsByStateId($from_sid, $to_sid); if (!($config_transition = reset($config_transitions))) { $config_transition = $workflow->createTransition($from_sid, $to_sid); } $row[$to_sid]['workflow_config_transition'] = ['#type' => 'value', '#value' => $config_transition]; $row[$to_sid]['roles'] = ['#type' => $stay_on_this_state ? 'checkboxes' : '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; }
/** * 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); }