You are here

views_data_export.module in Views data export 8

The views data export allowing exporting data from views displays.

The views data export module allows data to be exported from views displays.

File

views_data_export.module
View source
<?php

/**
 * @file
 * The views data export allowing exporting data from views displays.
 *
 * The views data export module allows data to be exported from views displays.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\query\QueryPluginBase;

/**
 * Implements hook_theme().
 */
function views_data_export_theme() {
  return [
    'export_icon' => [
      'variables' => [
        'format' => NULL,
        'url' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_help().
 */
function views_data_export_help($route_name, RouteMatchInterface $route_match) {
  if ($route_name === 'help.page.views_data_export') {
    $readme_file = file_exists(__DIR__ . '/README.md') ? __DIR__ . '/README.md' : __DIR__ . '/README.txt';
    if (!file_exists($readme_file)) {
      return NULL;
    }
    $text = file_get_contents($readme_file);
    if ($text && !\Drupal::moduleHandler()
      ->moduleExists('markdown')) {
      return '<pre>' . $text . '</pre>';
    }
    else {

      // Use the Markdown filter to render the README.
      $filter_manager = \Drupal::service('plugin.manager.filter');
      $settings = \Drupal::configFactory()
        ->get('markdown.settings')
        ->getRawData();
      $config = [
        'settings' => $settings,
      ];
      $filter = $filter_manager
        ->createInstance('markdown', $config);
      return $filter
        ->process($text, 'en');
    }
  }
  return NULL;
}

/**
 * Implements hook_ENTITY_TYPE_access().
 */
function views_data_export_file_access(EntityInterface $entity, $operation, AccountInterface $account) {

  /* @var $entity EntityInterface */
  if ($operation == 'download') {

    // Grant access to download the generate export if it was generated by the
    // current user.
    $pattern = '/\\/\\/views_data_export\\/(?<uid>\\d+)-\\d+-.+\\.csv$/';
    preg_match($pattern, $entity
      ->getFileUri(), $matches);
    if (isset($matches['uid']) && $matches['uid'] == $account
      ->id()) {
      return AccessResult::allowed();
    }
  }
}

/**
 * Implements hook_views_query_alter().
 */
function views_data_export_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if ($view
    ->getDisplay()
    ->getPluginId() != 'data_export') {
    return;
  }

  // Get facet source id from view display and alter search query because facet
  // module doesn't alter data_export displays.
  $facet_source = $view
    ->getDisplay()
    ->getOption('facet_settings');
  if (isset($facet_source) && $facet_source !== 'none') {
    $search_query = $query
      ->getSearchApiQuery();
    $facet_manager = \Drupal::service('facets.manager');
    $facet_manager
      ->alterQuery($search_query, $facet_source);
  }
}