You are here

function system_post_update_extra_fields in Drupal 8

Update all entity view displays that contain extra fields.

File

core/modules/system/system.post_update.php, line 155
Post update functions for System.

Code

function system_post_update_extra_fields(&$sandbox = NULL) {
  $config_entity_updater = \Drupal::classResolver(ConfigEntityUpdater::class);
  $entity_field_manager = \Drupal::service('entity_field.manager');
  $callback = function (EntityDisplayInterface $display) use ($entity_field_manager) {
    $display_context = $display instanceof EntityViewDisplayInterface ? 'display' : 'form';
    $extra_fields = $entity_field_manager
      ->getExtraFields($display
      ->getTargetEntityTypeId(), $display
      ->getTargetBundle());

    // If any extra fields are used as a component, resave the display with the
    // updated component information.
    $needs_save = FALSE;
    if (!empty($extra_fields[$display_context])) {
      foreach ($extra_fields[$display_context] as $name => $extra_field) {
        if ($component = $display
          ->getComponent($name)) {
          $display
            ->setComponent($name, $component);
          $needs_save = TRUE;
        }
      }
    }
    return $needs_save;
  };
  $config_entity_updater
    ->update($sandbox, 'entity_view_display', $callback);
}