You are here

public function Pdf::buildOptionsForm in PDF Generator 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/views/display/Pdf.php \Drupal\pdf_generator\Plugin\views\display\Pdf::buildOptionsForm()

Provide a form to edit options for this plugin.

Overrides PathPluginBase::buildOptionsForm

File

src/Plugin/views/display/Pdf.php, line 251

Class

Pdf
The plugin that handles a feed, such as RSS or atom.

Namespace

Drupal\pdf_generator\Plugin\views\display

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {

  // It is very important to call the parent function here.
  parent::buildOptionsForm($form, $form_state);
  switch ($form_state
    ->get('section')) {
    case 'title':
      $title = $form['title'];

      // A little juggling to move the 'title' field beyond our checkbox.
      unset($form['title']);
      $form['sitename_title'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Use the site name for the title'),
        '#default_value' => $this
          ->getOption('sitename_title'),
      ];
      $labels = [
        '' => $this
          ->t('- None -'),
      ];
      $fieldsLabels = $this->view->display_handler
        ->getFieldLabels();
      $fieldsLabels = array_merge($labels, $fieldsLabels);
      $form['field_title'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Use field value as title'),
        '#default_value' => $this
          ->getOption('field_title'),
      ];
      $form['select_field_title'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Select field for the title.'),
        '#options' => $fieldsLabels,
        '#default_value' => $this
          ->getOption('select_field_title'),
      ];
      $form['select_field_title']['#states'] = [
        'visible' => [
          ':input[name="field_title"]' => [
            'checked' => TRUE,
          ],
        ],
      ];
      $form['title'] = $title;
      $form['title']['#states'] = [
        'visible' => [
          ':input[name="sitename_title"]' => [
            'checked' => FALSE,
          ],
          ':input[name="field_title"]' => [
            'checked' => FALSE,
          ],
        ],
      ];
      $form['field_title']['#states'] = [
        'visible' => [
          ':input[name="sitename_title"]' => [
            'checked' => FALSE,
          ],
        ],
      ];
      $form['sitename_title']['#states'] = [
        'visible' => [
          ':input[name="field_title"]' => [
            'checked' => FALSE,
          ],
        ],
      ];
      break;
    case 'displays':
      $form['#title'] .= $this
        ->t('Attach to');
      $displays = [];
      foreach ($this->view->storage
        ->get('display') as $display_id => $display) {

        // @todo The display plugin should have display_title and id as well.
        if ($this->view->displayHandlers
          ->has($display_id) && $this->view->displayHandlers
          ->get($display_id)
          ->acceptAttachments()) {
          $displays[$display_id] = $display['display_title'];
        }
      }
      $form['displays'] = [
        '#title' => $this
          ->t('Displays'),
        '#type' => 'checkboxes',
        '#description' => $this
          ->t('The feed icon will be available only to the selected displays.'),
        '#options' => array_map('\\Drupal\\Component\\Utility\\Html::escape', $displays),
        '#default_value' => $this
          ->getOption('displays'),
      ];
      break;
    case 'inline_css':
      $form['inline_css'] = [
        '#title' => $this
          ->t('Inline CSS'),
        '#type' => 'textarea',
        '#description' => $this
          ->t('These styles are attached to the pdf.'),
        '#default_value' => $this
          ->getOption('inline_css'),
      ];
      break;
    case 'file_css':
      $form['file_css'] = [
        '#title' => $this
          ->t('File CSS'),
        '#type' => 'textfield',
        '#description' => $this
          ->t('The file will be read and attached to the pdf.'),
        '#default_value' => $this
          ->getOption('file_css'),
      ];
      break;
    case 'paper_disposition':
      $form['paper_disposition'] = [
        '#title' => $this
          ->t('Disposition'),
        '#type' => 'select',
        '#options' => $this->pdfGenerator
          ->availableDisposition(),
        '#required' => TRUE,
        '#description' => $this
          ->t('The disposition of each page of the PDF.'),
        '#default_value' => $this
          ->getOption('paper_disposition'),
      ];
      break;
    case 'paper_size':
      $form['paper_size'] = [
        '#title' => $this
          ->t('Paper size'),
        '#type' => 'select',
        '#options' => $this->pdfGenerator
          ->pageSizes(),
        '#required' => TRUE,
        '#description' => $this
          ->t('The disposition of each page of the PDF.'),
        '#default_value' => !empty($this
          ->getOption('paper_size')) ? $this
          ->getOption('paper_size') : 'a4',
      ];
      break;
    case 'path':
      $form['path']['#description'] = $this
        ->t('This view will be displayed by visiting this path on your site. It is recommended that the path be something like "path/%/%/feed" or "path/%/%/rss.xml", putting one % in the path for each contextual filter you have defined in the view.');
  }
}