1<?php
2
3/**
4 * @file
5 * Provides hook implementations for Layout Builder tests.
6 */
7
8use Drupal\Core\Breadcrumb\Breadcrumb;
9use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
10use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
11use Drupal\Core\Entity\EntityInterface;
12use Drupal\Core\Form\FormStateInterface;
13use Drupal\Core\Link;
14use Drupal\Core\Routing\RouteMatchInterface;
15use Drupal\Core\Url;
16
17/**
18 * Implements hook_plugin_filter_TYPE__CONSUMER_alter().
19 */
20function layout_builder_test_plugin_filter_block__layout_builder_alter(array &$definitions, array $extra) {
21  // Explicitly remove the "Help" blocks from the list.
22  unset($definitions['help_block']);
23
24  // Explicitly remove the "Sticky at top of lists field_block".
25  $disallowed_fields = [
26    'sticky',
27  ];
28
29  // Remove "Changed" field if this is the first section.
30  if ($extra['delta'] === 0) {
31    $disallowed_fields[] = 'changed';
32  }
33
34  foreach ($definitions as $plugin_id => $definition) {
35    // Field block IDs are in the form 'field_block:{entity}:{bundle}:{name}',
36    // for example 'field_block:node:article:revision_timestamp'.
37    preg_match('/field_block:.*:.*:(.*)/', $plugin_id, $parts);
38    if (isset($parts[1]) && in_array($parts[1], $disallowed_fields, TRUE)) {
39      // Unset any field blocks that match our predefined list.
40      unset($definitions[$plugin_id]);
41    }
42  }
43}
44
45/**
46 * Implements hook_entity_extra_field_info().
47 */
48function layout_builder_test_entity_extra_field_info() {
49  $extra['node']['bundle_with_section_field']['display']['layout_builder_test'] = [
50    'label' => t('Extra label'),
51    'description' => t('Extra description'),
52    'weight' => 0,
53  ];
54  $extra['node']['bundle_with_section_field']['display']['layout_builder_test_2'] = [
55    'label' => t('Extra Field 2'),
56    'description' => t('Extra Field 2 description'),
57    'weight' => 0,
58    'visible' => FALSE,
59  ];
60  return $extra;
61}
62
63/**
64 * Implements hook_entity_node_view().
65 */
66function layout_builder_test_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
67  if ($display->getComponent('layout_builder_test')) {
68    $build['layout_builder_test'] = [
69      '#markup' => 'Extra, Extra read all about it.',
70    ];
71  }
72  if ($display->getComponent('layout_builder_test_2')) {
73    $build['layout_builder_test_2'] = [
74      '#markup' => 'Extra Field 2 is hidden by default.',
75    ];
76  }
77}
78
79/**
80 * Implements hook_form_BASE_FORM_ID_alter() for layout_builder_configure_block.
81 */
82function layout_builder_test_form_layout_builder_configure_block_alter(&$form, FormStateInterface $form_state, $form_id) {
83  /** @var \Drupal\layout_builder\Form\ConfigureBlockFormBase $form_object */
84  $form_object = $form_state->getFormObject();
85
86  $form['layout_builder_test']['storage'] = [
87    '#type' => 'item',
88    '#title' => 'Layout Builder Storage: ' . $form_object->getSectionStorage()->getStorageId(),
89  ];
90  $form['layout_builder_test']['section'] = [
91    '#type' => 'item',
92    '#title' => 'Layout Builder Section: ' . $form_object->getCurrentSection()->getLayoutId(),
93  ];
94  $form['layout_builder_test']['component'] = [
95    '#type' => 'item',
96    '#title' => 'Layout Builder Component: ' . $form_object->getCurrentComponent()->getPluginId(),
97  ];
98}
99
100/**
101 * Implements hook_entity_form_display_alter().
102 */
103function layout_builder_entity_form_display_alter(EntityFormDisplayInterface $form_display, array $context) {
104  if ($context['form_mode'] === 'layout_builder') {
105    $form_display->setComponent('status', [
106      'type' => 'boolean_checkbox',
107      'settings' => [
108        'display_label' => TRUE,
109      ],
110    ]);
111  }
112}
113
114/**
115 * Implements hook_system_breadcrumb_alter().
116 */
117function layout_builder_test_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatchInterface $route_match, array $context) {
118  $breadcrumb->addLink(Link::fromTextAndUrl('External link', Url::fromUri('http://www.example.com')));
119}
120
121/**
122 * Implements hook_module_implements_alter().
123 */
124function layout_builder_test_module_implements_alter(&$implementations, $hook) {
125  if ($hook === 'system_breadcrumb_alter') {
126    // Move our hook_system_breadcrumb_alter() implementation to run before
127    // layout_builder_system_breadcrumb_alter().
128    $group = $implementations['layout_builder_test'];
129    $implementations = [
130      'layout_builder_test' => $group,
131    ] + $implementations;
132  }
133}
134