You are here

public function EditorManager::getAttachments in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/editor/src/Plugin/EditorManager.php \Drupal\editor\Plugin\EditorManager::getAttachments()
  2. 9 core/modules/editor/src/Plugin/EditorManager.php \Drupal\editor\Plugin\EditorManager::getAttachments()

Retrieves text editor libraries and JavaScript settings.

Parameters

array $format_ids: An array of format IDs as returned by array_keys(filter_formats()).

Return value

array An array of attachments, for use with #attached.

See also

\Drupal\Core\Render\AttachmentsResponseProcessorInterface::processAttachments()

File

core/modules/editor/src/Plugin/EditorManager.php, line 61

Class

EditorManager
Configurable text editor manager.

Namespace

Drupal\editor\Plugin

Code

public function getAttachments(array $format_ids) {
  $attachments = [
    'library' => [],
  ];
  $settings = [];
  foreach ($format_ids as $format_id) {
    $editor = editor_load($format_id);
    if (!$editor) {
      continue;
    }
    $plugin = $this
      ->createInstance($editor
      ->getEditor());
    $plugin_definition = $plugin
      ->getPluginDefinition();

    // Libraries.
    $attachments['library'] = array_merge($attachments['library'], $plugin
      ->getLibraries($editor));

    // Format-specific JavaScript settings.
    $settings['editor']['formats'][$format_id] = [
      'format' => $format_id,
      'editor' => $editor
        ->getEditor(),
      'editorSettings' => $plugin
        ->getJSSettings($editor),
      'editorSupportsContentFiltering' => $plugin_definition['supports_content_filtering'],
      'isXssSafe' => $plugin_definition['is_xss_safe'],
    ];
  }

  // Allow other modules to alter all JavaScript settings.
  $this->moduleHandler
    ->alter('editor_js_settings', $settings);
  if (empty($attachments['library']) && empty($settings)) {
    return [];
  }
  $attachments['drupalSettings'] = $settings;
  return $attachments;
}