You are here

private static function LayouterTextFormat::layouterLoadByField in Layouter - WYSIWYG layout templates 8

Enables Layouter for given textarea field.

Parameters

array $field: Array to process.

Return value

mixed Array of allowed formats.

1 call to LayouterTextFormat::layouterLoadByField()
LayouterTextFormat::preRender in src/LayouterTextFormat.php
Sets layouter template on field.

File

src/LayouterTextFormat.php, line 38

Class

LayouterTextFormat
Provides a trusted callback.

Namespace

Drupal\layouter

Code

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;
}