Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for configurable filename #13

Merged
merged 4 commits into from Aug 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/Plugin/views/display/DataExport.php
Expand Up @@ -2,6 +2,8 @@

namespace Drupal\views_data_export\Plugin\views\display;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheableResponse;
use Drupal\Core\Form\FormStateInterface;
use Drupal\rest\Plugin\views\display\RestExport;
use Drupal\views\ViewExecutable;
Expand All @@ -25,6 +27,58 @@
*/
class DataExport extends RestExport {

/**
* {@inheritdoc}
*/
public static function buildResponse($view_id, $display_id, array $args = []) {
// Do not call the parent method, as it makes the response harder to alter.
// @see https://www.drupal.org/node/2779807
$build = static::buildBasicRenderable($view_id, $display_id, $args);

// Setup an empty response, so for example, the Content-Disposition header
// can be set.
$response = new CacheableResponse('', 200);
$build['#response'] = $response;

/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');

$output = (string) $renderer->renderRoot($build);

$response->setContent($output);
$cache_metadata = CacheableMetadata::createFromRenderArray($build);
$response->addCacheableDependency($cache_metadata);

$response->headers->set('Content-type', $build['#content_type']);

return $response;
}

/**
* {@inheritdoc}
*/
public function render() {

// Add the content disposition header if a custom filename has been used.
if (($response = $this->view->getResponse()) && $this->getOption('filename')) {
$response->headers->set('Content-Disposition', 'attachment; filename="' . $this->generateFilename($this->getOption('filename')) . '"');
}

return parent::render();
}

/**
* Given a filename and a view, generate a filename.
*
* @param $filename_pattern
* The filename, which may contain replacement tokens.
* @return string
* The filename with any tokens replaced.
*/
protected function generateFilename($filename_pattern) {
return $this->globalTokenReplace($filename_pattern);
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -67,6 +121,11 @@ public function optionsSummary(&$categories, &$options) {
'title' => $this->t('Attach to'),
'value' => $attach_to,
);

// Add filename to the summary if set.
if ($this->getOption('filename')) {
$options['path']['value'] .= $this->t(' (@filename)', ['@filename' => $this->getOption('filename')]);
}
}
/**
* {@inheritdoc}
Expand All @@ -80,6 +139,17 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
unset($form['style']['type']['#options']['serializer']);
break;

case 'path':
$form['filename'] = [
'#type' => 'textfield',
'#title' => $this->t('Filename'),
'#default_value' => $this->options['filename'],
'#description' => $this->t('The filename that will be suggested to the browser for downloading purposes. You may include replacement patterns from the list below.'),
];
// Support tokens.
$this->globalTokenForm($form, $form_state);
break;

case 'displays':
$form['#title'] .= $this->t('Attach to');
$displays = [];
Expand Down Expand Up @@ -135,6 +205,10 @@ public function submitOptionsForm(&$form, FormStateInterface $form_state) {
case 'displays':
$this->setOption($section, $form_state->getValue($section));
break;

case 'path':
$this->setOption('filename', $form_state->getValue('filename'));
break;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/views/style/DataExport.php
Expand Up @@ -153,7 +153,7 @@ public function attachTo(array &$build, $display_id, Url $url, $title) {
// Attach a link to the CSV feed, which is an alternate representation.
$build['#attached']['html_head_link'][][] = [
'rel' => 'alternate',
'type' => 'text/' . $type,
'type' => $this->displayHandler->getMimeType(),
'title' => $title,
'href' => $url,
];
Expand Down