You are here

function gutenberg_theme in Gutenberg 8.2

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

Implements hook_theme().

File

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

Code

function gutenberg_theme() {
  $templates = [
    'page__node__edit__gutenberg' => [
      'template' => 'page--node--edit--gutenberg',
    ],
    'page__node__add__gutenberg' => [
      'template' => 'page--node--add--gutenberg',
    ],
    'node_edit_form__gutenberg' => [
      'template' => 'node-edit-form--gutenberg',
    ],
    // Gutenberg text field.
    'field_gutenberg_text' => [
      'render element' => 'element',
      'file' => 'gutenberg.theme.inc',
    ],
    'gutenberg_block' => [
      'variables' => [
        'block_name' => NULL,
        'block_attributes' => [],
        'block_content' => NULL,
      ],
      'file' => 'gutenberg.theme.inc',
    ],
  ];

  // Generate theme definitions for each module's dynamic block.

  /*
   * By default, Drupal 8 does not include theme suggestions from inside the
   * module in which they were created, so we must add them manually here.
   */
  $base_hook = 'gutenberg_block';

  /** @var \Drupal\gutenberg\GutenbergLibraryManagerInterface $gutenberg_library_manager */
  $gutenberg_library_manager = \Drupal::service('plugin.manager.gutenberg.library');
  foreach ($gutenberg_library_manager
    ->getModuleDefinitions() as $module => $definition) {
    if (empty($definition['dynamic-blocks'])) {

      // No dynamic blocks to declare.
      continue;
    }
    $template_hooks = drupal_find_theme_templates($templates, '.html.twig', drupal_get_path('module', $module) . '/templates');
    foreach ($definition['dynamic-blocks'] as $block_name => $block_theme_definition) {
      $block_name = str_replace('-', '_', $block_name);
      $block_parts = explode('/', $block_name);
      $hook_names = [
        $block_parts[0],
      ];
      if (count($block_parts) === 2) {

        // namespace/blockname.
        $hook_names[] = $block_parts[0] . '__' . $block_parts[1];
      }
      foreach ($hook_names as $hook_name) {
        $theme_hook_name = $base_hook . '__' . $hook_name;
        if (!isset($templates[$theme_hook_name]) && isset($template_hooks[$theme_hook_name])) {

          // Add the module's theme definition if the template exists.
          $template_hooks[$theme_hook_name]['type'] = 'module';
          $templates[$theme_hook_name] = $block_theme_definition + $template_hooks[$theme_hook_name];
        }
      }
    }
  }
  return $templates;
}