You are here

function printable_entity_view in Printer and PDF versions for Drupal 8+ 8

Same name and namespace in other branches
  1. 2.x printable.module \printable_entity_view()

Implements hook_entity_view().

File

./printable.module, line 194
Provides printer friendly content entities.

Code

function printable_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  $config = \Drupal::config('printable.settings');
  \Drupal::service('renderer')
    ->addCacheableDependency($build, $config);
  $printable_manager = \Drupal::service('printable.entity_manager');
  $link_builder = \Drupal::service('printable.link_builder');
  if ($printable_manager
    ->isPrintableEntity($entity) && !empty($entity
    ->id())) {
    $print_setting = $config
      ->get('printable_print_link_locations');
    $pdf_setting = $config
      ->get('printable_pdf_link_locations');

    // Get the name of entity over here.
    $entity_name = $printable_manager
      ->getEntityName($entity);
    $printable_navigation = [
      '#theme' => 'links__entity__printable',
      '#links' => $link_builder
        ->buildLinks($entity),
      '#attributes' => [
        'class' => [
          'pre_links',
        ],
      ],
    ];
    if (!in_array($entity_name, $print_setting)) {
      unset($printable_navigation['#links']['print']);
    }
    $pdf_library = (string) $config
      ->get('pdf_tool');
    if (!$pdf_library || !in_array($entity_name, $pdf_setting)) {
      unset($printable_navigation['#links']['pdf']);
    }

    // Add the build links to the entity being rendered.
    $build['printable_navigation'] = [
      '#markup' => '<strong class="node_view">' . drupal_render($printable_navigation) . '</strong>',
      '#attached' => [
        'library' => [
          'printable/entity-links',
        ],
      ],
      '#weight' => 100,
      '#cache' => [
        'tags' => $entity
          ->getEntityType()
          ->getListCacheTags(),
      ],
    ];
  }
}