diff --git a/src/Plugin/views/field/UWDropbutton.php b/src/Plugin/views/field/UWDropbutton.php new file mode 100644 index 0000000000000000000000000000000000000000..c9bf0aace5739e5a22f5b88cc6e6a3c44a38a233 --- /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 e2484bf213fc2e452a29d57e5e18912febca89c4..b44da4c44b71d71a3d178d430f0b27d7202fae67 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'; +}