classResolver example

'media_post_update_storage_handler' => '9.0.0',
    'media_post_update_enable_standalone_url' => '9.0.0',
    'media_post_update_add_status_extra_filter' => '9.0.0',
    'media_post_update_modify_base_field_author_override' => '10.0.0',
  ];
}

/** * Add the oEmbed loading attribute setting to field formatter instances. */
function media_post_update_oembed_loading_attribute(array &$sandbox = NULL): void {
  $media_config_updater = \Drupal::classResolver(MediaConfigUpdater::class);
  assert($media_config_updater instanceof MediaConfigUpdater);
  $media_config_updater->setDeprecationsEnabled(TRUE);
  \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'entity_view_display', function DEntityViewDisplayInterface $view_display) use ($media_config_updater): bool {
    return $media_config_updater->processOembedEagerLoadField($view_display);
  });
}
$current_perm = $config->get('display.default.display_options.access.options.perm');
  if ($current_perm === 'administer blocks') {
    $config->set('display.default.display_options.access.options.perm', 'access block library')
      ->save(TRUE);
  }
}

/** * Update permissions for users with "administer blocks" permission. */
function block_content_post_update_sort_permissions(&$sandbox = NULL) {
  \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'user_role', function DRole $role) {
    if ($role->hasPermission('administer blocks')) {
      $role->grantPermission('administer block content');
      $role->grantPermission('access block library');
      $role->grantPermission('administer block types');
      return TRUE;
    }
    return FALSE;
  });
}
function responsive_image_removed_post_updates() {
  return [
    'responsive_image_post_update_recreate_dependencies' => '9.0.0',
  ];
}

/** * Re-order mappings by breakpoint ID and descending numeric multiplier order. */
function responsive_image_post_update_order_multiplier_numerically(array &$sandbox = NULL): void {
  /** @var \Drupal\responsive_image\ResponsiveImageConfigUpdater $responsive_image_config_updater */
  $responsive_image_config_updater = \Drupal::classResolver(ResponsiveImageConfigUpdater::class);
  $responsive_image_config_updater->setDeprecationsEnabled(FALSE);
  \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'responsive_image_style', function DResponsiveImageStyleInterface $responsive_image_style) use ($responsive_image_config_updater): bool {
    return $responsive_image_config_updater->orderMultipliersNumerically($responsive_image_style);
  });
}

/** * Add the image loading settings to responsive image field formatter instances. */
function responsive_image_post_update_image_loading_attribute(array &$sandbox = NULL): void {
  $responsive_image_config_updater = \Drupal::classResolver(ResponsiveImageConfigUpdater::class);
  

function file_removed_post_updates() {
  return [
    'file_post_update_add_txt_if_allows_insecure_extensions' => '10.0.0',
  ];
}

/** * Grant all non-anonymous roles the 'delete own files' permission. */
function file_post_update_add_permissions_to_roles(?array &$sandbox = NULL): void {
  \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'user_role', function DRoleInterface $role): bool {
    if ($role->id() === RoleInterface::ANONYMOUS_ID || $role->isAdmin()) {
      return FALSE;
    }
    $role->grantPermission('delete own files');
    return TRUE;
  });
}
/** * No-op update. */
function user_post_update_sort_permissions(&$sandbox = NULL) {
}

/** * Ensure permissions stored in role configuration are sorted using the schema. */
function user_post_update_sort_permissions_again(&$sandbox = NULL) {
  \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'user_role', function DRole $role) {
    $permissions = $role->getPermissions();
    sort($permissions);
    return $permissions !== $role->getPermissions();
  });
}
function hook_post_update_NAME(&$sandbox) {
  // Example of updating some content.   $node = \Drupal\node\Entity\Node::load(123);
  $node->setTitle('foo');
  $node->save();

  $result = t('Node %nid saved', ['%nid' => $node->id()]);

  // Example of updating some config.   if (\Drupal::moduleHandler()->moduleExists('taxonomy')) {
    // Update the dependencies of all Vocabulary configuration entities.     \Drupal::classResolver(\Drupal\Core\Config\Entity\ConfigEntityUpdater::class)->update($sandbox, 'taxonomy_vocabulary');
  }

  return $result;
}

