View source
<?php
namespace Drupal\printable\Form;
use Drupal\printable\PrintableEntityManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Block\BlockManager;
class PrintableConfigurationForm extends ConfigFormBase {
protected $printableEntityManager;
protected $configFactory;
protected $blockManager;
public function __construct(PrintableEntityManagerInterface $printable_entity_manager, ConfigFactory $configFactory, BlockManager $blockManager) {
$this->printableEntityManager = $printable_entity_manager;
$this->configFactory = $configFactory;
$this->blockManager = $blockManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('printable.entity_manager'), $container
->get('config.factory'), $container
->get('plugin.manager.block'));
}
public function getFormId() {
return 'printable_configuration';
}
protected function getEditableConfigNames() {
return [
'printable.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state, $printable_format = NULL) {
$form['settings']['printable_entities'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Printable Enabled Entities'),
'#description' => $this
->t('Select the entities that printable support should be enabled for.'),
'#options' => [],
'#default_value' => [],
];
foreach ($this->printableEntityManager
->getCompatibleEntities() as $entity_type => $entity_definition) {
$form['settings']['printable_entities']['#options'][$entity_type] = $entity_definition
->getLabel();
}
foreach ($this->printableEntityManager
->getPrintableEntities() as $entity_type => $entity_definition) {
$form['settings']['printable_entities']['#default_value'][] = $entity_type;
}
$form['settings']['open_target_blank'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Open in New Tab'),
'#description' => $this
->t('Open the printable version in a new tab/window.'),
'#default_value' => $this
->config('printable.settings')
->get('open_target_blank'),
];
$form['settings']['css_include'] = [
'#type' => 'textfield',
'#title' => $this
->t('CSS Include'),
'#description' => $this
->t('Specify an additional CSS file to include. Relative to the root of the Drupal install. The token <em>[theme:theme_machine_name]</em> is available.'),
'#default_value' => $this
->config('printable.settings')
->get('css_include'),
];
$form['settings']['extract_links'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Extract Links'),
'#description' => $this
->t('Extract any links in the content, e.g. "Some Link (http://drupal.org)'),
'#default_value' => $this
->config('printable.settings')
->get('extract_links'),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->configFactory
->getEditable('printable.settings')
->set('printable_entities', $form_state
->getValue('printable_entities'))
->set('open_target_blank', $form_state
->getValue('open_target_blank'))
->set('css_include', $form_state
->getValue('css_include'))
->set('extract_links', $form_state
->getValue('extract_links'))
->save();
$this->blockManager
->clearCachedDefinitions();
parent::submitForm($form, $form_state);
}
}