View source
<?php
namespace Drupal\printable\Plugin;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\printable\LinkExtractor\LinkExtractorInterface;
use Drupal\printable\PrintableCssIncludeInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
abstract class PrintableFormatBase extends PluginBase implements PrintableFormatInterface, ContainerFactoryPluginInterface {
protected $configFactory;
protected $content;
protected $footerContent;
protected $printableCssInclude;
protected $linkExtractor;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ConfigFactoryInterface $config_factory, PrintableCssIncludeInterface $printable_css_include, LinkExtractorInterface $link_extractor) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
$this->printableCssInclude = $printable_css_include;
$this->linkExtractor = $link_extractor;
$this->configuration += $this
->defaultConfiguration();
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('printable.css_include'), $container
->get('printable.link_extractor'));
}
public function getLabel() {
return $this->pluginDefinition['title'];
}
public function getDescription() {
return $this->pluginDefinition['description'];
}
public function defaultConfiguration() {
return [];
}
public function getConfiguration() {
return $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = $configuration;
$this->configFactory
->getEditable('printable.format')
->set($this
->getPluginId(), $this->configuration)
->save();
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function setContent(array $content) {
$this->content = $content;
$this->footerContent = NULL;
if ($this->configFactory
->get('printable.settings')
->get('list_attribute')) {
$this->footerContent = $this->linkExtractor
->listAttribute((string) render($this->content));
}
}
public function getResponse() {
return new Response($this
->getOutput());
}
protected function buildContent() {
$build = [
'#theme' => [
'printable__' . $this
->getPluginId(),
'printable',
],
'#header' => [
'#theme' => [
'printable_header__' . $this
->getPluginId(),
'printable_header',
],
'#logo_url' => theme_get_setting('logo.url'),
],
'#content' => $this->content,
'#footer' => [
'#theme' => [
'printable_footer__' . $this
->getPluginId(),
'printable_footer',
],
'#footer_content' => $this->footerContent,
],
];
if ($include_path = $this->printableCssInclude
->getCssIncludePath()) {
$build['#attached']['css'][] = $include_path;
}
return $build;
}
protected function extractLinks($content) {
if ($this->configFactory
->get('printable.settings')
->get('extract_links')) {
$rendered_page = $this->linkExtractor
->extract($content);
}
else {
$rendered_page = $this->linkExtractor
->removeAttribute($content, 'href');
}
return $rendered_page;
}
protected function getOutput() {
$content = $this
->buildContent();
return $this
->extractLinks(render($content));
}
}