You are here

public function SettingsForm::buildForm in Image Effects 8

Same name and namespace in other branches
  1. 8.3 src/Form/SettingsForm.php \Drupal\image_effects\Form\SettingsForm::buildForm()
  2. 8.2 src/Form/SettingsForm.php \Drupal\image_effects\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 92

Class

SettingsForm
Main image_effects settings admin form.

Namespace

Drupal\image_effects\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('image_effects.settings');
  $ajaxing = (bool) $form_state
    ->getValues();

  // Color selector plugin.
  $color_plugin_id = $ajaxing ? $form_state
    ->getValue([
    'settings',
    'color_selector',
    'plugin_id',
  ]) : $config
    ->get('color_selector.plugin_id');
  $color_plugin = $this->colorManager
    ->getPlugin($color_plugin_id);
  if ($ajaxing && $form_state
    ->hasValue([
    'settings',
    'color_selector',
    'plugin_settings',
  ])) {
    $color_plugin
      ->setConfiguration($form_state
      ->getValue([
      'settings',
      'color_selector',
      'plugin_settings',
    ]));
  }

  // Image selector plugin.
  $image_plugin_id = $ajaxing ? $form_state
    ->getValue([
    'settings',
    'image_selector',
    'plugin_id',
  ]) : $config
    ->get('image_selector.plugin_id');
  $image_plugin = $this->imageManager
    ->getPlugin($image_plugin_id);
  if ($ajaxing && $form_state
    ->hasValue([
    'settings',
    'image_selector',
    'plugin_settings',
  ])) {
    $image_plugin
      ->setConfiguration($form_state
      ->getValue([
      'settings',
      'image_selector',
      'plugin_settings',
    ]));
  }

  // Font selector plugin.
  $font_plugin_id = $ajaxing ? $form_state
    ->getValue([
    'settings',
    'font_selector',
    'plugin_id',
  ]) : $config
    ->get('font_selector.plugin_id');
  $font_plugin = $this->fontManager
    ->getPlugin($font_plugin_id);
  if ($ajaxing && $form_state
    ->hasValue([
    'settings',
    'font_selector',
    'plugin_settings',
  ])) {
    $font_plugin
      ->setConfiguration($form_state
      ->getValue([
      'settings',
      'font_selector',
      'plugin_settings',
    ]));
  }

  // AJAX messages.
  $form['ajax_messages'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'image-effects-ajax-messages',
    ],
  ];

  // AJAX settings.
  $ajax_settings = [
    'callback' => [
      $this,
      'processAjax',
    ],
  ];

  // Main part of settings form.
  $form['settings'] = [
    '#type' => 'container',
    '#tree' => TRUE,
    '#attributes' => [
      'id' => 'image-effects-settings-main',
    ],
  ];

  // Color selector.
  $form['settings']['color_selector'] = [
    '#type' => 'details',
    '#open' => TRUE,
    '#title' => $this
      ->t('Color selector'),
    '#tree' => TRUE,
  ];
  $form['settings']['color_selector']['plugin_id'] = [
    '#type' => 'radios',
    '#options' => $this->colorManager
      ->getPluginOptions(),
    '#default_value' => $color_plugin
      ->getPluginId(),
    '#required' => TRUE,
    '#ajax' => $ajax_settings,
  ];
  $form['settings']['color_selector']['plugin_settings'] = $color_plugin
    ->buildConfigurationForm([], $form_state, $ajax_settings);

  // Image selector.
  $form['settings']['image_selector'] = [
    '#type' => 'details',
    '#open' => TRUE,
    '#title' => $this
      ->t('Image selector'),
    '#tree' => TRUE,
  ];
  $form['settings']['image_selector']['plugin_id'] = [
    '#type' => 'radios',
    '#options' => $this->imageManager
      ->getPluginOptions(),
    '#default_value' => $image_plugin
      ->getPluginId(),
    '#required' => TRUE,
    '#ajax' => $ajax_settings,
  ];
  $form['settings']['image_selector']['plugin_settings'] = $image_plugin
    ->buildConfigurationForm([], $form_state, $ajax_settings);

  // Font selector.
  $form['settings']['font_selector'] = [
    '#type' => 'details',
    '#open' => TRUE,
    '#title' => $this
      ->t('Font selector'),
    '#tree' => TRUE,
  ];
  $form['settings']['font_selector']['plugin_id'] = [
    '#type' => 'radios',
    '#options' => $this->fontManager
      ->getPluginOptions(),
    '#default_value' => $font_plugin
      ->getPluginId(),
    '#required' => TRUE,
    '#ajax' => $ajax_settings,
  ];
  $form['settings']['font_selector']['plugin_settings'] = $font_plugin
    ->buildConfigurationForm([], $form_state, $ajax_settings);
  return parent::buildForm($form, $form_state);
}