layout_builder_test.module

Same filename and directory in other branches
  1. 10 core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module
  2. 9 core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module

Provides hook implementations for Layout Builder tests.

File

core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module

View source
<?php


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

use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_plugin_filter_TYPE__CONSUMER_alter().
 */
function layout_builder_test_plugin_filter_block__layout_builder_alter(array &$definitions, array $extra) {
  // Explicitly remove the "Help" blocks from the list.
  unset($definitions['help_block']);
  // Explicitly remove the "Sticky at top of lists field_block".
  $disallowed_fields = [
    'sticky',
  ];
  // Remove "Changed" field if this is the first section.
  if ($extra['delta'] === 0) {
    $disallowed_fields[] = 'changed';
  }
  foreach ($definitions as $plugin_id => $definition) {
    // Field block IDs are in the form 'field_block:{entity}:{bundle}:{name}',
    // for example 'field_block:node:article:revision_timestamp'.
    preg_match('/field_block:.*:.*:(.*)/', $plugin_id, $parts);
    if (isset($parts[1]) && in_array($parts[1], $disallowed_fields, TRUE)) {
      // Unset any field blocks that match our predefined list.
      unset($definitions[$plugin_id]);
    }
  }
}

/**
 * Implements hook_entity_extra_field_info().
 */
function layout_builder_test_entity_extra_field_info() {
  $extra['node']['bundle_with_section_field']['display']['layout_builder_test'] = [
    'label' => t('Extra label'),
    'description' => t('Extra description'),
    'weight' => 0,
  ];
  return $extra;
}

/**
 * Implements hook_entity_node_view().
 */
function layout_builder_test_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  if ($display->getComponent('layout_builder_test')) {
    $build['layout_builder_test'] = [
      '#markup' => 'Extra, Extra read all about it.',
    ];
  }
}

/**
 * Implements hook_form_BASE_FORM_ID_alter() for layout_builder_configure_block.
 */
function layout_builder_test_form_layout_builder_configure_block_alter(&$form, FormStateInterface $form_state, $form_id) {
  /** @var \Drupal\layout_builder\Form\ConfigureBlockFormBase $form_object */
  $form_object = $form_state->getFormObject();
  $form['layout_builder_test']['storage'] = [
    '#type' => 'item',
    '#title' => 'Layout Builder Storage: ' . $form_object->getSectionStorage()
      ->getStorageId(),
  ];
  $form['layout_builder_test']['section'] = [
    '#type' => 'item',
    '#title' => 'Layout Builder Section: ' . $form_object->getCurrentSection()
      ->getLayoutId(),
  ];
  $form['layout_builder_test']['component'] = [
    '#type' => 'item',
    '#title' => 'Layout Builder Component: ' . $form_object->getCurrentComponent()
      ->getPluginId(),
  ];
}

/**
 * Implements hook_entity_form_display_alter().
 */
function layout_builder_entity_form_display_alter(EntityFormDisplayInterface $form_display, array $context) {
  if ($context['form_mode'] === 'layout_builder') {
    $form_display->setComponent('status', [
      'type' => 'boolean_checkbox',
      'settings' => [
        'display_label' => TRUE,
      ],
    ]);
  }
}

Functions

Title Deprecated Summary
layout_builder_entity_form_display_alter Implements hook_entity_form_display_alter().
layout_builder_test_entity_extra_field_info Implements hook_entity_extra_field_info().
layout_builder_test_form_layout_builder_configure_block_alter Implements hook_form_BASE_FORM_ID_alter() for layout_builder_configure_block.
layout_builder_test_node_view Implements hook_entity_node_view().
layout_builder_test_plugin_filter_block__layout_builder_alter Implements hook_plugin_filter_TYPE__CONSUMER_alter().

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.