You are here

abstract class VboExportBase 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
  2. 8.2 src/Plugin/Action/VboExportBase.php \Drupal\vbo_export\Plugin\Action\VboExportBase

Base class for export actions.

Hierarchy

Expanded class hierarchy of VboExportBase

File

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

Namespace

Drupal\vbo_export\Plugin\Action
View 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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
VboExportBase::access public function Checks object access. Overrides ActionInterface::access
VboExportBase::execute public function Executes the plugin. Overrides ExecutableInterface::execute
VboExportBase::executeMultiple public function Execute multiple handler. Overrides ActionBase::executeMultiple
VboExportBase::sendToFile protected function Output generated string to file. Message user.
VboExportBase::THEME constant 2