/** * Return an array of removed hook_post_update_NAME() function names. * * This should be used to indicate post-update functions that have existed in * some previous version of the module, but are no longer available. * * This implementation has to be placed in a MODULE.post_update.php file. * * @return string[] * An array where the keys are removed post-update function names, and the * values are the first stable version in which the update was removed. * * @ingroup update_api * * @see hook_post_update_NAME() */

  public function __construct(array $values$entity_type_id = 'responsive_image_style') {
    parent::__construct($values$entity_type_id);
  }

  /** * {@inheritdoc} */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);
    $config_updater = \Drupal::classResolver(ResponsiveImageConfigUpdater::class);
    $config_updater->orderMultipliersNumerically($this);
  }

  /** * {@inheritdoc} */
  public function addImageStyleMapping($breakpoint_id$multiplier, array $image_style_mapping) {
    // If there is an existing mapping, overwrite it.     foreach ($this->image_style_mappings as &$mapping) {
      if ($mapping['breakpoint_id'] === $breakpoint_id && $mapping['multiplier'] === $multiplier) {
        $mapping = $image_style_mapping + [
          
'layout_builder_post_update_tempstore_route_enhancer' => '10.0.0',
  ];
}

/** * Update timestamp formatter settings for Layout Builder fields. */
function layout_builder_post_update_timestamp_formatter(array &$sandbox = NULL): void {
  /** @var \Drupal\Core\Field\FormatterPluginManager $field_formatter_manager */
  $field_formatter_manager = \Drupal::service('plugin.manager.field.formatter');

  \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'entity_view_display', function DEntityViewDisplayInterface $entity_view_display) use ($field_formatter_manager): bool {
    $update = FALSE;
    if ($entity_view_display instanceof LayoutEntityDisplayInterface && $entity_view_display->isLayoutBuilderEnabled()) {
      foreach ($entity_view_display->getSections() as $section) {
        foreach ($section->getComponents() as $component) {
          if (str_starts_with($component->getPluginId(), 'field_block:')) {
            $configuration = $component->get('configuration');
            $formatter =& $configuration['formatter'];
            if ($formatter && isset($formatter['type'])) {
              $plugin_definition = $field_formatter_manager->getDefinition($formatter['type'], FALSE);
              // Check also potential plugins extending TimestampFormatter.               if ($plugin_definition && is_a($plugin_definition['class'], TimestampFormatter::class, TRUE)) {
                
$this->assertNotNull(\Drupal::cache('test'));
  }

  /** * Tests the classResolver method. * * @covers ::classResolver */
  public function testClassResolver() {
    $class_resolver = $this->prophesize(ClassResolverInterface::class);
    $this->setMockContainerService('class_resolver', $class_resolver->reveal());
    $this->assertInstanceOf(ClassResolverInterface::class, \Drupal::classResolver());
  }

  /** * Tests the classResolver method when called with a class. * * @covers ::classResolver */
  public function testClassResolverWithClass() {
    $class_resolver = $this->prophesize(ClassResolverInterface::class);
    $class_resolver->getInstanceFromDefinition(static::class)->willReturn($this);
    $this->setMockContainerService('class_resolver', $class_resolver->reveal());
    
'#type' => 'fieldset',
      '#title' => $this->t('Wrapper'),
      '#tree' => TRUE,
      '#parents' => ['foo'],
    ];
    $entity = EntityTest::load(1);
    if ($entity) {
      if ($entity) {
        $display = EntityFormDisplay::collectRenderDisplay($entity, 'default');
        $subform_state = SubformState::createForSubform($form['foo']$form$form_state);
        $display->buildForm($entity$form['foo']$subform_state);
        \Drupal::classResolver(FieldLayoutBuilder::class)->buildForm($form['foo']$display$subform_state);
      }
    }
    return $form;
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Do nothing.   }

}
$config = \Drupal::configFactory()->getEditable('system.feature_flags');
  $config->set('linkset_endpoint', FALSE)->save();
}

/** * Update timestamp formatter settings for entity view displays. */
function system_post_update_timestamp_formatter(array &$sandbox = NULL): void {
  /** @var \Drupal\Core\Field\FormatterPluginManager $field_formatter_manager */
  $field_formatter_manager = \Drupal::service('plugin.manager.field.formatter');

  \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'entity_view_display', function DEntityViewDisplayInterface $entity_view_display) use ($field_formatter_manager): bool {
    $update = FALSE;
    foreach ($entity_view_display->getComponents() as $name => $component) {
      if (empty($component['type'])) {
        continue;
      }

      $plugin_definition = $field_formatter_manager->getDefinition($component['type'], FALSE);
      // Check also potential plugins extending TimestampFormatter.       if (!is_a($plugin_definition['class'], TimestampFormatter::class, TRUE)) {
        continue;
      }

      

function ckeditor5_removed_post_updates() {
  return [
    'ckeditor5_post_update_alignment_buttons' => '10.0.0',
  ];
}

/** * The image toolbar item changed from `uploadImage` to `drupalInsertImage`. */
function ckeditor5_post_update_image_toolbar_item(&$sandbox = []) {
  $config_entity_updater = \Drupal::classResolver(ConfigEntityUpdater::class);

  $callback = function DEditor $editor) {
    // Only try to update editors using CKEditor 5.     if ($editor->getEditor() !== 'ckeditor5') {
      return FALSE;
    }

    $needs_update = FALSE;
    // Only update if the editor is using the `uploadImage` toolbar item.     $settings = $editor->getSettings();
    if (is_array($settings['toolbar']['items']) && in_array('uploadImage', $settings['toolbar']['items'], TRUE)) {
      
function text_removed_post_updates() {
  return [
    'text_post_update_add_required_summary_flag' => '9.0.0',
    'text_post_update_add_required_summary_flag_form_display' => '10.0.0',
  ];
}

/** * Add allowed_formats setting to existing text fields. */
function text_post_update_allowed_formats(&$sandbox = NULL) {
  \Drupal::classResolver(ConfigEntityUpdater::class)
    ->update($sandbox, 'field_config', function DFieldConfigInterface $field_config) {
      $class = get_class($field_config);
      // Deal only with text fields and descendants.       if (is_a($class, TextItemBase::class, TRUE)) {
        // Get the existing allowed_formats setting.         $allowed_formats = $field_config->get('settings.allowed_formats');
        if (!is_array($allowed_formats) && empty($allowed_formats)) {
          // Save default value if existing value not present.           $field_config->set('settings.allowed_formats', []);
          return TRUE;
        }
      }

function views_post_update_boolean_custom_titles(?array &$sandbox = NULL): void {
  // Empty update to rebuild Views config schema. }

/** * Add eager load option to all oembed type field configurations. */
function views_post_update_oembed_eager_load(?array &$sandbox = NULL): void {
  /** @var \Drupal\views\ViewsConfigUpdater $view_config_updater */
  $view_config_updater = \Drupal::classResolver(ViewsConfigUpdater::class);
  \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'view', function DViewEntityInterface $view) use ($view_config_updater): bool {
    return $view_config_updater->needsOembedEagerLoadFieldUpdate($view);
  });
}

/** * Add lazy load options to all responsive image type field configurations. */
function views_post_update_responsive_image_lazy_load(?array &$sandbox = NULL): void {
  /** @var \Drupal\views\ViewsConfigUpdater $view_config_updater */
  $view_config_updater = \Drupal::classResolver(ViewsConfigUpdater::class);
  
Home | Imprint | This part of the site doesn't use cookies.