5 * Post update functions for Layout Builder.
8 use Drupal\Core\Config\Entity\ConfigEntityUpdater;
9 use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
12 * Rebuild plugin dependencies for all entity view displays.
14 function layout_builder_post_update_rebuild_plugin_dependencies(&$sandbox = NULL) {
15 $storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
16 if (!isset($sandbox['ids'])) {
17 $sandbox['ids'] = $storage->getQuery()->accessCheck(FALSE)->execute();
18 $sandbox['count'] = count($sandbox['ids']);
21 for ($i = 0; $i < 10 && count($sandbox['ids']); $i++) {
22 $id = array_shift($sandbox['ids']);
23 if ($display = $storage->load($id)) {
28 $sandbox['#finished'] = empty($sandbox['ids']) ? 1 : ($sandbox['count'] - count($sandbox['ids'])) / $sandbox['count'];
32 * Ensure all extra fields are properly stored on entity view displays.
35 * \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::setComponent()
36 * was not correctly setting the configuration for extra fields. This function
37 * calls setComponent() for all extra field components to ensure the updated
38 * logic is invoked on all extra fields to correct the settings.
40 function layout_builder_post_update_add_extra_fields(&$sandbox = NULL) {
41 $entity_field_manager = \Drupal::service('entity_field.manager');
42 \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'entity_view_display', function (LayoutEntityDisplayInterface $display) use ($entity_field_manager) {
43 if (!$display->isLayoutBuilderEnabled()) {
47 $extra_fields = $entity_field_manager->getExtraFields($display->getTargetEntityTypeId(), $display->getTargetBundle());
48 $components = $display->getComponents();
49 // Sort the components to avoid them being reordered by setComponent().
50 uasort($components, 'Drupal\Component\Utility\SortArray::sortByWeightElement');
52 foreach ($components as $name => $component) {
53 if (isset($extra_fields['display'][$name])) {
54 $display->setComponent($name, $component);