diff --git a/config/schema/workbench_access.views.schema.yml b/config/schema/workbench_access.views.schema.yml index 106028a104b8af6e356e45519b74f2969589189e..c826ecac530ffe51b1f5c8353ca21dff2b78a36c 100644 --- a/config/schema/workbench_access.views.schema.yml +++ b/config/schema/workbench_access.views.schema.yml @@ -26,6 +26,9 @@ views.field.workbench_access_user_section: make_link: type: integer label: 'Make link' + display_assigned: + type: integer + label: 'Display assigned sections' views.field.workbench_access_section_id: type: views_field label: 'Workbench Section Id' diff --git a/src/Plugin/views/field/UserSection.php b/src/Plugin/views/field/UserSection.php index 5aec8f0959b44e33f94b72dff67c442b9a2871e7..2dad03e3eab346748cc710b50c370319b98a2f7f 100644 --- a/src/Plugin/views/field/UserSection.php +++ b/src/Plugin/views/field/UserSection.php @@ -10,6 +10,7 @@ use Drupal\workbench_access\UserSectionStorageInterface; use Drupal\workbench_access\WorkbenchAccessManager; use Drupal\workbench_access\WorkbenchAccessManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Form\FormStateInterface; /** * Field handler to present the section assigned to the user. @@ -52,6 +53,31 @@ class UserSection extends Section implements MultiItemsFieldHandlerInterface { ->setUserSectionStorage($container->get('workbench_access.user_section_storage')); } + /** + * {@inheritdoc} + */ + public function buildOptionsForm(&$form, FormStateInterface $form_state) { + parent::buildOptionsForm($form, $form_state); + $form['display_assigned'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Display user\'s assigned sections'), + '#default_value' => $this->options['display_assigned'], + ]; + return $form; + } + + /** + * {@inheritdoc} + */ + protected function defineOptions() { + $options = parent::defineOptions(); + $options['display_assigned'] = [ + 'default' => FALSE, + ]; + + return $options; + } + /** * Sets manager. * @@ -98,12 +124,19 @@ class UserSection extends Section implements MultiItemsFieldHandlerInterface { $this->items = []; $user = $this->getEntity($values); $all = $this->scheme->getAccessScheme()->getTree(); - if ($this->manager->userInAll($this->scheme, $user)) { - $sections = $this->manager->getAllSections($this->scheme, TRUE); + + if ($this->options['display_assigned']) { + $sections = $this->userSectionStorage->getUserSections($this->scheme, $user); } else { - $sections = $this->userSectionStorage->getUserSections($this->scheme, $user); + if ($this->manager->userInAll($this->scheme, $user)) { + $sections = $this->manager->getAllSections($this->scheme, TRUE); + } + else { + $sections = $this->userSectionStorage->getUserSections($this->scheme, $user); + } } + foreach ($sections as $id) { foreach ($all as $root => $data) { if (isset($data[$id])) { diff --git a/tests/modules/workbench_access_test/config/install/views.view.user_sections.yml b/tests/modules/workbench_access_test/config/install/views.view.user_sections.yml index 3def2d4200d38c79e5f9e4601d6955de4032b6c7..29f30a67a8d47068ea65e07dc5bc5d60019866fd 100644 --- a/tests/modules/workbench_access_test/config/install/views.view.user_sections.yml +++ b/tests/modules/workbench_access_test/config/install/views.view.user_sections.yml @@ -154,6 +154,7 @@ display: separator: ', ' entity_type: user plugin_id: workbench_access_user_section + display_assigned: 0 filters: status: value: '1' @@ -412,6 +413,120 @@ display: operator: AND groups: 1: AND + page_3: + id: page_3 + display_title: 'Page 3' + display_plugin: page + position: 1 + display_options: + title: 'User assigned sections' + fields: + name: + id: name + table: users_field_data + field: name + relationship: none + group_type: group + admin_label: '' + entity_type: user + entity_field: name + plugin_id: field + label: '' + exclude: false + alter: + alter_text: false + make_link: false + absolute: false + word_boundary: false + ellipsis: false + strip_tags: false + trim: false + html: false + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: true + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: true + empty: '' + hide_empty: false + empty_zero: false + hide_alter_empty: true + click_sort_column: value + type: user_name + settings: { } + group_column: value + group_columns: { } + group_rows: true + delta_limit: 0 + delta_offset: 0 + delta_reversed: false + delta_first_last: false + multi_type: separator + separator: ', ' + field_api_classes: false + workbench_access_section__editorial_section: + id: workbench_access_section__editorial_section + table: users + field: workbench_access_section__editorial_section + relationship: none + group_type: group + admin_label: '' + entity_type: user + plugin_id: workbench_access_user_section + label: '' + exclude: false + alter: + alter_text: false + text: '' + make_link: false + path: '' + absolute: false + external: false + replace_spaces: false + path_case: none + trim_whitespace: false + alt: '' + rel: '' + link_class: '' + prefix: '' + suffix: '' + target: '' + nl2br: false + max_length: 0 + word_boundary: true + ellipsis: true + more_link: false + more_link_text: '' + more_link_path: '' + strip_tags: false + trim: false + preserve_tags: '' + html: false + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: false + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: true + empty: '' + hide_empty: false + empty_zero: false + hide_alter_empty: true + separator: ', ' + type: separator + make_link: 0 + display_assigned: 1 + defaults: + title: false + fields: false + display_description: '' + display_extenders: { } + path: admin/people/sections-assigned cache_metadata: max-age: -1 contexts: diff --git a/tests/src/Functional/ViewsFieldTest.php b/tests/src/Functional/ViewsFieldTest.php index c4fbc1c0a7e08e4abcb15a40ac89987a1d5f2196..0b7da6aa6315634fe65079430de9ce406c5e492c 100644 --- a/tests/src/Functional/ViewsFieldTest.php +++ b/tests/src/Functional/ViewsFieldTest.php @@ -50,6 +50,13 @@ class ViewsFieldTest extends BrowserTestBase { */ protected $user2; + /** + * Test admin user. + * + * @var \Drupal\user\UserInterface + */ + protected $user3; + /** * User section storage. * @@ -131,6 +138,11 @@ class ViewsFieldTest extends BrowserTestBase { $this->user2->save(); $values = [reset($this->terms)->id()]; $this->userStorage->addUser($this->scheme, $this->user2, $values); + + $adminPermissions = ['bypass workbench access'] + $permissions; + $this->user3 = $this->createUser($adminPermissions); + $values = [reset($this->terms)->id()]; + $this->userStorage->addUser($this->scheme, $this->user3, $values); } /** @@ -167,6 +179,11 @@ class ViewsFieldTest extends BrowserTestBase { $row = $assert->elementExists('css', '.views-row:contains("' . $this->user2->label() . '")'); $assert->elementExists('css', '.views-row:contains("Some section")', $row); + // User 3 has the 'bypass workbench access' permission, so its row should + // show the vocabulary name. + $row = $assert->elementExists('css', '.views-row:contains("' . $this->user3->label() . '")'); + $assert->elementExists('css', '.views-row:contains("Editorial section")', $row); + // Now filter. $this->drupalGet('admin/people/sections', [ 'query' => [ @@ -190,6 +207,27 @@ class ViewsFieldTest extends BrowserTestBase { $assert->pageTextContains('Some section node 2'); $assert->elementNotExists('css', '.views-row:contains("Another section")'); $assert->elementNotExists('css', '.views-row:contains("More sections")'); + + // Test "Display user's assigned sections" option on the field. This should + // show the assigned section, not the vocabulary name, for users with the + // 'bypass workbench access' permission. + $this->drupalGet('admin/people/sections-assigned'); + + $row = $assert->elementExists('css', '.views-row:contains("' . $this->user->label() . '")'); + + // User 1 still shows a list of each section. + foreach ($this->terms as $section => $term) { + $assert->elementExists('css', '.views-row:contains("' . $section . '")', $row); + } + + // User 2 sill only has one section. + $row = $assert->elementExists('css', '.views-row:contains("' . $this->user2->label() . '")'); + $assert->elementExists('css', '.views-row:contains("Some section")', $row); + + // User 3, which has the 'bypass workbench access' permission, should now + // show its assigned section. + $row = $assert->elementExists('css', '.views-row:contains("' . $this->user2->label() . '")'); + $assert->elementExists('css', '.views-row:contains("Some section")', $row); } }