You are here

public function SettingsForm::buildForm in Entity Print 8.2

Same name and namespace in other branches
  1. 8 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 90

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) {
  $print_engines = [];
  foreach ($this->pluginManager
    ->getDefinitions() as $plugin_id => $definition) {

    /** @var \Drupal\entity_print\Plugin\PrintEngineInterface $class */
    $class = $definition['class'];
    if ($class::dependenciesAvailable()) {
      $print_engines[$definition['export_type']][$plugin_id] = $definition['label'];
    }
  }

  // Show a notification for each disabled print engine.
  foreach (array_keys($this->exportTypeManager
    ->getDefinitions()) as $export_type) {
    foreach ($this->pluginManager
      ->getDisabledDefinitions($export_type) as $plugin_id => $definition) {
      $class = $definition['class'];

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

  // Global settings.
  $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) {

    // If we have a print_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.
    $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);
}