You are here

function gutenberg_page_attachments in Gutenberg 8.2

Same name and namespace in other branches
  1. 8 gutenberg.module \gutenberg_page_attachments()

Implements hook_page_attachments().

File

./gutenberg.module, line 815
Provides integration with the Gutenberg editor.

Code

function gutenberg_page_attachments(array &$page) {

  /*
   * FIXME: Recommending removing the generated CSS functionality since
   *  Gutenberg core doesn't support it, and causes the styles to be embedded
   *  on all pages, even those without Gutenberg on it.
   *  @see https://developer.wordpress.org/block-editor/developers/themes/theme-support/#block-color-palettes
   *  If the functionality is truly needed, it could be added as a third
   *  party/custom module.
   */

  /** @var \Drupal\gutenberg\GutenbergLibraryManagerInterface $gutenberg_library_manager */
  $gutenberg_library_manager = \Drupal::service('plugin.manager.gutenberg.library');
  $theme_definition = $gutenberg_library_manager
    ->getActiveThemeMergedDefinition();
  if (empty($theme_definition['theme-support']['colors']) || !empty($theme_definition['theme-includes-colors'])) {

    // Color not available or the styles are already declared in the theme.
    return;
  }
  $css_markup = '';

  // Generate minified palette styles.
  foreach ($theme_definition['theme-support']['colors'] as $color) {
    $color_name = Html::getClass($color['name']);
    $color_value = $color['color'];
    $css_markup .= ":root .has-{$color_name}-color{color:{$color_value}}";
    $css_markup .= ":root .has-{$color_name}-background-color{background-color:{$color_value}}";
  }
  $page['#attached']['html_head'][] = [
    [
      '#tag' => 'style',
      '#attributes' => [
        'id' => Html::getUniqueId('gutenberg-palette'),
      ],
      '#value' => $css_markup,
    ],
    'gutenberg_palette',
  ];
}