You are here

class SettingsForm in Juicebox HTML5 Responsive Image Galleries 8.3

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

Defines a form that configures global Juicebox settings.

Hierarchy

Expanded class hierarchy of SettingsForm

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

File

src/Form/SettingsForm.php, line 15

Namespace

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

  /**
   * The Juicebox formatter service.
   *
   * @var Drupal\juicebox\JuiceboxFormatter\JuiceboxFormatterInterface
   */
  protected $juiceboxFormatter;

  /**
   * Constructs a new SettingsForm object.
   *
   * @param \Drupal\juicebox\JuiceboxFormatter $juicebox_formatter
   *   The Juicebox formatter service.
   */
  public function __construct(JuiceboxFormatter $juicebox_formatter) {
    $this->juiceboxFormatter = $juicebox_formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {

    // Instantiates this form class.
    return new static($container
      ->get('juicebox.formatter'));
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $library = $this->juiceboxFormatter
      ->getLibrary(TRUE, TRUE);
    $version = !empty($library['version']) ? $library['version'] : $this
      ->t('Unknown');

    // Get all settings.
    $settings = $this
      ->config('juicebox.settings')
      ->get();

    // If the base language list is not officially saved yet, we can get the
    // default value from the library settings.
    if (empty($settings['base_languagelist'])) {
      $settings['base_languagelist'] = $library['base_languagelist'];
    }
    $form['juicebox_admin_intro'] = [
      '#markup' => $this
        ->t("The options below apply to all Juicebox galleries. Note that most Juicebox configuration options are set within each gallery's unique configuration form and not applied on a global scope like the values here."),
    ];
    $form['apply_markup_filter'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Filter all title and caption output for compatibility with Juicebox javascript (recommended)'),
      '#default_value' => $settings['apply_markup_filter'],
      '#description' => $this
        ->t('This option helps ensure title/caption output is syntactically compatible with the Juicebox javascript library by removing block-level tags.'),
    ];
    $form['enable_cors'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Allow galleries to be embedded remotely (CORS support)'),
      '#default_value' => $settings['enable_cors'],
      '#description' => $this
        ->t('Enable cross-origin resource sharing (CORS) for all generated Juicebox XML. This will allow all origins/domains to use any Juicebox XML requested from this site for embedding purposes (adds a <em>Access-Control-Allow-Origin: *</em> header to all Juicebox XML responses).'),
    ];
    $form['multilingual'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Multilingual options'),
      '#open' => TRUE,
    ];
    $form['multilingual']['translate_interface'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Translate the Juicebox javascript interface'),
      '#default_value' => $settings['translate_interface'],
      '#description' => $this
        ->t('Send interface strings to the Juicebox javascript after passing them through the Drupal translation system.'),
    ];
    $form['multilingual']['base_languagelist'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Base string for interface translation'),
      '#default_value' => $settings['base_languagelist'],
      '#description' => $this
        ->t('The base <strong>English</strong> interface text that Drupal should attempt to translate and pass to the Juicebox javascript for display (using the "languageList" configuration option). This text will be treated as a <strong>single string</strong> by Drupal and must be translated with a tool such as the Locale module. Note that this base string value will rarely change, and any changes made to it will break existing translations.'),
      '#wysiwyg' => FALSE,
      '#states' => [
        // Hide the settings when the translate option is disabled.
        'invisible' => [
          ':input[name="translate_interface"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['multilingual']['base_languagelist_suggestion'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Suggested base string for currently detected Juicebox version (@version)', [
        '@version' => $version,
      ]),
      '#description' => new FormattableMarkup('<pre>' . $library['base_languagelist'] . '</pre>', []),
      '#states' => [
        // Hide the settings when the translate option is disabled.
        'invisible' => [
          ':input[name="translate_interface"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $multisize_disallowed = in_array('juicebox_multisize_image_style', $library['disallowed_conf']);
    $multisize_description = '<p>' . $this
      ->t('Some versions of the Juicebox javascript library support "multi-size" (adaptive) image delivery. Individual galleries configured to use the <strong>Juicebox PRO multi-size (adaptive)</strong> image style allow for three different source derivatives to be defined per image, each of which can be configured below. The Juicebox javascript library will then choose between these depending on the active screen mode (i.e. viewport size) of each user. See the Juicebox javascript library documentation for more information.') . '</p>';
    if ($multisize_disallowed) {
      $multisize_description .= '<p><strong>' . $this
        ->t('Your currently detected Juicebox version (@version) is not compatible with multi-size features, so the options below have been disabled.', [
        '@version' => $version,
      ]) . '</strong></p>';
    }

    // Mark our description, and its markup, as safe for unescaped display.
    $multisize_description = new FormattableMarkup($multisize_description, []);
    $form['juicebox_multisize'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Juicebox PRO multi-size style configuration'),
      '#description' => $multisize_description,
      '#open' => !$multisize_disallowed,
    ];

    // Get available image style presets.
    $presets = image_style_options(FALSE);
    $form['juicebox_multisize']['juicebox_multisize_small'] = [
      '#title' => $this
        ->t('Small mode image style'),
      '#default_value' => $settings['juicebox_multisize_small'],
      '#description' => $this
        ->t('The style formatter to use in small screen mode (e.g., non-retina mobile devices).'),
    ];
    $form['juicebox_multisize']['juicebox_multisize_medium'] = [
      '#title' => $this
        ->t('Medium mode image style'),
      '#default_value' => $settings['juicebox_multisize_medium'],
      '#description' => $this
        ->t('The style formatter to use in medium screen mode (e.g., desktops and retina mobile devices).'),
    ];
    $form['juicebox_multisize']['juicebox_multisize_large'] = [
      '#title' => $this
        ->t('Large mode image style'),
      '#default_value' => $settings['juicebox_multisize_large'],
      '#description' => $this
        ->t('The style formatter to use in large screen mode (e.g., expanded view and retina laptops).'),
    ];
    foreach ($form['juicebox_multisize'] as &$options) {
      if (is_array($options)) {
        $options += [
          '#type' => 'select',
          '#options' => $presets,
          '#empty_option' => $this
            ->t('None (original image)'),
          '#disabled' => $multisize_disallowed,
        ];
      }
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $library = $this->juiceboxFormatter
      ->getLibrary(TRUE, TRUE);
    if ($form_state
      ->getvalue('translate_interface') && !empty($library['installed']) && $form_state
      ->getvalue('base_languagelist') != $library['base_languagelist']) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('Interface translations are enabled but the base translation string does not match the suggested value for your version of the Juicebox javascript library. If some parts of the Juicebox interface do not appear translated correctly please verify that your base translation string is correct.'));
    }
    $this
      ->config('juicebox.settings')
      ->set('apply_markup_filter', $form_state
      ->getvalue('apply_markup_filter'))
      ->set('enable_cors', $form_state
      ->getvalue('enable_cors'))
      ->set('translate_interface', $form_state
      ->getvalue('translate_interface'))
      ->set('base_languagelist', $form_state
      ->getvalue('base_languagelist'))
      ->set('juicebox_multisize_small', $form_state
      ->getvalue('juicebox_multisize_small'))
      ->set('juicebox_multisize_medium', $form_state
      ->getvalue('juicebox_multisize_medium'))
      ->set('juicebox_multisize_large', $form_state
      ->getvalue('juicebox_multisize_large'))
      ->save();

    // These settings are global and may affect any gallery embed or XML code,
    // so we need to clear everything tagged with juicebox_gallery cache tag.
    Cache::invalidateTags([
      'juicebox_gallery',
    ]);
    $this
      ->messenger()
      ->addMessage($this
      ->t('The Juicebox configuration options have been saved'));
  }

}

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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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::$juiceboxFormatter protected property The Juicebox formatter service.
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::__construct public function Constructs a new SettingsForm object. 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.