You are here

abstract class PrintableFormatBase in Printer and PDF versions for Drupal 8+ 8

Same name and namespace in other branches
  1. 2.x src/Plugin/PrintableFormatBase.php \Drupal\printable\Plugin\PrintableFormatBase

Provides a base class for Filter plugins.

Hierarchy

Expanded class hierarchy of PrintableFormatBase

2 files declare their use of PrintableFormatBase
PdfFormat.php in modules/printable_pdf/src/Plugin/PrintableFormat/PdfFormat.php
PrintFormat.php in src/Plugin/PrintableFormat/PrintFormat.php

File

src/Plugin/PrintableFormatBase.php, line 17

Namespace

Drupal\printable\Plugin
View source
abstract class PrintableFormatBase extends PluginBase implements PrintableFormatInterface, ContainerFactoryPluginInterface {

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * A render array of the content to be output by the printable format.
   *
   * @var array
   */
  protected $content;

  /**
   * A string containing the list of links present in the page.
   *
   * @var string
   */
  protected $footerContent;

  /**
   * Printable CSS include manager.
   *
   * @var \Drupal\printable\PrintableCssIncludeInterface
   */
  protected $printableCssInclude;

  /**
   * Printable link extractor.
   *
   * @var \Drupal\printable\LinkExtractor\LinkExtractorInterface
   */
  protected $linkExtractor;

  /**
   * {@inheritdoc}
   *
   * @param array $configuration
   *   The configuration array.
   * @param string $plugin_id
   *   The plugin ID.
   * @param array $plugin_definition
   *   The plugin definition.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   * @param \Drupal\printable\PrintableCssIncludeInterface $printable_css_include
   *   The printable CSS include manager.
   * @param \Drupal\printable\LinkExtractor\LinkExtractorInterface $link_extractor
   *   The link extractor.
   */
  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();
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel() {
    return $this->pluginDefinition['title'];
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->pluginDefinition['description'];
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = $configuration;
    $this->configFactory
      ->getEditable('printable.format')
      ->set($this
      ->getPluginId(), $this->configuration)
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  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));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getResponse() {
    return new Response($this
      ->getOutput());
  }

  /**
   * Build a render array of the content, wrapped in the printable theme.
   *
   * @return array
   *   A render array representing the themed output of the content.
   */
  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;
  }

  /**
   * Extracts the links present in HTML string.
   *
   * @param string $content
   *   The HTML of the page to be added.
   *
   * @return string
   *   The HTML string with presence of links dependending on configuration.
   */
  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;
  }

  /**
   * Get the HTML output of the whole page and pass to the response object.
   *
   * @return string
   *   The HTML string representing the output of this printable format.
   */
  protected function getOutput() {
    $content = $this
      ->buildContent();

    // @todo add a renderer service over here.
    return $this
      ->extractLinks(render($content));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DependentPluginInterface::calculateDependencies public function Calculates dependencies for the configured plugin. 19
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginFormInterface::buildConfigurationForm public function Form constructor. 36
PluginFormInterface::submitConfigurationForm public function Form submission handler. 32
PrintableFormatBase::$configFactory protected property The config factory service.
PrintableFormatBase::$content protected property A render array of the content to be output by the printable format.
PrintableFormatBase::$footerContent protected property A string containing the list of links present in the page.
PrintableFormatBase::$linkExtractor protected property Printable link extractor.
PrintableFormatBase::$printableCssInclude protected property Printable CSS include manager.
PrintableFormatBase::buildContent protected function Build a render array of the content, wrapped in the printable theme.
PrintableFormatBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
PrintableFormatBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurablePluginInterface::defaultConfiguration 2
PrintableFormatBase::extractLinks protected function Extracts the links present in HTML string.
PrintableFormatBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurablePluginInterface::getConfiguration
PrintableFormatBase::getDescription public function Returns the administrative description for this format plugin. Overrides PrintableFormatInterface::getDescription
PrintableFormatBase::getLabel public function Returns the administrative label for this format plugin. Overrides PrintableFormatInterface::getLabel
PrintableFormatBase::getOutput protected function Get the HTML output of the whole page and pass to the response object.
PrintableFormatBase::getResponse public function Returns the response object for this format plugin. Overrides PrintableFormatInterface::getResponse 1
PrintableFormatBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface::setConfiguration
PrintableFormatBase::setContent public function Set the content for the printable response. Overrides PrintableFormatInterface::setContent
PrintableFormatBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 1
PrintableFormatBase::__construct public function Overrides PluginBase::__construct 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.