/** * {@inheritdoc} */ public function buildLinks(EntityInterface $entity = NULL) { $links = array(); $printable_settings = $this->configFactory->get('printable.settings'); // Build the array of links to be added to the entity. foreach ($this->printableFormatManager->getDefinitions() as $key => $definition) { $links[$key] = array('title' => $definition['title'], 'url' => Url::fromRoute('printable.show_format.' . $entity->getEntityTypeId(), array('printable_format' => $key, 'entity' => $entity->id()))); // Add target "blank" if the configuration option is set. if ($printable_settings->get('open_target_blank') && ($key == 'print' or !$printable_settings->get('save_pdf'))) { $links[$key]['attributes']['target'] = '_blank'; } } return $links; }
/** * Returns the entity rendered via the given printable format. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity to be printed. * @param string $printable_format * The identifier of the hadcopy format plugin. * * @return \Symfony\Component\HttpFoundation\Response * The printable response. */ public function showFormat(EntityInterface $entity, $printable_format) { if ($this->printableFormatManager->getDefinition($printable_format)) { $format = $this->printableFormatManager->createInstance($printable_format); $content = $this->entityManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, 'printable'); $format->setContent($content); if ($printable_format == 'print') { return $format->getResponse(); } else { $format->getResponse($content); $source_url = \Drupal::request()->getRequestUri(); $pos = strpos($source_url, "printable"); $pos_node = strpos($source_url, '/', $pos + 11); $source_url = substr($source_url, 0, $pos) . substr($source_url, $pos_node + 1); return new RedirectResponse($source_url); } } else { throw new NotFoundHttpException(); } }