class ExportUser in Open Social 10.0.x
Same name and namespace in other branches
- 8.9 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser
- 8 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser
- 8.2 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser
- 8.3 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser
- 8.4 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser
- 8.5 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser
- 8.6 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser
- 8.7 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser
- 8.8 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser
- 10.3.x modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser
- 10.1.x modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser
- 10.2.x modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser
Exports a user accounts to CSV.
Plugin annotation
@Action(
id = "social_user_export_user_action",
label = @Translation("Export the selected users to CSV"),
type = "user",
confirm = TRUE
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Action\ActionBase implements ActionInterface
- class \Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase implements ConfigurableInterface, ViewsBulkOperationsActionInterface uses ViewsBulkOperationsActionCompletedTrait
- class \Drupal\social_user_export\Plugin\Action\ExportUser implements ContainerFactoryPluginInterface, PluginFormInterface uses MessengerTrait
- class \Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase implements ConfigurableInterface, ViewsBulkOperationsActionInterface uses ViewsBulkOperationsActionCompletedTrait
- class \Drupal\Core\Action\ActionBase implements ActionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ExportUser
2 files declare their use of ExportUser
- ExportEnrolments.php in modules/
social_features/ social_event/ modules/ social_event_enrolments_export/ src/ Plugin/ Action/ ExportEnrolments.php - ExportMember.php in modules/
social_features/ social_group/ modules/ social_group_members_export/ src/ Plugin/ Action/ ExportMember.php
File
- modules/
social_features/ social_user_export/ src/ Plugin/ Action/ ExportUser.php, line 32
Namespace
Drupal\social_user_export\Plugin\ActionView source
class ExportUser extends ViewsBulkOperationsActionBase implements ContainerFactoryPluginInterface, PluginFormInterface {
use MessengerTrait;
/**
* The User export plugin manager.
*
* @var \Drupal\social_user_export\Plugin\UserExportPluginManager
*/
protected $userExportPlugin;
/**
* User export plugin definitions.
*
* @var array
*/
protected $pluginDefinitions;
/**
* A logger instance.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* The current user account.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* The user export plugin config object.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* Constructs a ExportUser 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\social_user_export\Plugin\UserExportPluginManager $userExportPlugin
* The user export plugin manager.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
* The current user account.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config factory for the export plugin access.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, UserExportPluginManager $userExportPlugin, LoggerInterface $logger, AccountProxyInterface $currentUser, ConfigFactoryInterface $configFactory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->userExportPlugin = $userExportPlugin;
$this->logger = $logger;
$this->currentUser = $currentUser;
$this->config = $configFactory
->get('social_user_export.settings');
// Get the definitions, check for access and and sort them by weight.
$definitions = $this->userExportPlugin
->getDefinitions();
$this->pluginDefinitions = $this
->pluginAccess($definitions);
usort($this->pluginDefinitions, [
$this,
'sortDefinitions',
]);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.user_export_plugin'), $container
->get('logger.factory')
->get('action'), $container
->get('current_user'), $container
->get('config.factory'));
}
/**
* {@inheritdoc}
*/
public function executeMultiple(array $entities) {
// Check if headers exists.
if (empty($this->context['sandbox']['results']['headers'])) {
$headers = [];
/** @var \Drupal\social_user_export\Plugin\UserExportPluginBase $instance */
foreach ($this->pluginDefinitions as $plugin) {
$instance = $this->userExportPlugin
->createInstance($plugin['id']);
$headers[] = $instance
->getHeader();
}
$this->context['sandbox']['results']['headers'] = $headers;
}
// Create the file if applicable.
if (empty($this->context['sandbox']['results']['file_path'])) {
// Store only the name relative to the output directory. On platforms such
// as Pantheon, different batch ticks can happen on different webheads.
// This can cause the file mount path to change, thus changing where on
// disk the tmp folder is actually located.
$this->context['sandbox']['results']['file_path'] = $this
->generateFilePath();
$file_path = $this
->getBaseOutputDirectory() . DIRECTORY_SEPARATOR . $this->context['sandbox']['results']['file_path'];
$csv = Writer::createFromPath($file_path, 'w');
$csv
->setDelimiter(',');
$csv
->setEnclosure('"');
$csv
->setEscape('\\');
$csv
->insertOne($this->context['sandbox']['results']['headers']);
}
else {
$file_path = $this
->getBaseOutputDirectory() . DIRECTORY_SEPARATOR . $this->context['sandbox']['results']['file_path'];
$csv = Writer::createFromPath($file_path, 'a');
}
// Add formatter.
$csv
->addFormatter([
new CsvEncoder(),
'formatRow',
]);
// Now add the entities to export.
foreach ($entities as $entity_id => $entity) {
$row = [];
/** @var \Drupal\social_user_export\Plugin\UserExportPluginBase $instance */
foreach ($this->pluginDefinitions as $plugin) {
$configuration = $this
->getPluginConfiguration($plugin['id'], $entity_id);
$instance = $this->userExportPlugin
->createInstance($plugin['id'], $configuration);
$row[] = $instance
->getValue($entity);
}
$csv
->insertOne($row);
}
if ($this->context['sandbox']['current_batch'] * $this->context['sandbox']['batch_size'] >= $this->context['sandbox']['total']) {
$data = @file_get_contents($file_path);
$name = basename($this->context['sandbox']['results']['file_path']);
$path = 'private://csv';
if (\Drupal::service('file_system')
->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS) && file_save_data($data, $path . '/' . $name)) {
$url = Url::fromUri(file_create_url($path . '/' . $name));
$link = Link::fromTextAndUrl($this
->t('Download file'), $url);
$this
->messenger()
->addMessage($this
->t('Export is complete. @link', [
'@link' => $link
->toString(),
]));
}
else {
$this
->messenger()
->addMessage($this
->t('Could not save the export file.'), 'error');
$this->logger
->error('Could not save the export file on: %name.', [
'%name' => $name,
]);
}
}
}
/**
* {@inheritdoc}
*/
public function execute($object = NULL) {
$this
->executeMultiple([
$object,
]);
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
/** @var \Drupal\user\UserInterface $object */
// @todo Check for export access instead.
return $object
->access('view', $account, $return_as_object);
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return $form;
}
/**
* Returns the directory that forms the base for this exports file output.
*
* This method wraps file_directory_temp() to give inheriting classes the
* ability to use a different file system than the temporary file system.
* This was previously possible but was changed in #3075818.
*
* @return string
* The path to the Drupal directory that should be used for this export.
*/
protected function getBaseOutputDirectory() : string {
return \Drupal::service('file_system')
->getTempDirectory();
}
/**
* Returns a unique file path for this export.
*
* The returned path is relative to getBaseOutputDirectory(). This allows it
* to work on distributed systems where the temporary file path may change
* in between batch ticks.
*
* To make sure the file can be downloaded, the path must be declared in the
* download pattern of the social user export module.
*
* @see social_user_export_file_download()
*
* @return string
* The path to the file.
*/
protected function generateFilePath() : string {
$hash = md5(microtime(TRUE));
return 'export-users-' . substr($hash, 20, 12) . '.csv';
}
/**
* Gets export plugin's configuration.
*
* @param int $plugin_id
* The plugin ID.
* @param int $entity_id
* The position of an entity in the entities list.
*
* @return array
* An array of export plugin's configuration.
*/
public function getPluginConfiguration($plugin_id, $entity_id) {
return [];
}
/**
* Check the access of export plugins based on config and permission.
*
* @param array $definitions
* The plugin definitions.
*
* @return array
* Returns only the plugins the user has access to.
*/
protected function pluginAccess(array $definitions) : array {
// When the user has access to administer users we know they may export all
// the available data.
if ($this->currentUser
->hasPermission('administer users')) {
return $definitions;
}
// Now we go through all the definitions and check if they should be removed
// or not based upon the config set by the site manager.
$allowed_plugins = $this->config
->get('plugins');
foreach ($definitions as $key => $definition) {
if (!array_key_exists($definition['id'], $allowed_plugins) || empty($allowed_plugins[$definition['id']])) {
unset($definitions[$key]);
}
}
return $definitions;
}
/**
* Order by weight.
*
* @param array $a
* First parameter.
* @param array $b
* Second parameter.
*
* @return int
* The weight to be used for the usort function.
*/
protected function sortDefinitions(array $a, array $b) : int {
if (isset($a['weight'], $b['weight'])) {
return $a['weight'] < $b['weight'] ? -1 : 1;
}
return 0;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
ExportUser:: |
protected | property | The user export plugin config object. | |
ExportUser:: |
protected | property | The current user account. | |
ExportUser:: |
protected | property | A logger instance. | |
ExportUser:: |
protected | property | User export plugin definitions. | |
ExportUser:: |
protected | property | The User export plugin manager. | |
ExportUser:: |
public | function |
Checks object access. Overrides ActionInterface:: |
2 |
ExportUser:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
ExportUser:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
1 |
ExportUser:: |
public | function |
Executes the plugin. Overrides ExecutableInterface:: |
|
ExportUser:: |
public | function |
Execute action on multiple entities. Overrides ViewsBulkOperationsActionBase:: |
2 |
ExportUser:: |
protected | function | Returns a unique file path for this export. | 2 |
ExportUser:: |
protected | function | Returns the directory that forms the base for this exports file output. | |
ExportUser:: |
public | function | Gets export plugin's configuration. | 1 |
ExportUser:: |
protected | function | Check the access of export plugins based on config and permission. | |
ExportUser:: |
protected | function | Order by weight. | |
ExportUser:: |
public | function |
Constructs a ExportUser object. Overrides PluginBase:: |
1 |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
ViewsBulkOperationsActionBase:: |
protected | property |
Configuration array. Overrides PluginBase:: |
|
ViewsBulkOperationsActionBase:: |
protected | property | Action context. | |
ViewsBulkOperationsActionBase:: |
protected | property | The processed view. | |
ViewsBulkOperationsActionBase:: |
public static | function | Default custom access callback. | |
ViewsBulkOperationsActionBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
|
ViewsBulkOperationsActionBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
ViewsBulkOperationsActionBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
ViewsBulkOperationsActionBase:: |
public | function |
Set action context. Overrides ViewsBulkOperationsActionInterface:: |
|
ViewsBulkOperationsActionBase:: |
public | function |
Set view object. Overrides ViewsBulkOperationsActionInterface:: |
|
ViewsBulkOperationsActionBase:: |
public | function | Default configuration form submit handler. | 1 |
ViewsBulkOperationsActionBase:: |
public | function | Default configuration form validator. | |
ViewsBulkOperationsActionCompletedTrait:: |
public static | function | Batch finished callback. | 1 |
ViewsBulkOperationsActionCompletedTrait:: |
public static | function | Set message function wrapper. | 1 |
ViewsBulkOperationsActionCompletedTrait:: |
public static | function | Translation function wrapper. | 1 |