class WorkflowConfigTransitionRoleForm in Workflow 8
Defines a class to build a listing of Workflow Config Transitions entities.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait- class \Drupal\workflow\Form\WorkflowConfigTransitionFormBase- class \Drupal\workflow\Form\WorkflowConfigTransitionRoleForm
 
 
- class \Drupal\workflow\Form\WorkflowConfigTransitionFormBase
 
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of WorkflowConfigTransitionRoleForm
See also
\Drupal\workflow\Entity\WorkflowConfigTransition
1 string reference to 'WorkflowConfigTransitionRoleForm'
File
- src/Form/ WorkflowConfigTransitionRoleForm.php, line 13 
Namespace
Drupal\workflow\FormView source
class WorkflowConfigTransitionRoleForm extends WorkflowConfigTransitionFormBase {
  /**
   * {@inheritdoc}
   */
  protected $entitiesKey = 'workflow_state';
  /**
   * {@inheritdoc}
   */
  protected $type = 'permission';
  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header = [];
    $workflow = $this->workflow;
    $states = $workflow
      ->getStates($all = 'CREATION');
    if ($states) {
      $header['label_new'] = $this
        ->t('From \\ To');
      /** @var \Drupal\workflow\Entity\WorkflowState $state */
      foreach ($states as $state) {
        // Don't allow transition TO (creation).
        if (!$state
          ->isCreationState()) {
          $header[$state
            ->id()] = $this
            ->t('@label', [
            '@label' => $state
              ->label(),
          ]);
        }
      }
    }
    return $header;
  }
  /**
   * {@inheritdoc}
   *
   * Builds a row for the following table:
   *   Transitions, for example:
   *     18 => [
   *       20 => [
   *         '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 = [];
    $workflow = $this->workflow;
    if ($workflow) {
      // Each $entity is a from-state.
      /** @var \Drupal\workflow\Entity\WorkflowState $entity */
      $from_state = $entity;
      $from_sid = $from_state
        ->id();
      /** @var \Drupal\workflow\Entity\WorkflowState[] $states */
      $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'.
        // array_combine(array_keys($roles), array_keys($roles));
        $allow_all_roles = [];
        /** @var \Drupal\workflow\Entity\WorkflowState $state */
        foreach ($states as $state) {
          $row['to'] = [
            '#type' => 'value',
            '#markup' => $this
              ->t('@label', [
              '@label' => $from_state
                ->label(),
            ]),
          ];
          /** @var \Drupal\workflow\Entity\WorkflowState $to_state */
          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();
            // 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);
            }
            $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;
  }
  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
    // 'Creation' state may not be the only state.
    if (count($form_state
      ->getValue($this->entitiesKey)) < 2) {
      $form_state
        ->setErrorByName('id', $this
        ->t('Please create at least one other state.'));
      return;
    }
    // Make sure 'author' is checked for (creation) -> [something].
    $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(),
      ]));
    }
  }
  /**
   * {@inheritdoc}
   */
  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) {
        /** @var \Drupal\workflow\Entity\WorkflowConfigTransition $config_transition */
        if (isset($transition_data['workflow_config_transition'])) {
          $config_transition = $transition_data['workflow_config_transition'];
          $config_transition->roles = $transition_data['roles'];
          $config_transition
            ->save();
        }
        else {
          // Should not be possible.
        }
      }
    }
    $this
      ->messenger()
      ->addStatus($this
      ->t('The workflow was updated.'));
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| ConfigFormBaseTrait:: | protected | function | Retrieves a configuration object. | |
| DependencySerializationTrait:: | protected | property | An array of entity type IDs keyed by the property name of their storages. | |
| DependencySerializationTrait:: | protected | property | An array of service IDs keyed by property name used for serialization. | |
| DependencySerializationTrait:: | public | function | 1 | |
| DependencySerializationTrait:: | public | function | 2 | |
| FormBase:: | protected | property | The config factory. | 1 | 
| FormBase:: | protected | property | The request stack. | 1 | 
| FormBase:: | protected | property | The route match. | |
| FormBase:: | protected | function | Gets the config factory for this form. | 1 | 
| FormBase:: | private | function | Returns the service container. | |
| FormBase:: | protected | function | Gets the current user. | |
| FormBase:: | protected | function | Gets the request object. | |
| FormBase:: | protected | function | Gets the route match. | |
| FormBase:: | protected | function | Gets the logger for a specific channel. | |
| FormBase:: | protected | function | Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: | |
| FormBase:: | public | function | Resets the configuration factory. | |
| FormBase:: | public | function | Sets the config factory for this form. | |
| FormBase:: | public | function | Sets the request stack object to use. | |
| LinkGeneratorTrait:: | protected | property | The link generator. | 1 | 
| LinkGeneratorTrait:: | protected | function | Returns the link generator. | |
| LinkGeneratorTrait:: | protected | function | Renders a link to a route given a route name and its parameters. | |
| LinkGeneratorTrait:: | public | function | Sets the link generator service. | |
| LoggerChannelTrait:: | protected | property | The logger channel factory service. | |
| LoggerChannelTrait:: | protected | function | Gets the logger for a specific channel. | |
| LoggerChannelTrait:: | public | function | Injects the logger channel factory. | |
| MessengerTrait:: | protected | property | The messenger. | 29 | 
| MessengerTrait:: | public | function | Gets the messenger. | 29 | 
| MessengerTrait:: | public | function | Sets the messenger. | |
| RedirectDestinationTrait:: | protected | property | The redirect destination service. | 1 | 
| RedirectDestinationTrait:: | protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
| RedirectDestinationTrait:: | protected | function | Returns the redirect destination service. | |
| RedirectDestinationTrait:: | public | function | Sets the redirect destination service. | |
| StringTranslationTrait:: | protected | property | The string translation service. | 1 | 
| StringTranslationTrait:: | protected | function | Formats a string containing a count of items. | |
| StringTranslationTrait:: | protected | function | Returns the number of plurals supported by a given language. | |
| StringTranslationTrait:: | protected | function | Gets the string translation service. | |
| StringTranslationTrait:: | public | function | Sets the string translation service to use. | 2 | 
| StringTranslationTrait:: | protected | function | Translates a string to the current language or to a given language. | |
| UrlGeneratorTrait:: | protected | property | The url generator. | |
| UrlGeneratorTrait:: | protected | function | Returns the URL generator service. | |
| UrlGeneratorTrait:: | public | function | Sets the URL generator service. | |
| UrlGeneratorTrait:: | protected | function | Generates a URL or path for a specific route based on the given parameters. | |
| WorkflowConfigTransitionFormBase:: | protected | property | The entities being listed. | |
| WorkflowConfigTransitionFormBase:: | protected | property | The messenger / logger service. | |
| WorkflowConfigTransitionFormBase:: | protected | property | The workflow object. | |
| WorkflowConfigTransitionFormBase:: | public | function | Form constructor. Overrides ConfigFormBase:: | |
| WorkflowConfigTransitionFormBase:: | public static | function | Instantiates a new instance of this class. Overrides ConfigFormBase:: | |
| WorkflowConfigTransitionFormBase:: | protected | function | Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: | 1 | 
| WorkflowConfigTransitionFormBase:: | public | function | Returns a unique string identifying the form. Overrides FormInterface:: | 1 | 
| WorkflowConfigTransitionFormBase:: | public | function | Create an $entity for every ConfigTransition. | |
| WorkflowConfigTransitionFormBase:: | public | function | Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase:: | |
| WorkflowConfigTransitionRoleForm:: | protected | property | The key to use for the form element containing the entities. Overrides WorkflowConfigTransitionFormBase:: | |
| WorkflowConfigTransitionRoleForm:: | protected | property | The WorkflowConfigTransition form type. Overrides WorkflowConfigTransitionFormBase:: | |
| WorkflowConfigTransitionRoleForm:: | public | function | ||
| WorkflowConfigTransitionRoleForm:: | public | function | Builds a row for the following table: Transitions, for example: 18 => [ 20 => [ '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… | |
| WorkflowConfigTransitionRoleForm:: | public | function | Form submission handler. Overrides ConfigFormBase:: | |
| WorkflowConfigTransitionRoleForm:: | public | function | Form validation handler. Overrides FormBase:: | 
