You are here

class PrintableConfigurationForm in Printer and PDF versions for Drupal 8+ 8

Same name and namespace in other branches
  1. 2.x src/Form/PrintableConfigurationForm.php \Drupal\printable\Form\PrintableConfigurationForm

Provides shared configuration form for all printable formats.

Hierarchy

Expanded class hierarchy of PrintableConfigurationForm

1 string reference to 'PrintableConfigurationForm'
printable.routing.yml in ./printable.routing.yml
printable.routing.yml

File

src/Form/PrintableConfigurationForm.php, line 15

Namespace

Drupal\printable\Form
View source
class PrintableConfigurationForm extends ConfigFormBase {

  /**
   * The printable entity manager.
   *
   * @var \Drupal\printable\PrintableEntityManagerInterface
   */
  protected $printableEntityManager;

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * The block manager service.
   *
   * @var \Drupal\Core\Block\BlockManager
   */
  protected $blockManager;

  /**
   * Constructs a new form object.
   *
   * @param \Drupal\printable\PrintableEntityManagerInterface $printable_entity_manager
   *   The printable entity manager.
   * @param \Drupal\Core\Config\ConfigFactory $configFactory
   *   Defines the configuration object factory.
   * @param \Drupal\Core\Block\BlockManager $blockManager
   *   Manages discovery and instantiation of block plugins.
   */
  public function __construct(PrintableEntityManagerInterface $printable_entity_manager, ConfigFactory $configFactory, BlockManager $blockManager) {
    $this->printableEntityManager = $printable_entity_manager;
    $this->configFactory = $configFactory;
    $this->blockManager = $blockManager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('printable.entity_manager'), $container
      ->get('config.factory'), $container
      ->get('plugin.manager.block'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'printable_configuration';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'printable.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $printable_format = NULL) {

    // Allow users to choose what entities printable is enabled for.
    $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' => [],
    ];

    // Build the options array.
    foreach ($this->printableEntityManager
      ->getCompatibleEntities() as $entity_type => $entity_definition) {
      $form['settings']['printable_entities']['#options'][$entity_type] = $entity_definition
        ->getLabel();
    }

    // Build the default values array.
    foreach ($this->printableEntityManager
      ->getPrintableEntities() as $entity_type => $entity_definition) {
      $form['settings']['printable_entities']['#default_value'][] = $entity_type;
    }

    // Provide option to open printable page in a new tab/window.
    $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'),
    ];

    // Allow users to include CSS from the current theme.
    $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'),
    ];

    // Provide option to turn off link extraction.
    $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);
  }

  /**
   * {@inheritdoc}
   */
  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();

    // Invalidate the block cache to update custom block-based derivatives.
    // @todo try to make configsaveevent later.
    $this->blockManager
      ->clearCachedDefinitions();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PrintableConfigurationForm::$blockManager protected property The block manager service.
PrintableConfigurationForm::$configFactory protected property The config factory service. Overrides FormBase::$configFactory
PrintableConfigurationForm::$printableEntityManager protected property The printable entity manager.
PrintableConfigurationForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
PrintableConfigurationForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
PrintableConfigurationForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
PrintableConfigurationForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
PrintableConfigurationForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
PrintableConfigurationForm::__construct public function Constructs a new form object. Overrides ConfigFormBase::__construct
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.