abstract class VboExportBase in VBO export 8
Same name and namespace in other branches
- 8.3 src/Plugin/Action/VboExportBase.php \Drupal\vbo_export\Plugin\Action\VboExportBase
- 8.2 src/Plugin/Action/VboExportBase.php \Drupal\vbo_export\Plugin\Action\VboExportBase
Base class for export actions.
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\vbo_export\Plugin\Action\VboExportBase
- class \Drupal\Core\Action\ActionBase implements ActionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of VboExportBase
File
- src/
Plugin/ Action/ VboExportBase.php, line 13
Namespace
Drupal\vbo_export\Plugin\ActionView source
abstract class VboExportBase extends ActionBase {
const THEME = '';
/**
* Output generated string to file. Message user.
*/
protected function sendToFile($csv_string, $filename) {
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
$wrappers = $stream_wrapper_manager
->getWrappers();
if (isset($wrappers['private'])) {
$wrapper = 'private';
}
else {
$wrapper = 'public';
}
if (!empty($csv_string)) {
$destination = $wrapper . '://' . $filename;
$file = file_save_data($csv_string, $destination, FILE_EXISTS_REPLACE);
$file
->setTemporary();
$file
->save();
$file_url = Url::fromUri(file_create_url($file
->getFileUri()));
$link = \Drupal::l(t('Click here'), $file_url);
drupal_set_message(t('Export file created, @link to download.', array(
'@link' => $link,
)));
}
}
/**
* Execute multiple handler.
*
* Execute action on multiple entities to generate csv output
* and display a download link.
*/
public function executeMultiple(array $entities) {
$params = \Drupal::routeMatch()
->getRouteObject()
->getDefaults();
if (!empty($params['view_id'])) {
$view = Views::getView($params['view_id']);
$view
->setDisplay($params['display_id']);
$nids = array();
foreach ($entities as $entity) {
$id = $entity
->id();
$nids[$id] = $entity
->id();
}
$view
->build();
$base_table = $view->storage
->get('base_table');
$alias = $view->query->tables[$base_table][$base_table]['alias'];
$view->build_info['query']
->condition($alias . '.' . $view->storage
->get('base_field'), $nids, 'in');
$view->query
->execute($view);
$view->result = array_values($view->result);
// Variables for vbo_export theme.
$renderable = [
'#theme' => static::THEME,
];
// Build csv structure array.
$structure = [];
foreach ($view->field as $id => $field) {
if ($id == 'node_bulk_form' || $field->options['exclude'] || empty($field->options['entity_field'])) {
continue;
}
$structure[$id] = [
'id' => $id,
'label' => $field->options['label'],
'entity_type' => $field->options['entity_type'],
'field' => $field->options['entity_field'],
'plugin_id' => $field->options['plugin_id'],
];
}
if (!empty($structure) && !empty($view->result)) {
// Build csv header.
foreach ($structure as $item) {
$renderable['#header'][] = $item['label'];
}
// Render csv rows.
$view->style_plugin
->preRender($view->result);
foreach ($view->result as $num => $row) {
foreach ($structure as $field_id => $item) {
$renderable['#rows'][$num][$field_id] = $view->style_plugin
->getField($num, $field_id)
->__toString();
}
}
$renderer = \Drupal::service('renderer');
$csv_string = $renderer
->render($renderable);
$rand = substr(hash('ripemd160', uniqid()), 0, 8);
$filename = $params['view_id'] . '_' . date('Y_m_d_H_i', REQUEST_TIME) . '-' . $rand . '.' . static::EXTENSION;
$this
->sendToFile($csv_string, $filename);
}
}
}
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
$this
->executeMultiple([
$entity,
]);
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
$access = $object
->access('view', $account, TRUE);
return $access
->isAllowed();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
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:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
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. | |
VboExportBase:: |
public | function |
Checks object access. Overrides ActionInterface:: |
|
VboExportBase:: |
public | function |
Executes the plugin. Overrides ExecutableInterface:: |
|
VboExportBase:: |
public | function |
Execute multiple handler. Overrides ActionBase:: |
|
VboExportBase:: |
protected | function | Output generated string to file. Message user. | |
VboExportBase:: |
constant | 2 |