LinksConfigurationForm.php in Printer and PDF versions for Drupal 8+ 8
File
src/Form/LinksConfigurationForm.php
View source
<?php
namespace Drupal\printable\Form;
use Drupal\printable\PrintableEntityManagerInterface;
use Drupal\printable\PrintableFormatPluginManager;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactory;
class LinksConfigurationForm extends FormBase {
protected $printableEntityManager;
protected $configFactory;
public function __construct(PrintableEntityManagerInterface $printable_entity_manager, PrintableFormatPluginManager $printable_format_manager, ConfigFactory $configFactory) {
$this->printableEntityManager = $printable_entity_manager;
$this->printableFormatManager = $printable_format_manager;
$this->configFactory = $configFactory;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('printable.entity_manager'), $container
->get('printable.format_plugin_manager'), $container
->get('config.factory'));
}
public function getFormId() {
return 'links_configuration';
}
public function buildForm(array $form, FormStateInterface $form_state, $printable_format = NULL) {
$form['settings']['print_print_link_pos'] = [
'#type' => 'checkboxes',
'#title' => 'Link location',
'#default_value' => [],
'#options' => [
'node' => $this
->t('Links area'),
'comment' => $this
->t('Comment area'),
'user' => $this
->t('User area'),
],
'#description' => $this
->t('Choose the location of the link(s) to the printer-friendly version pages. The Links area is usually below the node content, whereas the Comment area is placed near the comments. The user area is near the user name. Select the options for which you want to enable the link. If you select any option then it means that you have enabled printable support for that entity in the configuration tab.'),
];
foreach ($this
->config('printable.settings')
->get('printable_print_link_locations') as $link_location) {
$form['settings']['print_print_link_pos']['#default_value'][] = $link_location;
}
$form['settings']['submit'] = [
'#type' => 'submit',
'#value' => 'Submit',
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->configFactory
->getEditable('printable.settings')
->set('printable_print_link_locations', $form_state
->getValue('print_print_link_pos'))
->save();
drupal_set_message('The configuration option has been saved', 'status');
}
}