View source
<?php
namespace Drupal\entity_print_views\Plugin\views\field;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\entity_print\Plugin\ExportTypeManagerInterface;
use Drupal\views\Plugin\views\field\LinkBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PrintLink extends LinkBase {
protected $exportTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccessManagerInterface $access_manager, ExportTypeManagerInterface $exportTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $access_manager);
$this->exportTypeManager = $exportTypeManager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('access_manager'), $container
->get('plugin.manager.entity_print.export_type'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['open_new_window'] = [
'default' => FALSE,
];
$options['export_type'] = [
'default' => 'pdf',
];
return $options;
}
protected function getUrlInfo(ResultRow $row) {
$entity = $this
->getEntity($row);
if (!empty($this->options['open_new_window'])) {
$this->options['alter']['target'] = '_blank';
}
return Url::fromRoute('entity_print.view', [
'entity_type' => $entity
->getEntityTypeId(),
'entity_id' => $entity
->id(),
'export_type' => $this->options['export_type'],
]);
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['export_type'] = [
'#type' => 'select',
'#title' => $this
->t('Export Type'),
'#required' => TRUE,
'#options' => $this->exportTypeManager
->getFormOptions(),
'#default_value' => $this->options['export_type'],
];
$form['open_new_window'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Open in new window'),
'#default_value' => $this->options['open_new_window'],
];
}
protected function getDefaultLabel() {
return $this
->t('View PDF');
}
}