You are here

class LayouterTextFormat in Layouter - WYSIWYG layout templates 8

Provides a trusted callback.

Hierarchy

Expanded class hierarchy of LayouterTextFormat

See also

layouter_element_info_alter()

1 file declares its use of LayouterTextFormat
layouter.module in ./layouter.module
Contains hooks implementations, prerender callback and common functions.

File

src/LayouterTextFormat.php, line 14

Namespace

Drupal\layouter
View source
class LayouterTextFormat implements RenderCallbackInterface {

  /**
   * Sets layouter template on field.
   */
  public static function preRender($element) {
    if (isset($element['value'])) {
      $element['value'] = self::layouterLoadByField($element['value']);
    }
    else {
      $element = self::layouterLoadByField($element);
    }
    return $element;
  }

  /**
   * Enables Layouter for given textarea field.
   *
   * @param array $field
   *   Array to process.
   *
   * @return mixed
   *   Array of allowed formats.
   */
  private static function layouterLoadByField(array $field) {
    static $processed_ids = [];
    $processed = !isset($field['#id']) || isset($processed_ids[$field['#id']]) || $field['#id'] == 'edit-log';
    $not_accessible = isset($field['#access']) && !$field['#access'];
    $disabled = isset($field['#attributes']['disabled']) && $field['#attributes']['disabled'] == 'disabled';
    if ($processed || $not_accessible || $disabled) {
      return $field;
    }
    $active_text_formats = self::layouterActiveTextFormats();
    $js_settings['window']['active_text_formats'] = $active_text_formats;
    $js_settings['window']['textareas_id'][$field['#id']] = $field['#id'];
    $field['#attached']['drupalSettings']['layouter'] = $js_settings;
    $field['#attached']['library'][] = 'layouter/form';
    $field['#attached']['library'][] = 'core/drupal.dialog.ajax';
    if (!isset($processed_ids[$field['#id']])) {
      $processed_ids[$field['#id']] = [];
    }
    $textarea_id = $field['#id'];
    $class = 'layouter';
    $link = [
      '#type' => 'link',
      '#url' => Url::fromRoute('layouter.form', [
        'textarea_id' => $textarea_id,
      ]),
      '#title' => t('Select the text template'),
      '#attributes' => [
        'id' => [
          'layouter-' . $textarea_id,
        ],
        'class' => [
          'use-ajax',
          'layouter-link',
          $textarea_id,
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => '75%',
          'title' => t('Layouter'),
          'dialogClass' => 'no-close',
        ]),
        'title' => t('Click to select the text template with a simplified form of layout'),
      ],
    ];
    $link = render($link);
    $suffix = '<div class="filter-wrapper layouter-link-wrapper">' . $link . '</div>';

    // Remember extra information and reuse it during "Preview".
    $processed_ids[$field['#id']]['suffix'] = $suffix;
    $processed_ids[$field['#id']]['class'][] = $class;
    $field['#suffix'] = empty($field['#suffix']) ? $suffix : $field['#suffix'] . $suffix;
    $field['#attributes']['class'][] = $class;
    return $field;
  }

  /**
   * Gets the list of text formats for which Layouter is enabled.
   *
   * @return array
   *   Array of allowed formats.
   */
  private static function layouterActiveTextFormats() {
    $layouter_text_formats = \Drupal::config('layouter.settings')
      ->get('text_formats');
    $text_formats_enabled = [];
    if ($layouter_text_formats) {
      foreach ($layouter_text_formats as $text_format) {
        if ($text_format) {
          $text_formats_enabled[] = $text_format;
        }
      }
    }
    return $text_formats_enabled;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LayouterTextFormat::layouterActiveTextFormats private static function Gets the list of text formats for which Layouter is enabled.
LayouterTextFormat::layouterLoadByField private static function Enables Layouter for given textarea field.
LayouterTextFormat::preRender public static function Sets layouter template on field.