You are here

class PrintableLinkBuilder in Printer and PDF versions for Drupal 8+ 8

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

Helper class for the printable module.

Hierarchy

Expanded class hierarchy of PrintableLinkBuilder

1 file declares its use of PrintableLinkBuilder
PrintableLinkBuilderTest.php in tests/src/Unit/PrintableLinkBuilderTest.php
1 string reference to 'PrintableLinkBuilder'
printable.services.yml in ./printable.services.yml
printable.services.yml
1 service uses PrintableLinkBuilder
printable.link_builder in ./printable.services.yml
Drupal\printable\PrintableLinkBuilder

File

src/PrintableLinkBuilder.php, line 12

Namespace

Drupal\printable
View source
class PrintableLinkBuilder implements PrintableLinkBuilderInterface {

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

  /**
   * The URL generator service.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface
   */
  protected $urlGenerator;

  /**
   * The printable format plugin manager.
   *
   * @var \Drupal\printable\PrintableFormatPluginManager
   */
  protected $printableFormatManager;

  /**
   * Constructs a new PrintableLinkBuilder object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory service.
   * @param \Drupal\printable\PrintableFormatPluginManager $printable_format_manager
   *   The printable format plugin manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, PrintableFormatPluginManager $printable_format_manager) {
    $this->configFactory = $config_factory;
    $this->printableFormatManager = $printable_format_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function buildLinks(EntityInterface $entity = NULL) {
    $links = [];
    $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] = [
        'title' => $definition['title'],
        'url' => Url::fromRoute('printable.show_format.' . $entity
          ->getEntityTypeId(), [
          '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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PrintableLinkBuilder::$configFactory protected property The config factory service.
PrintableLinkBuilder::$printableFormatManager protected property The printable format plugin manager.
PrintableLinkBuilder::$urlGenerator protected property The URL generator service.
PrintableLinkBuilder::buildLinks public function Build a render array of the printable links for a given entity. Overrides PrintableLinkBuilderInterface::buildLinks
PrintableLinkBuilder::__construct public function Constructs a new PrintableLinkBuilder object.