5 * Provides hook implementations for Field Layout.
8 use Drupal\Core\Entity\ContentEntityFormInterface;
9 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
10 use Drupal\Core\Entity\EntityInterface;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\Core\Routing\RouteMatchInterface;
13 use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface;
14 use Drupal\field_layout\Entity\FieldLayoutEntityFormDisplay;
15 use Drupal\field_layout\Entity\FieldLayoutEntityViewDisplay;
16 use Drupal\field_layout\FieldLayoutBuilder;
17 use Drupal\field_layout\Form\FieldLayoutEntityFormDisplayEditForm;
18 use Drupal\field_layout\Form\FieldLayoutEntityViewDisplayEditForm;
21 * Implements hook_help().
23 function field_layout_help($route_name, RouteMatchInterface $route_match) {
24 switch ($route_name) {
25 case 'help.page.field_layout':
26 $output = '<h3>' . t('About') . '</h3>';
27 $output .= '<p>' . t('The Field Layout module allows you to arrange fields into regions on forms and displays of entities such as nodes and users.') . '</p>';
28 $output .= '<p>' . t('For more information, see the <a href=":field-layout-documentation">online documentation for the Field Layout module</a>.', [':field-layout-documentation' => 'https://www.drupal.org/documentation/modules/field_layout']) . '</p>';
34 * Implements hook_entity_type_alter().
36 function field_layout_entity_type_alter(array &$entity_types) {
37 /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
38 $entity_types['entity_view_display']->setClass(FieldLayoutEntityViewDisplay::class);
39 $entity_types['entity_form_display']->setClass(FieldLayoutEntityFormDisplay::class);
41 // The form classes are only needed when Field UI is installed.
42 if (\Drupal::moduleHandler()->moduleExists('field_ui')) {
43 $entity_types['entity_view_display']->setFormClass('edit', FieldLayoutEntityViewDisplayEditForm::class);
44 $entity_types['entity_form_display']->setFormClass('edit', FieldLayoutEntityFormDisplayEditForm::class);
49 * Implements hook_entity_view_alter().
51 function field_layout_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
52 if ($display instanceof EntityDisplayWithLayoutInterface) {
53 \Drupal::classResolver(FieldLayoutBuilder::class)->buildView($build, $display);
58 * Implements hook_form_alter().
60 function field_layout_form_alter(&$form, FormStateInterface $form_state, $form_id) {
61 $form_object = $form_state->getFormObject();
62 if ($form_object instanceof ContentEntityFormInterface && $display = $form_object->getFormDisplay($form_state)) {
63 if ($display instanceof EntityDisplayWithLayoutInterface) {
64 \Drupal::classResolver(FieldLayoutBuilder::class)->buildForm($form, $display);