You are here

Page.php in Views PDF 8

File

src/Plugin/views/display/Page.php
View source
<?php

/**
 * @file
 * Contains \Drupal\views_pdf\Plugin\views\display\Page.
 */
namespace Drupal\views_pdf\Plugin\views\display;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\State\StateInterface;
use Drupal\views\Plugin\views\display\Page as ViewsPage;
use Drupal\views_pdf\ViewsPdfBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;

/**
 * This class contains all the functionality of the PDF display.
 *
 * @ingroup views_display_plugins
 *
 * @ViewsDisplay(
 *   id = "views_pdf_page",
 *   title = @Translation("PDF Page"),
 *   help = @Translation("Display the view as a pdf page, with a URL and menu links."),
 *   uses_menu_links = TRUE,
 *   uses_route = TRUE,
 *   contextual_links_locations = {"page"},
 *   theme = "views_view",
 *   admin =
 *   @Translation("PDF Page")
 * )
 */
class Page extends ViewsPage {

  /**
   * {@inheritdoc}
   */
  public function defaultableSections($section = NULL) {
    if (in_array($section, [
      'style_options',
      'style_plugin',
      'row_options',
      'row_plugin',
    ])) {
      return FALSE;
    }
    $sections = parent::defaultableSections($section);

    // Tell views our sitename_title option belongs in the title section.
    if ($section === 'title') {
      $sections[] = 'sitename_title';
    }
    elseif (!$section) {
      $sections['title'][] = 'sitename_title';
    }
    return $sections;
  }

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['displays'] = [
      'default' => [],
    ];

    // Overrides for standard stuff:
    $options['style_plugin']['default'] = 'unformatted_pdf';
    $options['style_options']['default'] = [
      'mission_description' => FALSE,
      'description' => '',
    ];
    $options['sitename_title']['default'] = FALSE;
    $options['row_plugin']['default'] = 'pdf_fields';
    $options['defaults']['default']['style_plugin'] = FALSE;
    $options['defaults']['default']['style_options'] = FALSE;
    $options['defaults']['default']['row_plugin'] = FALSE;
    $options['defaults']['default']['row_options'] = FALSE;

