annotate core/modules/layout_builder/layout_builder.post_update.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children af1871eacc83
rev   line source
Chris@14 1 <?php
Chris@14 2
Chris@14 3 /**
Chris@14 4 * @file
Chris@14 5 * Post update functions for Layout Builder.
Chris@14 6 */
Chris@14 7
Chris@17 8 use Drupal\Core\Config\Entity\ConfigEntityUpdater;
Chris@17 9 use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
Chris@17 10
Chris@14 11 /**
Chris@14 12 * Rebuild plugin dependencies for all entity view displays.
Chris@14 13 */
Chris@14 14 function layout_builder_post_update_rebuild_plugin_dependencies(&$sandbox = NULL) {
Chris@14 15 $storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
Chris@14 16 if (!isset($sandbox['ids'])) {
Chris@14 17 $sandbox['ids'] = $storage->getQuery()->accessCheck(FALSE)->execute();
Chris@14 18 $sandbox['count'] = count($sandbox['ids']);
Chris@14 19 }
Chris@14 20
Chris@14 21 for ($i = 0; $i < 10 && count($sandbox['ids']); $i++) {
Chris@14 22 $id = array_shift($sandbox['ids']);
Chris@14 23 if ($display = $storage->load($id)) {
Chris@14 24 $display->save();
Chris@14 25 }
Chris@14 26 }
Chris@14 27
Chris@14 28 $sandbox['#finished'] = empty($sandbox['ids']) ? 1 : ($sandbox['count'] - count($sandbox['ids'])) / $sandbox['count'];
Chris@14 29 }
Chris@17 30
Chris@17 31 /**
Chris@17 32 * Ensure all extra fields are properly stored on entity view displays.
Chris@17 33 *
Chris@17 34 * Previously
Chris@17 35 * \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::setComponent()
Chris@17 36 * was not correctly setting the configuration for extra fields. This function
Chris@17 37 * calls setComponent() for all extra field components to ensure the updated
Chris@17 38 * logic is invoked on all extra fields to correct the settings.
Chris@17 39 */
Chris@17 40 function layout_builder_post_update_add_extra_fields(&$sandbox = NULL) {
Chris@17 41 $entity_field_manager = \Drupal::service('entity_field.manager');
Chris@17 42 \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'entity_view_display', function (LayoutEntityDisplayInterface $display) use ($entity_field_manager) {
Chris@17 43 if (!$display->isLayoutBuilderEnabled()) {
Chris@17 44 return FALSE;
Chris@17 45 }
Chris@17 46
Chris@17 47 $extra_fields = $entity_field_manager->getExtraFields($display->getTargetEntityTypeId(), $display->getTargetBundle());
Chris@17 48 $components = $display->getComponents();
Chris@17 49 // Sort the components to avoid them being reordered by setComponent().
Chris@17 50 uasort($components, 'Drupal\Component\Utility\SortArray::sortByWeightElement');
Chris@17 51 $result = FALSE;
Chris@17 52 foreach ($components as $name => $component) {
Chris@17 53 if (isset($extra_fields['display'][$name])) {
Chris@17 54 $display->setComponent($name, $component);
Chris@17 55 $result = TRUE;
Chris@17 56 }
Chris@17 57 }
Chris@17 58 return $result;
Chris@17 59 });
Chris@17 60 }