Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa

## [Unreleased]

- [PR-224](https://github.com/OS2Forms/os2forms/pull/224)
- Add permission for accessing actions and bulk operations in webform submission list
- [PR-101](https://github.com/OS2Forms/os2forms/pull/101)
- Added support for `os2web_key` in Digital post
- Switched from saving settings in key value store to config, i.e
Expand Down
1 change: 1 addition & 0 deletions modules/os2forms_webform_list/os2forms_webform_list.module
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ function os2forms_webform_list_entity_type_alter(array &$entity_types) {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
// Define a new list builder classes.
$entity_types['webform']->setListBuilderClass('Drupal\os2forms_webform_list\CustomWebformEntityListBuilder');
$entity_types['webform_submission']->setListBuilderClass('Drupal\os2forms_webform_list\CustomWebformSubmissionListBuilder');
$entity_types['maestro_template']->setListBuilderClass('Drupal\os2forms_webform_list\CustomMaestroTemplateEntityListBuilder');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'access webform submission list bulk operations and actions':
title: 'Access webform submission list bulk operations and actions'
description: 'Access webform submission list bulk operations and actions'
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Drupal\os2forms_webform_list;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\webform\Utility\WebformDialogHelper;
use Drupal\webform\WebformSubmissionListBuilder;

/**
* Defines a class to build a listing of webform entities.
*
* @see \Drupal\webform\Entity\Webform
*/
class CustomWebformSubmissionListBuilder extends WebformSubmissionListBuilder {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't agree with the name here. The name should give a hint about a purpose of the class/file. Currently it only say that is customly made, which is obvious already.


/**
* Build the webform submission entity list.
*
* @return array
* A renderable array containing the entity list.
*/
protected function buildEntityList(): array {
$build = [];

// Filter form.
if (empty($this->account)) {
$build['filter_form'] = $this->buildFilterForm();
}

// Customize buttons.
if ($this->customize) {
$build['customize'] = $this->buildCustomizeButton();
}

// Display info.
if ($this->total) {
$build['info'] = $this->buildInfo();
}

// Table.
$build += EntityListBuilder::render();
$build['table']['#sticky'] = TRUE;
$build['table']['#attributes']['class'][] = 'webform-results-table';

// Bulk operations only visible on webform submissions pages.
$webform_submission_bulk_form = $this->configFactory->get('webform.settings')->get('settings.webform_submission_bulk_form');
if ($webform_submission_bulk_form
&& !$this->account
&& $this->webform
&& $this->webform->access('submission_update_any')
&& $this->currentUser->hasPermission('access webform submission list bulk operations and actions')) {
$build['table'] = \Drupal::formBuilder()->getForm('\Drupal\webform\Form\WebformSubmissionBulkForm', $build['table'], $this->webform->access('submission_delete_any'));
}

// Must preload libraries required by (modal) dialogs.
// Must preload libraries required by (modal) dialogs.
WebformDialogHelper::attachLibraries($build);

return $build;
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is lot of copy-paste from the parent class: WebformSubmissionListBuilder, but if my findings are correctn, then only thing added is the check for a new permission:
$this->currentUser->hasPermission('access webform submission list bulk operations and actions'

condider removing the code duplication and depend as much as possible on the contrib code - in that way there is much less support in the future, should the contrib code change.

This function can be simplified:
(please note, this is an untested code, but you'll get the idea):

protected function buildEntityList(): array {
  $build = parent::buildEntityList();

  // Bulk operations only visible on webform submissions pages.
   $webform_submission_bulk_form = $this->configFactory->get('webform.settings')->get('settings.webform_submission_bulk_form');
    if ($webform_submission_bulk_form
      && !$this->account
      && $this->webform
      && $this->webform->access('submission_update_any')
      && $this->currentUser->hasPermission('access webform submission list bulk operations and actions')) {
      $build['table'] = \Drupal::formBuilder()->getForm('\Drupal\webform\Form\WebformSubmissionBulkForm', $build['table'], $this->webform->access('submission_delete_any'));
  }
  else {
    unset($build['table']);
  }
  return $builld;
}


/**
* Add permissions check on operations.
*
* @return array
* A renderable array containing the entity list.
*/
public function getDefaultOperations(EntityInterface $entity): array {
if ($this->currentUser->hasPermission('access webform submission list bulk operations and actions')) {
return parent::getDefaultOperations($entity);
}
else {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, instead of duplicating the code i would suggest to rely as much as possible on the contibuted code.

$operations = parent::getDefaultOperations($entity);

if ($this->currentUser->hasPermission('access webform submission list bulk operations and actions')) {
  foreach ($operations as $key => $operation) {
    if ($key !== 'view' && $key !== 'log') {
      unset($operations[$key]);
    }
  }
}

return $operations;

$webform = $entity->getWebform();
$operations = [];

if ($entity->access('view')) {
$operations['view'] = [
'title' => $this->t('View'),
'weight' => 20,
'url' => $this->requestHandler->getUrl($entity, $this->sourceEntity, 'webform.user.submission'),
];
}

if ($entity->access('view_any')
&& $this->currentUser->hasPermission('access webform submission log')
&& $webform->hasSubmissionLog()
&& $this->moduleHandler->moduleExists('webform_submission_log')) {
$operations['log'] = [
'title' => $this->t('Log'),
'weight' => 100,
'url' => $this->requestHandler->getUrl($entity, $this->sourceEntity, 'webform_submission.log'),
];
}

return $operations;
}
}

}
Loading