Browse Source

added download link

main
astanley 7 hours ago
parent
commit
437ed4893f
  1. 38
      src/Controller/MetadataProfileController.php
  2. 3
      src/Routing/RouteSubscriber.php

38
src/Controller/MetadataProfileController.php

@ -19,6 +19,10 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeTypeInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
class MetadataProfileController extends ControllerBase {
@ -162,6 +166,16 @@ class MetadataProfileController extends ControllerBase {
$metadata_profile = $this->getMetadataProfile();
$entity = $this->routeMatch->getParameter($this->entityType);
$build['download'] = [
'#type' => 'link',
'#title' => $this->t('Download profile'),
'#url' => $entity->toUrl('metadata-download'),
'#attributes' => [
'class' => ['button', 'button--primary'],
],
'#weight' => -10,
];
$build['summary_table'] = [
'#type' => 'container',
'#attributes' => ['class' => ['scrolling-table-container']],
@ -867,27 +881,43 @@ class MetadataProfileController extends ControllerBase {
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
* The file download response.
*/
public function download() {
public function download(NodeTypeInterface $node_type) {
// Rebuild your state from the entity
$this->entityBundle = $node_type->id();
$this->entityTypeBundleOf = $node_type->getEntityType()->getBundleOf();
$contentType = $this->entityBundle;
$entityKey = $this->entityTypeBundleOf;
$headers = $this->getHeaders();
$metadata_profile = $this->getMetadataProfile();
$rows = $this->getRows($metadata_profile);
$rows = $this->sanitizeRowsForCSV($rows);
$filename_slug = "{$entityKey}__{$contentType}.csv";
$filename = $this->fileSystem->getTempDirectory() . '/' . $filename_slug;
// Write file to temporary filesystem.
if (file_exists($filename)) {
$this->fileSystem->delete($filename);
}
$fh = fopen($filename, 'w');
fputcsv($fh, $headers);
foreach ($rows as $row) {
fputcsv($fh, $row);
}
fclose($fh);
$request = new Request(['file' => $filename_slug]);
return $this->fileDownloadController->download($request, 'temporary');
$response = new BinaryFileResponse($filename);
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$filename_slug
);
return $response;
}
private function formatFacets(array $field_profile) {

3
src/Routing/RouteSubscriber.php

@ -40,8 +40,7 @@ class RouteSubscriber extends RouteSubscriberBase {
$collection->add("entity.$entity_type_id.metadata_profile", $route);
}
if ($route = $this->getEntityDownloadRoute($entity_type)) {
$collection->add("entity.$entity_type_id.metadata_profile.download", $route);
}
$collection->add("entity.$entity_type_id.metadata_download", $route); }
}
}

Loading…
Cancel
Save