You are here

class WorkflowAccessRoleForm in Workflow 8

Provides the base form for workflow add and edit forms.

Hierarchy

Expanded class hierarchy of WorkflowAccessRoleForm

1 string reference to 'WorkflowAccessRoleForm'
workflow_access.routing.yml in modules/workflow_access/workflow_access.routing.yml
modules/workflow_access/workflow_access.routing.yml

File

modules/workflow_access/src/Form/WorkflowAccessRoleForm.php, line 14

Namespace

Drupal\workflow_access\Form
View source
class WorkflowAccessRoleForm extends WorkflowConfigTransitionFormBase {

  /**
   * {@inheritdoc}
   */
  protected $entitiesKey = 'workflow_state';

  /**
   * The WorkflowConfigTransition form type.
   *
   * @var string
   */
  protected $type = 'access';

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'workflow_access_role';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'workflow_access.role',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header = [
      'label_new' => $this
        ->t('State'),
      'view' => $this
        ->t('Roles who can view posts in this state'),
      'update' => $this
        ->t('Roles who can edit posts in this state'),
      'delete' => $this
        ->t('Roles who can delete posts in this state'),
    ];
    return $header;
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
    $row = [];
    $workflow = $this->workflow;
    if ($workflow) {

      /** @var \Drupal\workflow\Entity\WorkflowState $state */
      $state = $entity;
      $sid = $state
        ->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");
      if ($state
        ->isCreationState()) {

        // No need to set perms on creation.
        return [];
      }
      $view = $update = $delete = [];
      $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 = [
          AccountInterface::ANONYMOUS_ROLE => AccountInterface::ANONYMOUS_ROLE,
          AccountInterface::AUTHENTICATED_ROLE => AccountInterface::AUTHENTICATED_ROLE,
        ];
      }
      $row['label_new'] = [
        '#type' => 'value',
        '#markup' => $this
          ->t('@label', [
          '@label' => $state
            ->label(),
        ]),
      ];
      $row['view'] = [
        '#type' => 'checkboxes',
        '#options' => $roles,
        '#default_value' => $view,
      ];
      $row['update'] = [
        '#type' => 'checkboxes',
        '#options' => $roles,
        '#default_value' => $update,
      ];
      $row['delete'] = [
        '#type' => 'checkboxes',
        '#options' => $roles,
        '#default_value' => $delete,
      ];
    }
    return $row;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    foreach ($form_state
      ->getValue($this->entitiesKey) as $sid => $access) {

      // @todo Not waterproof; can be done smarter, using elementchildren().
      if (!WorkflowState::load($sid)) {
        continue;
      }
      foreach ($access['view'] as $rid => $checked) {
        $data[$rid] = [
          'grant_view' => !empty($access['view'][$rid]) ? (bool) $access['view'][$rid] : 0,
          'grant_update' => !empty($access['update'][$rid]) ? (bool) $access['update'][$rid] : 0,
          'grant_delete' => !empty($access['delete'][$rid]) ? (bool) $access['delete'][$rid] : 0,
        ];
      }
      workflow_access_insert_workflow_access_by_sid($sid, $data);

      // Update all nodes to reflect new settings.
      node_access_needs_rebuild(TRUE);
    }
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.
WorkflowAccessRoleForm::$entitiesKey protected property The key to use for the form element containing the entities. Overrides WorkflowConfigTransitionFormBase::$entitiesKey
WorkflowAccessRoleForm::$type protected property The WorkflowConfigTransition form type. Overrides WorkflowConfigTransitionFormBase::$type
WorkflowAccessRoleForm::buildHeader public function
WorkflowAccessRoleForm::buildRow public function
WorkflowAccessRoleForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides WorkflowConfigTransitionFormBase::getEditableConfigNames
WorkflowAccessRoleForm::getFormId public function Returns a unique string identifying the form. Overrides WorkflowConfigTransitionFormBase::getFormId
WorkflowAccessRoleForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
WorkflowConfigTransitionFormBase::$entities protected property The entities being listed.
WorkflowConfigTransitionFormBase::$logger protected property The messenger / logger service.
WorkflowConfigTransitionFormBase::$workflow protected property The workflow object.
WorkflowConfigTransitionFormBase::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
WorkflowConfigTransitionFormBase::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
WorkflowConfigTransitionFormBase::load public function Create an $entity for every ConfigTransition.
WorkflowConfigTransitionFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase::__construct