You are here

class WebformDialogHelper in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Utility/WebformDialogHelper.php \Drupal\webform\Utility\WebformDialogHelper

Helper class for modal and off-canvas dialog methods.

Hierarchy

Expanded class hierarchy of WebformDialogHelper

42 files declare their use of WebformDialogHelper
TestWebformOffCanvasWidthHandler.php in tests/modules/webform_test_handler/src/Plugin/WebformHandler/TestWebformOffCanvasWidthHandler.php
TestWebformOffCanvasWidthVariant.php in tests/modules/webform_test_variant/src/Plugin/WebformVariant/TestWebformOffCanvasWidthVariant.php
webform.libraries.inc in includes/webform.libraries.inc
Webform libraries.
webform.theme.template.inc in includes/webform.theme.template.inc
Preprocessors and helper functions to make theming easier.
WebformAccessGroupForm.php in modules/webform_access/src/WebformAccessGroupForm.php

... See full list

File

src/Utility/WebformDialogHelper.php, line 10

Namespace

Drupal\webform\Utility
View source
class WebformDialogHelper {

  /**
   * Width for wide dialog. (modal: 1000px; off-canvas: 800px)
   *
   * Used by: Video only.
   *
   * @var string
   */
  const DIALOG_WIDE = 'wide';

  /**
   * Width for normal dialog. (modal: 800px; off-canvas: 600px)
   *
   * Used by: Add and edit element/handler, etc…
   *
   * @var string
   */
  const DIALOG_NORMAL = 'normal';

  /**
   * Width for narrow dialog. (modal: 700px; off-canvas: 500px)
   *
   * Used by: Duplicate and delete entity, notes, etc…
   *
   * @var string
   */
  const DIALOG_NARROW = 'narrow';

  /**
   * Prevent dialog from being displayed.
   *
   * @var string
   */
  const DIALOG_NONE = 'none';

  /**
   * Use outside-in off-canvas system tray instead of dialogs.
   *
   * @return bool
   *   TRUE if outside_in.module is enabled and system trays are not disabled.
   */
  public static function useOffCanvas() {
    return !\Drupal::config('webform.settings')
      ->get('ui.offcanvas_disabled') ? TRUE : FALSE;
  }

  /**
   * Attach libraries required by (modal) dialogs.
   *
   * @param array $build
   *   A render array.
   */
  public static function attachLibraries(array &$build) {
    $build['#attached']['library'][] = 'webform/webform.admin.dialog';
    if (static::useOffCanvas()) {
      $build['#attached']['library'][] = 'webform/webform.admin.off_canvas';
    }

    // @see \Drupal\webform\Element\WebformHtmlEditor::preRenderWebformHtmlEditor
    if (\Drupal::moduleHandler()
      ->moduleExists('imce') && \Drupal\imce\Imce::access()) {
      $build['#attached']['library'][] = 'imce/drupal.imce.ckeditor';
      $build['#attached']['drupalSettings']['webform']['html_editor']['ImceImageIcon'] = file_create_url(drupal_get_path('module', 'imce') . '/js/plugins/ckeditor/icons/imceimage.png');
    }
  }

  /**
   * Get modal dialog attributes.
   *
   * @param int|string $width
   *   Width of the modal dialog.
   * @param array $class
   *   Additional class names to be included in the dialog's attributes.
   *
   * @return array
   *   Modal dialog attributes.
   */
  public static function getModalDialogAttributes($width = self::DIALOG_NORMAL, array $class = []) {
    if (\Drupal::config('webform.settings')
      ->get('ui.dialog_disabled')) {
      return $class ? [
        'class' => $class,
      ] : [];
    }
    $dialog_widths = [
      static::DIALOG_WIDE => 1000,
      static::DIALOG_NORMAL => 800,
      static::DIALOG_NARROW => 700,
    ];
    $width = isset($dialog_widths[$width]) ? $dialog_widths[$width] : $width;
    $class[] = 'webform-ajax-link';
    return [
      'class' => $class,
      'data-dialog-type' => 'modal',
      'data-dialog-options' => Json::encode([
        'width' => $width,
        // .webform-ui-dialog is used to set the dialog's top position.
        // @see modules/sandbox/webform/css/webform.ajax.css
        'dialogClass' => 'webform-ui-dialog',
      ]),
    ];
  }

  /**
   * Get modal dialog attributes.
   *
   * @param int|string $width
   *   Width of the modal dialog.
   * @param array $class
   *   Additional class names to be included in the dialog's attributes.
   *
   * @return array
   *   Modal dialog attributes.
   */
  public static function getOffCanvasDialogAttributes($width = self::DIALOG_NORMAL, array $class = []) {
    if (\Drupal::config('webform.settings')
      ->get('ui.dialog_disabled') || $width === self::DIALOG_NONE) {
      return $class ? [
        'class' => $class,
      ] : [];
    }
    if (!static::useOffCanvas()) {
      return self::getModalDialogAttributes($width, $class);
    }
    $dialog_widths = [
      static::DIALOG_WIDE => 800,
      static::DIALOG_NORMAL => 600,
      static::DIALOG_NARROW => 550,
    ];
    $width = isset($dialog_widths[$width]) ? $dialog_widths[$width] : $width;
    $class[] = 'webform-ajax-link';
    return [
      'class' => $class,
      'data-dialog-type' => 'dialog',
      'data-dialog-renderer' => 'off_canvas',
      'data-dialog-options' => Json::encode([
        'width' => $width,
        'dialogClass' => 'ui-dialog-off-canvas webform-off-canvas',
      ]),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WebformDialogHelper::attachLibraries public static function Attach libraries required by (modal) dialogs.
WebformDialogHelper::DIALOG_NARROW constant Width for narrow dialog. (modal: 700px; off-canvas: 500px)
WebformDialogHelper::DIALOG_NONE constant Prevent dialog from being displayed.
WebformDialogHelper::DIALOG_NORMAL constant Width for normal dialog. (modal: 800px; off-canvas: 600px)
WebformDialogHelper::DIALOG_WIDE constant Width for wide dialog. (modal: 1000px; off-canvas: 800px)
WebformDialogHelper::getModalDialogAttributes public static function Get modal dialog attributes.
WebformDialogHelper::getOffCanvasDialogAttributes public static function Get modal dialog attributes.
WebformDialogHelper::useOffCanvas public static function Use outside-in off-canvas system tray instead of dialogs.