You are here

class SettingsForm in Image Effects 8

Same name and namespace in other branches
  1. 8.3 src/Form/SettingsForm.php \Drupal\image_effects\Form\SettingsForm
  2. 8.2 src/Form/SettingsForm.php \Drupal\image_effects\Form\SettingsForm

Main image_effects settings admin form.

@todo the array syntax for $form_state->getValue([...]) fails code style checking, but this is quite inconvenient. See if sniff gets adjusted or a different way to access nested keys will be available.

Hierarchy

Expanded class hierarchy of SettingsForm

1 string reference to 'SettingsForm'
image_effects.routing.yml in ./image_effects.routing.yml
image_effects.routing.yml

File

src/Form/SettingsForm.php, line 21

Namespace

Drupal\image_effects\Form
View source
class SettingsForm extends ConfigFormBase {

  /**
   * The color selector plugin manager.
   *
   * @var \Drupal\image_effects\Plugin\ImageEffectsPluginManager
   */
  protected $colorManager;

  /**
   * The image selector plugin manager.
   *
   * @var \Drupal\image_effects\Plugin\ImageEffectsPluginManager
   */
  protected $imageManager;

  /**
   * The font selector plugin manager.
   *
   * @var \Drupal\image_effects\Plugin\ImageEffectsPluginManager
   */
  protected $fontManager;

  /**
   * Constructs the class for image_effects settings form.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\image_effects\Plugin\ImageEffectsPluginManager $color_plugin_manager
   *   The color selector plugin manager.
   * @param \Drupal\image_effects\Plugin\ImageEffectsPluginManager $image_plugin_manager
   *   The image selector plugin manager.
   * @param \Drupal\image_effects\Plugin\ImageEffectsPluginManager $font_plugin_manager
   *   The font selector plugin manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ImageEffectsPluginManager $color_plugin_manager, ImageEffectsPluginManager $image_plugin_manager, ImageEffectsPluginManager $font_plugin_manager) {
    parent::__construct($config_factory);
    $this->colorManager = $color_plugin_manager;
    $this->imageManager = $image_plugin_manager;
    $this->fontManager = $font_plugin_manager;
  }

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

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

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

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

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('image_effects.settings');

    // Color plugin.
    $color_plugin = $this->colorManager
      ->getPlugin($form_state
      ->getValue([
      'settings',
      'color_selector',
      'plugin_id',
    ]));
    if ($form_state
      ->hasValue([
      'settings',
      'color_selector',
      'plugin_settings',
    ])) {
      $color_plugin
        ->setConfiguration($form_state
        ->getValue([
        'settings',
        'color_selector',
        'plugin_settings',
      ]));
    }
    $config
      ->set('color_selector.plugin_id', $color_plugin
      ->getPluginId())
      ->set('color_selector.plugin_settings.' . $color_plugin
      ->getPluginId(), $color_plugin
      ->getConfiguration());

    // Image plugin.
    $image_plugin = $this->imageManager
      ->getPlugin($form_state
      ->getValue([
      'settings',
      'image_selector',
      'plugin_id',
    ]));
    if ($form_state
      ->hasValue([
      'settings',
      'image_selector',
      'plugin_settings',
    ])) {
      $image_plugin
        ->setConfiguration($form_state
        ->getValue([
        'settings',
        'image_selector',
        'plugin_settings',
      ]));
    }
    $config
      ->set('image_selector.plugin_id', $image_plugin
      ->getPluginId())
      ->set('image_selector.plugin_settings.' . $image_plugin
      ->getPluginId(), $image_plugin
      ->getConfiguration());

    // Font plugin.
    $font_plugin = $this->fontManager
      ->getPlugin($form_state
      ->getValue([
      'settings',
      'font_selector',
      'plugin_id',
    ]));
    if ($form_state
      ->hasValue([
      'settings',
      'font_selector',
      'plugin_settings',
    ])) {
      $font_plugin
        ->setConfiguration($form_state
        ->getValue([
        'settings',
        'font_selector',
        'plugin_settings',
      ]));
    }
    $config
      ->set('font_selector.plugin_id', $font_plugin
      ->getPluginId())
      ->set('font_selector.plugin_settings.' . $font_plugin
      ->getPluginId(), $font_plugin
      ->getConfiguration());
    $config
      ->save();
    parent::submitForm($form, $form_state);
  }

  /**
   * AJAX callback.
   */
  public function processAjax($form, FormStateInterface $form_state) {
    $response = new AjaxResponse();
    $status_messages = [
      '#type' => 'status_messages',
    ];
    $response
      ->addCommand(new HtmlCommand('#image-effects-ajax-messages', $status_messages));
    $response
      ->addCommand(new HtmlCommand('#image-effects-settings-main', $form['settings']));
    return $response;
  }

}

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::$configFactory protected property The config factory. 1
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.
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.
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.
SettingsForm::$colorManager protected property The color selector plugin manager.
SettingsForm::$fontManager protected property The font selector plugin manager.
SettingsForm::$imageManager protected property The image selector plugin manager.
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SettingsForm::processAjax public function AJAX callback.
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
SettingsForm::__construct public function Constructs the class for image_effects settings form. Overrides ConfigFormBase::__construct
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.