function ComponentPluginManager::alterDefinitions

Same name in this branch
  1. 10 core/lib/Drupal/Core/Theme/ComponentPluginManager.php \Drupal\Core\Theme\ComponentPluginManager::alterDefinitions()
Same name in other branches
  1. 11.x core/modules/sdc/src/ComponentPluginManager.php \Drupal\sdc\ComponentPluginManager::alterDefinitions()
  2. 11.x core/lib/Drupal/Core/Theme/ComponentPluginManager.php \Drupal\Core\Theme\ComponentPluginManager::alterDefinitions()

Overrides DefaultPluginManager::alterDefinitions

File

core/modules/sdc/src/ComponentPluginManager.php, line 245

Class

ComponentPluginManager
Defines a plugin manager to deal with sdc.

Namespace

Drupal\sdc

Code

protected function alterDefinitions(&$definitions) {
    // Save in the definition whether this is a module or a theme. This is
    // important because when creating the plugin instance (the Component
    // object) we'll need to negotiate based on the active theme.
    $definitions = array_map([
        $this,
        'alterDefinition',
    ], $definitions);
    // Validate the definition after alterations.
    assert(Inspector::assertAll(fn(array $definition) => $this->isValidDefinition($definition), $definitions));
    parent::alterDefinitions($definitions);
    // Finally, validate replacements.
    $replacing_definitions = array_filter($definitions, static fn(array $definition) => ($definition['replaces'] ?? NULL) && ($definitions[$definition['replaces']] ?? NULL));
    $validation_errors = array_reduce($replacing_definitions, function (array $errors, array $new_definition) use ($definitions) {
        $original_definition = $definitions[$new_definition['replaces']];
        $original_schemas = $original_definition['props'] ?? NULL;
        $new_schemas = $new_definition['props'] ?? NULL;
        if (!$original_schemas || !$new_schemas) {
            return [
                sprintf("Component \"%s\" is attempting to replace \"%s\", however component replacement requires both components to have schema definitions.", $new_definition['id'], $original_definition['id']),
            ];
        }
        try {
            $this->compatibilityChecker
                ->isCompatible($original_schemas, $new_schemas);
        } catch (IncompatibleComponentSchema $e) {
            $errors[] = sprintf("\"%s\" is incompatible with the component is wants to replace \"%s\". Errors:\n%s", $new_definition['id'], $original_definition['id'], $e->getMessage());
        }
        return $errors;
    }, []);
    if (!empty($validation_errors)) {
        throw new IncompatibleComponentSchema(implode("\n", $validation_errors));
    }
}

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