You are here

protected function ConfigDependencies::onDependencyRemovalForMethodGranularity in Drupal 9

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

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::onDependencyRemovalForMethodGranularity()
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 153

Class

ConfigDependencies
Calculates rest resource config dependencies.

Namespace

Drupal\rest\Entity

Code

protected function onDependencyRemovalForMethodGranularity(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)) {

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

          // Remove the key if there are no more authentication providers
          // supported by this request method.
          unset($configuration[$request_method]['supported_auth']);
        }
        if (empty($configuration[$request_method]['supported_formats'])) {

          // Remove the key if there are no more formats supported by this
          // request method.
          unset($configuration[$request_method]['supported_formats']);
        }
        if (empty($configuration[$request_method])) {

          // Remove the request method altogether if it no longer has any
          // supported authentication providers or formats.
          unset($configuration[$request_method]);
        }
      }
    }
    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;
}