    // New Options
    $options['default_page_format'] = [
      'default' => 'A4',
    ];
    $options['default_page_format_custom'] = [
      'default' => '',
    ];
    $options['default_page_orientation'] = [
      'default' => 'P',
    ];
    $options['unit'] = [
      'default' => 'mm',
    ];
    $options['margin_left'] = [
      'default' => '15',
    ];
    $options['margin_right'] = [
      'default' => '15',
    ];
    $options['margin_top'] = [
      'default' => '15',
    ];
    $options['margin_bottom'] = [
      'default' => '15',
    ];
    $options['leading_template'] = [
      'default' => '',
    ];
    $options['template'] = [
      'default' => '',
    ];
    $options['succeed_template'] = [
      'default' => '',
    ];
    $options['default_font_family'] = [
      'default' => 'helvetica',
    ];
    $options['default_font_style'] = [
      'default' => [],
    ];
    $options['default_font_size'] = [
      'default' => '11',
    ];
    $options['default_text_align'] = [
      'default' => 'L',
    ];
    $options['default_font_color'] = [
      'default' => '000000',
    ];
    $options['default_text_hyphenate'] = [
      'default' => 'none',
    ];
    $options['css_file'] = [
      'default' => '',
    ];
    return $options;
  }
  public function optionsSummary(&$categories, &$options) {
    parent::optionsSummary($categories, $options);
    $fonts = ViewsPdfBase::getAvailableFontsCleanList();

    // Change Page title:
    $categories['page'] = [
      'title' => t('PDF settings'),
      'column' => 'second',
      'build' => [
        '#weight' => -10,
      ],
    ];

    // Add for pdf page settings
    $options['pdf_page'] = [
      'category' => 'page',
      'title' => t('PDF Page Settings'),
      'value' => $this
        ->getOption('default_page_format'),
      'desc' => t('Define some PDF specific settings.'),
    ];

    // Add for pdf font settings
    $options['pdf_fonts'] = [
      'category' => 'page',
      'title' => t('PDF Fonts Settings'),
      'value' => t(':family at :size pt', [
        ':family' => $fonts[$this
          ->getOption('default_font_family')],
        ':size' => $this
          ->getOption('default_font_size'),
      ]),
      'desc' => t('Define some PDF specific settings.'),
    ];

    // add for pdf template settings
    if ($this
      ->getOption('leading_template') !== '' || $this
      ->getOption('template') !== '' || $this
      ->getOption('succeed_template') !== '') {
      $isAnyTemplate = t('Yes');
    }
    else {
      $isAnyTemplate = t('No');
    }
    $options['pdf_template'] = [
      'category' => 'page',
      'title' => t('PDF Template Settings'),
      'value' => $isAnyTemplate,
      'desc' => t('Define some PDF specific settings.'),
    ];
    if ($this
      ->getOption('css_file') === '') {
      $css_file = t('None');
    }
    else {
      $css_file = $this
        ->getOption('css_file');
    }
    $options['css'] = [
      'category' => 'page',
      'title' => t('CSS File'),
      'value' => $css_file,
      'desc' => t('Define a CSS file attached to all HTML output.'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    switch ($form_state
      ->get('section')) {
      case 'pdf_page':
        $form['#title'] .= t('PDF Page Options');
        $form['default_page_format'] = [
          '#type' => 'select',
          '#title' => t('Default Page Format'),
          '#required' => TRUE,
          '#options' => [
            's',
            'e',
          ],
          '#description' => t('This is the default page format. If you specifiy a different format in the template section, this settings will be override.'),
          '#default_value' => $this
            ->getOption('default_page_format'),
        ];
        $form['default_page_format_custom'] = [
          '#type' => 'textfield',
          '#title' => t('Custom Page Format'),
          '#description' => t('Here you can specifiy a custom page format. The schema is "[width]x[height]".'),
          '#default_value' => $this
            ->getOption('default_page_format_custom'),
        ];
        $form['default_page_orientation'] = [
          '#type' => 'radios',
          '#title' => t('Default Page Orientation'),
          '#required' => TRUE,
          '#options' => [
            'P' => t('Portrait'),
            'L' => t('Landscape'),
          ],
          '#description' => t('This is the default page orientation.'),
          '#default_value' => $this
            ->getOption('default_page_orientation'),
        ];
        $form['unit'] = [
          '#type' => 'select',
          '#title' => t('Unit'),
          '#required' => TRUE,
          '#options' => [
            'mm' => t('mm: Millimeter'),
            'pt' => t('pt: Point'),
            'cm' => t('cm: Centimeter'),
            'in' => t('in: Inch'),
          ],
          '#description' => t('This is the unit for the entered unit data. If you change this option all defined units were changed, but not converted.'),
          '#default_value' => $this
            ->getOption('unit'),
        ];
        $form['margin_left'] = [
          '#type' => 'textfield',
          '#title' => t('Margin: Left'),
          '#required' => TRUE,
          '#default_value' => $this
            ->getOption('margin_left'),
        ];
        $form['margin_right'] = [
          '#type' => 'textfield',
          '#title' => t('Margin: Right'),
          '#required' => TRUE,
          '#default_value' => $this
            ->getOption('margin_right'),
        ];
        $form['margin_top'] = [
          '#type' => 'textfield',
          '#title' => t('Margin: Top'),
          '#required' => TRUE,
          '#default_value' => $this
            ->getOption('margin_top'),
        ];
        $form['margin_bottom'] = [
          '#type' => 'textfield',
          '#title' => t('Margin: Bottom'),
          '#required' => TRUE,
          '#default_value' => $this
            ->getOption('margin_bottom'),
        ];
        break;
      case 'pdf_fonts':
        $fonts = ViewsPdfBase::getAvailableFontsCleanList();
        $font_styles = [
          'b' => t('Bold'),
          'i' => t('Italic'),
          'u' => t('Underline'),
          'd' => t('Line through'),
          'o' => t('Overline'),
        ];
        $align = [
          'L' => t('Left'),
          'C' => t('Center'),
          'R' => t('Right'),
          'J' => t('Justify'),
        ];
        $hyphenate = [
          'none' => t('None'),
          'auto' => t('Detect automatically'),
        ];
        $hyphenate = array_merge($hyphenate, ViewsPdfBase::getAvailableHyphenatePatterns());
        $form['#title'] .= t('PDF Default Font Options');
        $form['description'] = [
          '#prefix' => '<div class="description form-item">',
          '#suffix' => '</div>',
          '#value' => t('Here you specify a the default font settings for the document.'),
        ];
        $form['default_font_size'] = [
          '#type' => 'textfield',
          '#title' => t('Font Size'),
          '#size' => 10,
          '#default_value' => $this
            ->getOption('default_font_size'),
        ];
        $form['default_font_family'] = [
          '#type' => 'select',
          '#title' => t('Font Family'),
          '#options' => $fonts,
          '#size' => 5,
          '#default_value' => $this
            ->getOption('default_font_family'),
        ];
        $form['default_font_style'] = [
          '#type' => 'checkboxes',
          '#title' => t('Font Style'),
          '#options' => $font_styles,
          '#default_value' => $this
            ->getOption('default_font_style'),
        ];
        $form['default_text_align'] = [
          '#type' => 'radios',
          '#title' => t('Text Alignment'),
          '#options' => $align,
          '#default_value' => $this
            ->getOption('default_text_align'),
        ];
        $form['default_text_hyphenate'] = [
          '#type' => 'select',
          '#title' => t('Text Hyphenation'),
          '#options' => $hyphenate,
          '#description' => t('If you want to use hyphenation, then you need to download from <a href="@url">ctan.org</a> your needed pattern set. Then upload it to the dir "hyphenate_patterns" in the TCPDF lib directory. Perhaps you need to create the dir first. If you select the automated detection, then we try to get the language of the current node and select an appropriate hyphenation pattern.', [
            '@url' => 'http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/tex',
          ]),
          '#default_value' => $this
            ->getOption('default_text_hyphenate'),
        ];
        $form['default_font_color'] = [
          '#type' => 'textfield',
          '#title' => t('Text Color'),
          '#description' => t('If a value is entered without a comma, it will be interpreted as a hexadecimal RGB color. Normal RGB can be used by separating the components by a comma. e.g 255,255,255 for white. A CMYK color can be entered in the same way as RGB. e.g. 0,100,0,0 for magenta.'),
          '#size' => 20,
          '#default_value' => $this
            ->getOption('default_font_color'),
        ];
        break;
      case 'pdf_template':
        $form['#title'] .= t('PDF Templates');
        $templates = array_merge([
          t('-- None --'),
        ], []);
        $form['leading_template'] = [
          '#type' => 'select',
          '#options' => $templates,
          '#title' => t('Leading PDF Template'),
          '#required' => FALSE,
          '#description' => t('Here you specify a PDF file to be printed in front of every row.'),
          '#default_value' => $this
            ->getOption('leading_template'),
        ];
        $form['template'] = [
          '#type' => 'select',
          '#options' => $templates,
          '#title' => t('Template PDF'),
          '#description' => t('Here you specify a PDF file on which the content is printed. The first page of this document is used for the first page, in the target document. The second page is used for the second page in the target document and so on. If the target document has more that this template file, the last page of the template will be repeated. The leading document has no effect on the order of the pages.'),
          '#default_value' => $this
            ->getOption('template'),
        ];
        $form['succeed_template'] = [
          '#type' => 'select',
          '#options' => $templates,
          '#title' => t('Succeed PDF Template'),
          '#required' => FALSE,
          '#description' => t('Here you specify a PDF file to be printed after the main content.'),
          '#default_value' => $this
            ->getOption('succeed_template'),
        ];
        $form['template_file'] = [
          '#type' => 'file',
          '#title' => t('Upload New Template File'),
        ];
        $form['#attached']['js'][] = [
          'type' => 'setting',
          'data' => [
            'urlIsAjaxTrusted' => [
              '',
            ],
          ],
        ];
        break;
      case 'displays':
        $form['#title'] .= t('Attach to');
        $displays = [];
        foreach ($this->view
          ->getDisplay() as $display_id => $display) {
          if (!empty($display->handler) && $display->handler
            ->accept_attachments()) {
            $displays[$display_id] = $display->display_title;
          }
        }
        $form['displays'] = [
          '#type' => 'checkboxes',
          '#description' => t('The feed icon will be available only to the selected displays.'),
          '#options' => $displays,
          '#default_value' => $this
            ->getOption('displays'),
        ];
        break;
      case 'css':
        $form['#title'] .= t('CSS File');
        $form['css_file'] = [
          '#type' => 'textfield',
          '#description' => t('URL to a CSS file. This file is attached to all fields, rendered as HTML.'),
          '#default_value' => $this
            ->getOption('css_file'),
        ];
        break;
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function &setPageRenderArray(array &$element = NULL) {
    if (isset($element)) {
      static::$pageRenderArray =& $element;
    }
    return static::$pageRenderArray;
  }

  /**
   * Gets the current views page render array.
   *
   * @return array
   *   The page render array.
   */
  public static function &getPageRenderArray() {
    return static::$pageRenderArray;
  }

  /**
   * {@inheritdoc}
   */
  public function execute() {
    parent::execute();

    // And now render the view.
    return $this->view
      ->render();
  }
  public function preview() {
    return $this
      ->t('The PDF display does not provide a preview.');
  }

  /**
   * {@inheritdoc}
   */
  public function getPagerText() {
    return [
      'items per page title' => $this
        ->t('Items per page'),
      'items per page description' => $this
        ->t('The number of items to display per page. Enter 0 for no limit.'),
    ];
  }

}

Classes

Namesort descending Description
Page This class contains all the functionality of the PDF display.