You are here

protected function ConfigDependencies::onDependencyRemovalForResourceGranularity in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/rest/src/Entity/ConfigDependencies.php \Drupal\rest\Entity\ConfigDependencies::onDependencyRemovalForResourceGranularity()
  2. 10 core/modules/rest/src/Entity/ConfigDependencies.php \Drupal\rest\Entity\ConfigDependencies::onDependencyRemovalForResourceGranularity()

Informs the entity that entities it depends on will be deleted.

Parameters

\Drupal\rest\RestResourceConfigInterface $rest_config: The rest configuration.

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

Return value

bool TRUE if the entity has been changed as a result, FALSE if not.

1 call to ConfigDependencies::onDependencyRemovalForResourceGranularity()
ConfigDependencies::onDependencyRemoval in core/modules/rest/src/Entity/ConfigDependencies.php
Informs the entity that entities it depends on will be deleted.

File

core/modules/rest/src/Entity/ConfigDependencies.php, line 219

Class

ConfigDependencies
Calculates rest resource config dependencies.

Namespace

Drupal\rest\Entity

Code

protected function onDependencyRemovalForResourceGranularity(RestResourceConfigInterface $rest_config, array $dependencies) {
  $changed = FALSE;

  // Only module-related dependencies can be fixed. All other types of
  // dependencies cannot, because they were not generated based on supported
  // authentication providers or formats.
  if (isset($dependencies['module'])) {

    // Try to fix dependencies.
    $removed_auth = array_keys(array_intersect($this->authProviders, $dependencies['module']));
    $removed_formats = array_keys(array_intersect($this->formatProviders, $dependencies['module']));
    $configuration_before = $configuration = $rest_config
      ->get('configuration');
    if (!empty($removed_auth) || !empty($removed_formats)) {

      // All methods support the same formats and authentication providers, so
      // get those for whichever the first listed method is.
      $first_method = $rest_config
        ->getMethods()[0];

      // Try to fix dependency problems by removing affected
      // authentication providers and formats.
      foreach ($removed_formats as $format) {
        if (in_array($format, $rest_config
          ->getFormats($first_method), TRUE)) {
          $configuration['formats'] = array_diff($configuration['formats'], $removed_formats);
        }
      }
      foreach ($removed_auth as $auth) {
        if (in_array($auth, $rest_config
          ->getAuthenticationProviders($first_method), TRUE)) {
          $configuration['authentication'] = array_diff($configuration['authentication'], $removed_auth);
        }
      }
      if (empty($configuration['authentication'])) {

        // Remove the key if there are no more authentication providers
        // supported.
        unset($configuration['authentication']);
      }
      if (empty($configuration['formats'])) {

        // Remove the key if there are no more formats supported.
        unset($configuration['formats']);
      }
      if (empty($configuration['authentication']) || empty($configuration['formats'])) {

        // If there no longer are any supported authentication providers or
        // formats, this REST resource can no longer function, and so we
        // cannot fix this config entity to keep it working.
        $configuration = [];
      }
    }
    if ($configuration_before != $configuration && !empty($configuration)) {
      $rest_config
        ->set('configuration', $configuration);

      // Only mark the dependencies problems as fixed if there is any
      // configuration left.
      $changed = TRUE;
    }
  }

  // If the dependency problems are not marked as fixed at this point they
  // should be related to the resource plugin and the config entity should
  // be deleted.
  return $changed;
}