You are here

class SettingsForm in Textimage 8.3

Same name and namespace in other branches
  1. 8.4 src/Form/SettingsForm.php \Drupal\textimage\Form\SettingsForm

Main Textimage settings admin form.

Hierarchy

Expanded class hierarchy of SettingsForm

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

File

src/Form/SettingsForm.php, line 16

Namespace

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

  /**
   * The Textimage factory.
   *
   * @var \Drupal\textimage\TextimageFactory
   */
  protected $textimageFactory;

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

  /**
   * The Image factory.
   *
   * @var \Drupal\Core\Image\ImageFactory
   */
  protected $imageFactory;

  /**
   * Constructs the class for Textimage settings form.
   *
   * @param \Drupal\textimage\TextimageFactory $textimage_factory
   *   The Textimage factory.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\image_effects\Plugin\ImageEffectsPluginManager $font_plugin_manager
   *   The font selector plugin manager.
   * @param \Drupal\Core\Image\ImageFactory $image_factory
   *   The Image factory.
   */
  public function __construct(TextimageFactory $textimage_factory, ConfigFactoryInterface $config_factory, ImageEffectsPluginManager $font_plugin_manager, ImageFactory $image_factory) {
    parent::__construct($config_factory);
    $this->textimageFactory = $textimage_factory;
    $this->fontManager = $font_plugin_manager;
    $this->imageFactory = $image_factory;
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('textimage.settings');
    $form['settings'] = [
      '#type' => 'container',
      '#tree' => TRUE,
      '#attributes' => [
        'id' => 'textimage-settings-main',
      ],
    ];

    // Main part of settings form.
    $form['settings']['main'] = [
      '#type' => 'details',
      '#open' => TRUE,
      '#title' => $this
        ->t('Main settings'),
    ];

    // Default image file format/extension.
    $extensions = $this->imageFactory
      ->getSupportedExtensions();
    $options = array_combine($extensions, $extensions);
    $form['settings']['main']['default_extension'] = [
      '#type' => 'select',
      '#options' => $options,
      '#title' => $this
        ->t('Default image file extension'),
      '#default_value' => $config
        ->get('default_extension'),
      '#required' => TRUE,
      '#description' => $this
        ->t('Select the default extension of the image files produced by Textimage. This can be overridden by image style effects that specify a format conversion like e.g. <em>Convert</em>. This setting does not affect image derivatives created by the Image module.'),
    ];

    // Default font.
    $font_plugin = $this->fontManager
      ->getPlugin($this
      ->config('image_effects.settings')
      ->get('font_selector.plugin_id'));
    $form['settings']['main']['default_font_uri'] = $font_plugin
      ->selectionElement([
      '#title' => $this
        ->t('Default font'),
      '#description' => $this
        ->t('Select the default font to be used by Textimage.'),
      '#default_value' => $config
        ->get('default_font.uri'),
    ]);

    // URL generation.
    $form['settings']['url_generation'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('URL generation'),
      '#open' => TRUE,
    ];
    $form['settings']['url_generation']['enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enabled'),
      '#description' => $this
        ->t("When selected, direct generation of Textimage images is enabled for users having the 'Generate Textimage URL derivatives' permission."),
      '#default_value' => $config
        ->get('url_generation.enabled'),
    ];
    $form['settings']['url_generation']['text_separator'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Text separator'),
      '#maxlength' => 5,
      '#required' => TRUE,
      '#description' => $this
        ->t("Indicate the sequence of characters to be used to split the URL text string in separate strings. Each string will be consumed by a 'Text overlay' effect in the sequence specified within the image style. Note that slashes '/' and plus '+' characters are not allowed."),
      '#default_value' => $config
        ->get('url_generation.text_separator'),
    ];

    // Maintenance.
    $form['settings']['maintenance'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Maintenance'),
    ];
    $form['settings']['maintenance']['debug'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Display debugging information'),
      '#default_value' => $config
        ->get('debug'),
      '#description' => $this
        ->t("Logs Textimage debug messages and shows them to users with the '%permission' permissions.", [
        '%permission' => implode(', ', [
          $this
            ->t('Administer site configuration'),
          $this
            ->t('Administer image styles'),
        ]),
      ]),
    ];
    $form['settings']['maintenance']['flush_all_label'] = [
      '#markup' => $this
        ->t('Remove all image files generated via Textimage, flush all the image styles, and clear the Textimage cache.') . '<br/>',
    ];
    $form['settings']['maintenance']['flush_all'] = [
      '#type' => 'submit',
      '#name' => 'flush_all',
      '#value' => $this
        ->t('Cleanup Textimage'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if (preg_match('/[+\\/]/', $form_state
      ->getValue([
      'settings',
      'url_generation',
      'text_separator',
    ]))) {
      $form_state
        ->setErrorByName('settings][url_generation][text_separator', $this
        ->t('Invalid characters specified for the text separator.'));
    }
  }

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

    // Redirect to cleanup if required.
    if ($form_state
      ->getTriggeringElement()['#name'] == 'flush_all') {
      $form_state
        ->setRedirect('textimage.flush_all');
      return;
    }

    // Main settings.
    $font_plugin = $this->fontManager
      ->getPlugin($this
      ->config('image_effects.settings')
      ->get('font_selector.plugin_id'));
    $config
      ->set('default_extension', $form_state
      ->getValue([
      'settings',
      'main',
      'default_extension',
    ]))
      ->set('default_font.name', $font_plugin
      ->getDescription($form_state
      ->getValue([
      'settings',
      'main',
      'default_font_uri',
    ])))
      ->set('default_font.uri', $form_state
      ->getValue([
      'settings',
      'main',
      'default_font_uri',
    ]));

    // URL generation.
    $config
      ->set('url_generation.enabled', $form_state
      ->getValue([
      'settings',
      'url_generation',
      'enabled',
    ]))
      ->set('url_generation.text_separator', $form_state
      ->getValue([
      'settings',
      'url_generation',
      'text_separator',
    ]));

    // Maintenance.
    $config
      ->set('debug', $form_state
      ->getValue([
      'settings',
      'maintenance',
      'debug',
    ]));
    $config
      ->save();
    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::$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::$fontManager protected property The font selector plugin manager.
SettingsForm::$imageFactory protected property The Image factory.
SettingsForm::$textimageFactory protected property The Textimage factory.
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::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 Textimage 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.