function MediaEmbed::onCollectionDependencyRemoval

Same name and namespace in other branches
  1. main core/modules/media/src/Plugin/Filter/MediaEmbed.php \Drupal\media\Plugin\Filter\MediaEmbed::onCollectionDependencyRemoval()

Informs the plugin in a collection to act on removal of dependencies.

This method allows a plugin instance in a collection to remove dependencies from their configuration. For example, if a plugin integrates with a specific module, it should remove that module from its own configuration when the module is uninstalled.

Parameters

array<string, list<string>> $dependencies: An array of dependencies that will be deleted keyed by dependency type. Dependency types are, for example, entity, module and theme.

Return value

\Drupal\Core\Plugin\RemovableDependentPluginReturn

  • RemovableDependentPluginReturn::Changed if the configuration of the plugin instance has changed
  • RemovableDependentPluginReturn::Remove if the plugin instance should be removed from the plugin collection
  • RemovableDependentPluginReturn::Unchanged if the configuration of the plugin instance has not changed.

Overrides RemovableDependentPluginInterface::onCollectionDependencyRemoval

File

core/modules/media/src/Plugin/Filter/MediaEmbed.php, line 487

Class

MediaEmbed
Provides a filter to embed media items using a custom tag.

Namespace

Drupal\media\Plugin\Filter

Code

public function onCollectionDependencyRemoval(array $dependencies) : RemovableDependentPluginReturn {
  $status = RemovableDependentPluginReturn::Unchanged;
  if (!isset($dependencies['config'])) {
    return $status;
  }
  // If view modes for media are deleted, remove the view mode from the plugin
  // settings and return that the plugin settings have changed.
  foreach ($dependencies['config'] as $config) {
    if ($config instanceof EntityViewModeInterface && str_starts_with($config->id(), 'media.')) {
      $view_mode_id = substr_replace($config->id(), '', 0, 6);
      if (isset($this->settings['allowed_view_modes'][$view_mode_id])) {
        unset($this->settings['allowed_view_modes'][$view_mode_id]);
        $status = RemovableDependentPluginReturn::Changed;
      }
      // If the default embed view mode is set to a view mode being deleted,
      // change the default embed view mode to the default entity display
      // mode, and make sure that default is in the allowed view modes.
      if ($this->settings['default_view_mode'] === $view_mode_id) {
        $this->settings['default_view_mode'] = EntityDisplayRepositoryInterface::DEFAULT_DISPLAY_MODE;
        $this->settings['allowed_view_modes'] += [
          EntityDisplayRepositoryInterface::DEFAULT_DISPLAY_MODE => EntityDisplayRepositoryInterface::DEFAULT_DISPLAY_MODE,
        ];
        ksort($this->settings['allowed_view_modes']);
        $status = RemovableDependentPluginReturn::Changed;
      }
    }
  }
  return $status;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.