You are here

public function SettingsForm::buildForm in Entity Print 8

Same name and namespace in other branches
  1. 8.2 src/Form/SettingsForm.php \Drupal\entity_print\Form\SettingsForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/SettingsForm.php, line 78

Class

SettingsForm
Defines a form that configures Entity Print settings.

Namespace

Drupal\entity_print\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
  $disabled_engines = [];
  $pdf_engines = [];
  foreach ($this->pluginManager
    ->getDefinitions() as $plugin_id => $definition) {

    /** @var \Drupal\entity_print\Plugin\PdfEngineInterface $class */
    $class = $definition['class'];
    if ($class::dependenciesAvailable()) {
      $pdf_engines[$plugin_id] = $definition['label'];
    }
    else {
      $disabled_engines[$plugin_id] = $definition['label'];

      // Show the user which PDF engines are disabled, but only for the page load
      // not on AJAX requests.
      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',
  ];

  // If we have a pdf_engine in the form_state then use that otherwise, fall
  // back to what was saved as this is a fresh form. Check explicitly for NULL
  // in case they selected the None option which is false'y.
  $plugin_id = !is_null($form_state
    ->getValue('pdf_engine')) ? $form_state
    ->getValue('pdf_engine') : $config
    ->get('pdf_engine');

  // If we have a plugin id and the plugin hasn't since been disabled then we
  // load the config for the plugin.
  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);
}