Commit 633128ec authored by catch's avatar catch
Browse files

Issue #3541507 by quietone, ajinkya45, smustgrave, dcam, larowlan: Move...

Issue #3541507 by quietone, ajinkya45, smustgrave, dcam, larowlan: Move field_layout display updates from layout_builder to field_layout
parent 86e7dc8b
Loading
Loading
Loading
Loading
Loading
+37 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\field_layout\Hook;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\ContentEntityFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
@@ -13,6 +14,8 @@
use Drupal\field_layout\Form\FieldLayoutEntityViewDisplayEditForm;
use Drupal\field_layout\Entity\FieldLayoutEntityFormDisplay;
use Drupal\field_layout\Entity\FieldLayoutEntityViewDisplay;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\layout_builder\Section;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Hook\Attribute\Hook;

@@ -78,4 +81,38 @@ public function formAlter(&$form, FormStateInterface $form_state, $form_id) : vo
    }
  }

  /**
   * Implements hook_modules_installed().
   */
  #[Hook('modules_installed')]
  public function modulesInstalled($modules, bool $is_syncing): void {
    if (!in_array('layout_builder', $modules)) {
      return;
    }
    $display_changed = FALSE;

    $displays = LayoutBuilderEntityViewDisplay::loadMultiple();
    /** @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface[] $displays */
    foreach ($displays as $display) {
      // Create the first section from any existing Field Layout settings.
      $field_layout = $display->getThirdPartySettings('field_layout');
      if (isset($field_layout['id'])) {
        $field_layout += ['settings' => []];
        $display
          ->enableLayoutBuilder()
          ->appendSection(new Section($field_layout['id'], $field_layout['settings']))
          ->save();
        $display_changed = TRUE;
      }

      // Clear the rendered cache to ensure the new layout builder flow is used.
      // While in many cases the above change will not affect the rendered
      // output, the cacheability metadata will have changed and should be
      // processed to prepare for future changes.
      if ($display_changed) {
        Cache::invalidateTags(['rendered']);
      }
    }
  }

}
+0 −33
Original line number Diff line number Diff line
@@ -5,40 +5,7 @@
 * Contains install and update functions for Layout Builder.
 */

use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\layout_builder\Section;

/**
 * Implements hook_install().
 */
function layout_builder_install(): void {
  $display_changed = FALSE;

  $displays = LayoutBuilderEntityViewDisplay::loadMultiple();
  /** @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface[] $displays */
  foreach ($displays as $display) {
    // Create the first section from any existing Field Layout settings.
    $field_layout = $display->getThirdPartySettings('field_layout');
    if (isset($field_layout['id'])) {
      $field_layout += ['settings' => []];
      $display
        ->enableLayoutBuilder()
        ->appendSection(new Section($field_layout['id'], $field_layout['settings']))
        ->save();
      $display_changed = TRUE;
    }
  }

  // Clear the rendered cache to ensure the new layout builder flow is used.
  // While in many cases the above change will not affect the rendered output,
  // the cacheability metadata will have changed and should be processed to
  // prepare for future changes.
  if ($display_changed) {
    Cache::invalidateTags(['rendered']);
  }
}

/**
 * Implements hook_schema().