PrintableLinkBuilder.php in Printer and PDF versions for Drupal 8+ 2.x
File
src/PrintableLinkBuilder.php
View source
<?php
namespace Drupal\printable;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
class PrintableLinkBuilder implements PrintableLinkBuilderInterface {
protected $configFactory;
protected $urlGenerator;
protected $printableFormatManager;
public function __construct(ConfigFactoryInterface $config_factory, PrintableFormatPluginManager $printable_format_manager) {
$this->configFactory = $config_factory;
$this->printableFormatManager = $printable_format_manager;
}
public function buildLinks(EntityInterface $entity = NULL) {
$links = [];
$printable_settings = $this->configFactory
->get('printable.settings');
foreach ($this->printableFormatManager
->getDefinitions() as $key => $definition) {
$links[$key] = [
'title' => $definition['title'],
'url' => Url::fromRoute('printable.show_format.' . $entity
->getEntityTypeId(), [
'printable_format' => $key,
'entity' => $entity
->id(),
]),
];
if ($printable_settings
->get('open_target_blank') && ($key == 'print' or !$printable_settings
->get('save_pdf'))) {
$links[$key]['attributes']['target'] = '_blank';
}
}
return $links;
}
}