From a2945110304c581c951909dedfe2dc5eed6ffcdb Mon Sep 17 00:00:00 2001 From: Liam Morland <lkmorlan@uwaterloo.ca> Date: Tue, 1 Jun 2021 13:10:55 -0400 Subject: [PATCH] ISTWCMS-4635: Create UWDropbutton with access check --- src/Plugin/views/field/UWDropbutton.php | 42 +++++++++++++++++++++++++ uw_cfg_common.module | 8 +++++ 2 files changed, 50 insertions(+) create mode 100644 src/Plugin/views/field/UWDropbutton.php diff --git a/src/Plugin/views/field/UWDropbutton.php b/src/Plugin/views/field/UWDropbutton.php new file mode 100644 index 00000000..c9bf0aac --- /dev/null +++ b/src/Plugin/views/field/UWDropbutton.php @@ -0,0 +1,42 @@ +<?php + +namespace Drupal\uw_cfg_common\Plugin\views\field; + +use Drupal\views\Plugin\views\field\Dropbutton; +use Drupal\views\ResultRow; + +/** + * Provides a handler that renders links as dropbutton with access check. + * + * Identical to parent class except that render() removes links for which the + * user has no access. + * + * @ingroup views_field_handlers + * + * @ViewsField("uwdropbutton") + */ +class UWDropbutton extends Dropbutton { + + /** + * {@inheritdoc} + */ + public function render(ResultRow $values) { + $links = []; + foreach ($this->getLinks() as $link) { + if ($link['url']->access()) { + $links[] = $link; + } + } + + if (!empty($links)) { + return [ + '#type' => 'dropbutton', + '#links' => $links, + ]; + } + else { + return ''; + } + } + +} diff --git a/uw_cfg_common.module b/uw_cfg_common.module index e2484bf2..b44da4c4 100644 --- a/uw_cfg_common.module +++ b/uw_cfg_common.module @@ -581,3 +581,11 @@ function uw_cfg_common_block_content_create_access(AccountInterface $account, ar // No opinion. return AccessResult::neutral(); } + +/** + * Implements hook_views_plugins_field_alter(). + */ +function uw_cfg_common_views_plugins_field_alter(array &$plugins): void { + // Replace Drupal\views\Plugin\views\field\Dropbutton with UW version. + $plugins['dropbutton']['class'] = 'Drupal\uw_cfg_common\Plugin\views\field\UWDropbutton'; +} -- GitLab