You are here

public function VboExportBase::executeMultiple in VBO export 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/Action/VboExportBase.php \Drupal\vbo_export\Plugin\Action\VboExportBase::executeMultiple()
  2. 8.2 src/Plugin/Action/VboExportBase.php \Drupal\vbo_export\Plugin\Action\VboExportBase::executeMultiple()

Execute multiple handler.

Execute action on multiple entities to generate csv output and display a download link.

Overrides ActionBase::executeMultiple

1 call to VboExportBase::executeMultiple()
VboExportBase::execute in src/Plugin/Action/VboExportBase.php
Executes the plugin.

File

src/Plugin/Action/VboExportBase.php, line 47

Class

VboExportBase
Base class for export actions.

Namespace

Drupal\vbo_export\Plugin\Action

Code

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);
    }
  }
}