layout_builder.module 3.51 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

/**
 * @file
 * Provides hook implementations for Layout Builder.
 */

use Drupal\Core\Form\FormStateInterface;
9 10
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
11 12 13 14
use Drupal\field\FieldConfigInterface;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplayStorage;
use Drupal\layout_builder\Form\LayoutBuilderEntityViewDisplayForm;
15 16 17 18

/**
 * Implements hook_help().
 */
19 20 21 22 23 24 25 26 27 28 29 30 31
function layout_builder_help($route_name, RouteMatchInterface $route_match) {
  // Add help text to the Layout Builder UI.
  if ($route_match->getRouteObject()->getOption('_layout_builder')) {
    $output = '<p>' . t('This layout builder tool allows you to configure the layout of the main content area.') . '</p>';
    if (\Drupal::currentUser()->hasPermission('administer blocks')) {
      $output .= '<p>' . t('To manage other areas of the page, use the <a href="@block-ui">block administration page</a>.', ['@block-ui' => Url::fromRoute('block.admin_display')->toString()]) . '</p>';
    }
    else {
      $output .= '<p>' . t('To manage other areas of the page, use the block administration page.') . '</p>';
    }
    return $output;
  }

32 33 34 35 36 37 38 39 40 41 42 43 44 45
  switch ($route_name) {
    case 'help.page.layout_builder':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Layout Builder provides layout building utility.') . '</p>';
      $output .= '<p>' . t('For more information, see the <a href=":layout-builder-documentation">online documentation for the Layout Builder module</a>.', [':layout-builder-documentation' => 'https://www.drupal.org/docs/8/core/modules/layout_builder']) . '</p>';
      return $output;
  }
}

/**
 * Implements hook_entity_type_alter().
 */
function layout_builder_entity_type_alter(array &$entity_types) {
  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
46 47 48 49
  $entity_types['entity_view_display']
    ->setClass(LayoutBuilderEntityViewDisplay::class)
    ->setStorageClass(LayoutBuilderEntityViewDisplayStorage::class)
    ->setFormClass('edit', LayoutBuilderEntityViewDisplayForm::class);
50 51 52
}

/**
53
 * Implements hook_form_FORM_ID_alter() for \Drupal\field_ui\Form\EntityFormDisplayEditForm.
54
 */
55 56 57
function layout_builder_form_entity_form_display_edit_form_alter(&$form, FormStateInterface $form_state) {
  // Hides the Layout Builder field. It is rendered directly in
  // \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::buildMultiple().
58 59 60 61 62 63 64 65
  unset($form['fields']['layout_builder__layout']);
  $key = array_search('layout_builder__layout', $form['#fields']);
  if ($key !== FALSE) {
    unset($form['#fields'][$key]);
  }
}

/**
66
 * Implements hook_field_config_insert().
67
 */
68 69
function layout_builder_field_config_insert(FieldConfigInterface $field_config) {
  // Clear the sample entity for this entity type and bundle.
70 71
  $sample_entity_generator = \Drupal::service('layout_builder.sample_entity_generator');
  $sample_entity_generator->delete($field_config->getTargetEntityTypeId(), $field_config->getTargetBundle());
72
  \Drupal::service('plugin.manager.block')->clearCachedDefinitions();
73 74 75
}

/**
76
 * Implements hook_field_config_delete().
77
 */
78 79
function layout_builder_field_config_delete(FieldConfigInterface $field_config) {
  // Clear the sample entity for this entity type and bundle.
80 81
  $sample_entity_generator = \Drupal::service('layout_builder.sample_entity_generator');
  $sample_entity_generator->delete($field_config->getTargetEntityTypeId(), $field_config->getTargetBundle());
82
  \Drupal::service('plugin.manager.block')->clearCachedDefinitions();
83
}