Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / layout_builder / layout_builder.post_update.php
1 <?php
2
3 /**
4  * @file
5  * Post update functions for Layout Builder.
6  */
7
8 use Drupal\Core\Config\Entity\ConfigEntityUpdater;
9 use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
10
11 /**
12  * Rebuild plugin dependencies for all entity view displays.
13  */
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']);
19   }
20
21   for ($i = 0; $i < 10 && count($sandbox['ids']); $i++) {
22     $id = array_shift($sandbox['ids']);
23     if ($display = $storage->load($id)) {
24       $display->save();
25     }
26   }
27
28   $sandbox['#finished'] = empty($sandbox['ids']) ? 1 : ($sandbox['count'] - count($sandbox['ids'])) / $sandbox['count'];
29 }
30
31 /**
32  * Ensure all extra fields are properly stored on entity view displays.
33  *
34  * Previously
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.
39  */
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()) {
44       return FALSE;
45     }
46
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');
51     $result = FALSE;
52     foreach ($components as $name => $component) {
53       if (isset($extra_fields['display'][$name])) {
54         $display->setComponent($name, $component);
55         $result = TRUE;
56       }
57     }
58     return $result;
59   });
60 }