View source
<?php
namespace Drupal\entity_print\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\entity_print\Plugin\EntityPrintPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Form\FormStateInterface;
class SettingsForm extends ConfigFormBase {
protected $pluginManager;
protected $storage;
public function __construct(ConfigFactoryInterface $config_factory, EntityPrintPluginManager $plugin_manager, EntityStorageInterface $entity_storage) {
parent::__construct($config_factory);
$this->pluginManager = $plugin_manager;
$this->storage = $entity_storage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('plugin.manager.entity_print.pdf_engine'), $container
->get('entity_type.manager')
->getStorage('pdf_engine'));
}
public function getFormID() {
return 'entity_print_admin_settings_form';
}
protected function getEditableConfigNames() {
return [
'entity_print.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$disabled_engines = [];
$pdf_engines = [];
foreach ($this->pluginManager
->getDefinitions() as $plugin_id => $definition) {
$class = $definition['class'];
if ($class::dependenciesAvailable()) {
$pdf_engines[$plugin_id] = $definition['label'];
}
else {
$disabled_engines[$plugin_id] = $definition['label'];
if (!$request
->isXmlHttpRequest()) {
drupal_set_message($this
->t('@name is not available because it is not configured. @installation.', [
'@name' => $definition['label'],
'@installation' => $class::getInstallationInstructions(),
]), 'warning');
}
}
}
$config = $this
->config('entity_print.settings');
$form['entity_print'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Entity Print Config'),
];
$form['entity_print']['default_css'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable Default CSS'),
'#description' => $this
->t('Provides some very basic font and padding styles.'),
'#default_value' => $config
->get('default_css'),
];
$form['entity_print']['force_download'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Force Download'),
'#description' => $this
->t('This option will attempt to force the browser to download the PDF with a filename from the node title.'),
'#default_value' => $config
->get('force_download'),
];
$form['entity_print']['pdf_engine'] = [
'#type' => 'select',
'#title' => $this
->t('Pdf Engine'),
'#description' => $this
->t('Select the PDF engine to render the PDF'),
'#options' => $pdf_engines,
'#default_value' => $config
->get('pdf_engine'),
'#empty_option' => $this
->t('- None -'),
'#ajax' => [
'callback' => '::ajaxPluginFormCallback',
'wrapper' => 'pdf-engine-config',
'effect' => 'fade',
],
];
$form['entity_print']['pdf_engine_config'] = [
'#type' => 'container',
'#id' => 'pdf-engine-config',
];
$plugin_id = !is_null($form_state
->getValue('pdf_engine')) ? $form_state
->getValue('pdf_engine') : $config
->get('pdf_engine');
if ($plugin_id && !in_array($plugin_id, array_keys($disabled_engines), TRUE)) {
$form['entity_print']['pdf_engine_config'][$plugin_id] = $this
->getPluginForm($plugin_id, $form_state);
}
return parent::buildForm($form, $form_state);
}
public function ajaxPluginFormCallback(&$form, FormStateInterface $form_state) {
return $form['entity_print']['pdf_engine_config'];
}
protected function getPluginForm($plugin_id, FormStateInterface $form_state) {
$plugin = $this->pluginManager
->createInstance($plugin_id);
$form = [
'#type' => 'fieldset',
'#title' => $this
->t('@engine Settings', [
'@engine' => $plugin
->getPluginDefinition()['label'],
]),
];
return $form + $plugin
->buildConfigurationForm([], $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
if ($plugin_id = $form_state
->getValue('pdf_engine')) {
$entity = $this
->loadConfigEntity($plugin_id);
$plugin = $entity
->getPdfEnginePluginCollection()
->get($entity
->id());
$plugin
->validateConfigurationForm($form, $form_state);
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($plugin_id = $form_state
->getValue('pdf_engine')) {
$entity = $this
->loadConfigEntity($plugin_id);
$plugin = $entity
->getPdfEnginePluginCollection()
->get($entity
->id());
$plugin
->submitConfigurationForm($form, $form_state);
$entity
->save();
}
$values = $form_state
->getValues();
$this
->config('entity_print.settings')
->set('default_css', $values['default_css'])
->set('force_download', $values['force_download'])
->set('pdf_engine', $values['pdf_engine'])
->save();
}
protected function loadConfigEntity($plugin_id) {
if (!($entity = $this->storage
->load($plugin_id))) {
$entity = $this->storage
->create([
'id' => $plugin_id,
]);
}
return $entity;
}
}