Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / layout_builder / layout_builder.module
1 <?php
2
3 /**
4  * @file
5  * Provides hook implementations for Layout Builder.
6  */
7
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Routing\RouteMatchInterface;
10 use Drupal\Core\Url;
11 use Drupal\field\FieldConfigInterface;
12 use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
13 use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplayStorage;
14 use Drupal\layout_builder\Form\LayoutBuilderEntityViewDisplayForm;
15
16 /**
17  * Implements hook_help().
18  */
19 function layout_builder_help($route_name, RouteMatchInterface $route_match) {
20   // Add help text to the Layout Builder UI.
21   if ($route_match->getRouteObject()->getOption('_layout_builder')) {
22     $output = '<p>' . t('This layout builder tool allows you to configure the layout of the main content area.') . '</p>';
23     if (\Drupal::currentUser()->hasPermission('administer blocks')) {
24       $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>';
25     }
26     else {
27       $output .= '<p>' . t('To manage other areas of the page, use the block administration page.') . '</p>';
28     }
29     return $output;
30   }
31
32   switch ($route_name) {
33     case 'help.page.layout_builder':
34       $output = '<h3>' . t('About') . '</h3>';
35       $output .= '<p>' . t('Layout Builder provides layout building utility.') . '</p>';
36       $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>';
37       return $output;
38   }
39 }
40
41 /**
42  * Implements hook_entity_type_alter().
43  */
44 function layout_builder_entity_type_alter(array &$entity_types) {
45   /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
46   $entity_types['entity_view_display']
47     ->setClass(LayoutBuilderEntityViewDisplay::class)
48     ->setStorageClass(LayoutBuilderEntityViewDisplayStorage::class)
49     ->setFormClass('edit', LayoutBuilderEntityViewDisplayForm::class);
50 }
51
52 /**
53  * Implements hook_form_FORM_ID_alter() for \Drupal\field_ui\Form\EntityFormDisplayEditForm.
54  */
55 function layout_builder_form_entity_form_display_edit_form_alter(&$form, FormStateInterface $form_state) {
56   // Hides the Layout Builder field. It is rendered directly in
57   // \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::buildMultiple().
58   unset($form['fields']['layout_builder__layout']);
59   $key = array_search('layout_builder__layout', $form['#fields']);
60   if ($key !== FALSE) {
61     unset($form['#fields'][$key]);
62   }
63 }
64
65 /**
66  * Implements hook_field_config_insert().
67  */
68 function layout_builder_field_config_insert(FieldConfigInterface $field_config) {
69   // Clear the sample entity for this entity type and bundle.
70   $sample_entity_generator = \Drupal::service('layout_builder.sample_entity_generator');
71   $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 function layout_builder_field_config_delete(FieldConfigInterface $field_config) {
79   // Clear the sample entity for this entity type and bundle.
80   $sample_entity_generator = \Drupal::service('layout_builder.sample_entity_generator');
81   $sample_entity_generator->delete($field_config->getTargetEntityTypeId(), $field_config->getTargetBundle());
82   \Drupal::service('plugin.manager.block')->clearCachedDefinitions();
83 }