diff --git a/src/Controller/MetadataProfileController.php b/src/Controller/MetadataProfileController.php index 18afa95..e38b740 100644 --- a/src/Controller/MetadataProfileController.php +++ b/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) { diff --git a/src/Routing/RouteSubscriber.php b/src/Routing/RouteSubscriber.php index ec67be5..f5f0dec 100644 --- a/src/Routing/RouteSubscriber.php +++ b/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); } } }