Skip to content

Commit

Permalink
Disable the CSV export action on group and event pages if all the soc…
Browse files Browse the repository at this point in the history
…ial_user_export plugins are disabled
  • Loading branch information
nkoporec committed Nov 29, 2024
1 parent 34b0620 commit baff571
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\social_event_managers\Plugin\views\field;

use Drupal\Core\Action\ActionManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
Expand Down Expand Up @@ -41,6 +42,13 @@ class SocialEventManagersViewsBulkOperationsBulkForm extends ViewsBulkOperations
*/
protected $pluginActionManager;

/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* Constructs a new SocialEventManagersViewsBulkOperationsBulkForm object.
*
Expand All @@ -66,6 +74,8 @@ class SocialEventManagersViewsBulkOperationsBulkForm extends ViewsBulkOperations
* The entity type manager.
* @param \Drupal\Core\Action\ActionManager $pluginActionManager
* The action manager.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
*/
public function __construct(
array $configuration,
Expand All @@ -78,12 +88,14 @@ public function __construct(
AccountInterface $currentUser,
RequestStack $requestStack,
EntityTypeManagerInterface $entity_type_manager,
ActionManager $pluginActionManager
ActionManager $pluginActionManager,
ConfigFactoryInterface $config_factory
) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $viewData, $actionManager, $actionProcessor, $tempStoreFactory, $currentUser, $requestStack);

$this->entityTypeManager = $entity_type_manager;
$this->pluginActionManager = $pluginActionManager;
$this->configFactory = $config_factory;
}

/**
Expand All @@ -101,7 +113,8 @@ public static function create(ContainerInterface $container, array $configuratio
$container->get('current_user'),
$container->get('request_stack'),
$container->get('entity_type.manager'),
$container->get('plugin.manager.action')
$container->get('plugin.manager.action'),
$container->get('config.factory')
);
}

Expand All @@ -116,6 +129,24 @@ public function getBulkOptions(): array {
}

foreach ($this->options['selected_actions'] as $key => $selected_action_data) {
$export_actions = [
"social_event_enrolments_export_enrollments_action",
"social_event_an_enroll_enrolments_export_action",
];
// Check if we have enabled the export.
if (in_array($selected_action_data['action_id'], $export_actions)) {
$social_user_settings = $this->configFactory->get('social_user_export.settings');
$social_user_settings_plugins = array_filter($social_user_settings->get('plugins'));

if (!$this->currentUser()->hasPermission('administer social_user_export') && empty($social_user_settings_plugins)) {
unset($this->options['selected_actions'][$key]);
unset($bulk_options[$key]);
unset($this->bulkOptions[$key]);
break;
}

}

$definition = $this->actions[$selected_action_data['action_id']];
if (!empty($selected_action_data['preconfiguration']['label_override'])) {
$real_label = $selected_action_data['preconfiguration']['label_override'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

namespace Drupal\social_group\Plugin\views\field;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Url;
use Drupal\group\Entity\GroupInterface;
use Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm;
use Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionManager;
use Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionProcessorInterface;
use Drupal\views_bulk_operations\Service\ViewsBulkOperationsViewDataInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Defines the Groups Views Bulk Operations field plugin.
Expand All @@ -17,6 +25,72 @@
*/
class SocialGroupViewsBulkOperationsBulkForm extends ViewsBulkOperationsBulkForm {

/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* Constructs a new SocialGroupViewsBulkOperationsBulkForm object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\views_bulk_operations\Service\ViewsBulkOperationsViewDataInterface $viewData
* The VBO View Data provider service.
* @param \Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionManager $actionManager
* Extended action manager object.
* @param \Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionProcessorInterface $actionProcessor
* Views Bulk Operations action processor.
* @param \Drupal\Core\TempStore\PrivateTempStoreFactory $tempStoreFactory
* User private temporary storage factory.
* @param \Drupal\Core\Session\AccountInterface $currentUser
* The current user object.
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* The request stack.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
ViewsBulkOperationsViewDataInterface $viewData,
ViewsBulkOperationsActionManager $actionManager,
ViewsBulkOperationsActionProcessorInterface $actionProcessor,
PrivateTempStoreFactory $tempStoreFactory,
AccountInterface $currentUser,
RequestStack $requestStack,
ConfigFactoryInterface $config_factory
) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $viewData, $actionManager, $actionProcessor, $tempStoreFactory, $currentUser, $requestStack);

$this->configFactory = $config_factory;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('views_bulk_operations.data'),
$container->get('plugin.manager.views_bulk_operations_action'),
$container->get('views_bulk_operations.processor'),
$container->get('tempstore.private'),
$container->get('current_user'),
$container->get('request_stack'),
$container->get('config.factory')
);
}

/**
* {@inheritdoc}
*/
Expand All @@ -39,6 +113,20 @@ public function getBulkOptions(): array {
switch ($selected_action_data['action_id']) {
case 'social_group_members_export_member_action':
case 'social_group_delete_group_content_action':
// Check if we have enabled the export.
if ($selected_action_data['action_id'] == "social_group_members_export_member_action") {
$social_user_settings = $this->configFactory->get('social_user_export.settings');
$social_user_settings_plugins = array_filter($social_user_settings->get('plugins'));

if (!$this->currentUser()->hasPermission('administer social_user_export') && empty($social_user_settings_plugins)) {
unset($this->options['selected_actions'][$key]);
unset($bulk_options[$key]);
unset($this->bulkOptions[$key]);
break;
}

}

$bulk_options[$key] = $this->t('<b>@action</b> selected members', [
'@action' => $real_label,
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ function social_user_export_install(): void {
function social_user_export_update_last_removed() : int {
return 11002;
}

/**
* Assign a new administer social_user_export permission to SM.
*/
function social_user_export_update_11003(): void {
user_role_grant_permissions('sitemanager', ['administer social_user_export']);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
administer social_user_export:
title: 'Administer user export'
description: 'Allows users to export users even if disabled via config.'
restrict access: true

0 comments on commit baff571

Please sign in to comment.