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\EntityPrintPluginManagerInterface;
use Drupal\entity_print\Plugin\ExportTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Form\FormStateInterface;
class SettingsForm extends ConfigFormBase {
protected $pluginManager;
protected $storage;
protected $exportTypeManager;
public function __construct(ConfigFactoryInterface $config_factory, EntityPrintPluginManagerInterface $plugin_manager, ExportTypeManagerInterface $export_type_manager, EntityStorageInterface $entity_storage) {
parent::__construct($config_factory);
$this->pluginManager = $plugin_manager;
$this->exportTypeManager = $export_type_manager;
$this->storage = $entity_storage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('plugin.manager.entity_print.print_engine'), $container
->get('plugin.manager.entity_print.export_type'), $container
->get('entity_type.manager')
->getStorage('print_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) {
$print_engines = [];
foreach ($this->pluginManager
->getDefinitions() as $plugin_id => $definition) {
$class = $definition['class'];
if ($class::dependenciesAvailable()) {
$print_engines[$definition['export_type']][$plugin_id] = $definition['label'];
}
}
foreach (array_keys($this->exportTypeManager
->getDefinitions()) as $export_type) {
foreach ($this->pluginManager
->getDisabledDefinitions($export_type) as $plugin_id => $definition) {
$class = $definition['class'];
if (!$request
->isXmlHttpRequest()) {
$this
->messenger()
->addWarning($this
->t('@name is not available because it is not configured. @installation.', [
'@name' => $definition['label'],
'@installation' => $class::getInstallationInstructions(),
]));
}
}
}
$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 Print with a filename from the node title.'),
'#default_value' => $config
->get('force_download'),
];
foreach ($this->exportTypeManager
->getDefinitions() as $export_type => $definition) {
$selected_plugin_id = !is_null($form_state
->getValue($export_type)) ? $form_state
->getValue($export_type) : $config
->get('print_engines.' . $export_type . '_engine');
$form['entity_print'][$export_type] = [
'#type' => 'select',
'#title' => $definition['label'],
'#description' => $this
->t('Select the default %label engine for printing.', [
'%label' => $definition['label'],
]),
'#options' => !empty($print_engines[$export_type]) ? $print_engines[$export_type] : [],
'#default_value' => $selected_plugin_id,
'#empty_option' => $this
->t('- None -'),
'#ajax' => [
'callback' => '::ajaxPluginFormCallback',
'wrapper' => $export_type . '-config',
'effect' => 'fade',
],
];
$form['entity_print'][$export_type . '_config'] = [
'#type' => 'container',
'#id' => $export_type . '-config',
];
if ($this->pluginManager
->isPrintEngineEnabled($selected_plugin_id)) {
$form['entity_print'][$export_type . '_config'][$selected_plugin_id] = $this
->getPluginForm($selected_plugin_id, $form_state);
}
}
return parent::buildForm($form, $form_state);
}
public function ajaxPluginFormCallback(&$form, FormStateInterface $form_state) {
$export_type = $form_state
->getTriggeringElement()['#name'];
return $form['entity_print'][$export_type . '_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'],
]),
'#tree' => TRUE,
];
return $form + $plugin
->buildConfigurationForm([], $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
foreach ($this->exportTypeManager
->getDefinitions() as $export_type => $definition) {
if ($plugin_id = $form_state
->getValue($export_type)) {
$entity = $this
->loadConfigEntity($plugin_id);
$plugin = $entity
->getPrintEnginePluginCollection()
->get($entity
->id());
$plugin
->validateConfigurationForm($form, $form_state);
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('entity_print.settings');
foreach ($this->exportTypeManager
->getDefinitions() as $export_type => $definition) {
if ($plugin_id = $form_state
->getValue($export_type)) {
$entity = $this
->loadConfigEntity($plugin_id);
$plugin = $entity
->getPrintEnginePluginCollection()
->get($entity
->id());
$plugin
->submitConfigurationForm($form, $form_state);
$entity
->save();
}
$config
->set('print_engines.' . $export_type . '_engine', $plugin_id);
}
$values = $form_state
->getValues();
$config
->set('default_css', $values['default_css'])
->set('force_download', $values['force_download'])
->save();
$this
->messenger()
->addStatus($this
->t('Configuration saved.'));
}
protected function loadConfigEntity($plugin_id) {
if (!($entity = $this->storage
->load($plugin_id))) {
$entity = $this->storage
->create([
'id' => $plugin_id,
]);
}
return $entity;
}
}