function ComponentNegotiator::maybeNegotiateByTheme

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

See if there is a candidate in the theme hierarchy.

Parameters

array[] $candidates: All the components that might be a match.

Return value

string|null The plugin ID for the negotiated component, or NULL if none was found.

1 call to ComponentNegotiator::maybeNegotiateByTheme()
ComponentNegotiator::doNegotiate in core/lib/Drupal/Core/Theme/ComponentNegotiator.php
Negotiates the active component for the current request.

File

core/lib/Drupal/Core/Theme/ComponentNegotiator.php, line 92

Class

ComponentNegotiator
Determines which component should be used.

Namespace

Drupal\Core\Theme

Code

private function maybeNegotiateByTheme(array $candidates) : ?string {
    // Prepare the error message.
    $theme_name = $this->themeManager
        ->getActiveTheme()
        ->getName();
    // Let's do theme based negotiation.
    $base_theme_names = array_map(static fn(Extension $extension) => $extension->getName(), $this->themeManager
        ->getActiveTheme()
        ->getBaseThemeExtensions());
    $considered_themes = [
        $theme_name,
        $base_theme_names,
    ];
    // Only consider components in the theme hierarchy tree.
    $candidates = array_filter($candidates, static fn(array $definition) => $definition['extension_type'] === ExtensionType::Theme && in_array($definition['provider'], $considered_themes, TRUE));
    if (empty($candidates)) {
        return NULL;
    }
    $theme_weights = array_flip($considered_themes);
    $sort_by_theme_weight = static fn(array $definition_a, array $definition_b) => $theme_weights[$definition_a['provider']] <=> $theme_weights[$definition_b['provider']];
    // Sort the candidates by weight and choose the one with the lowest weight.
    uasort($candidates, $sort_by_theme_weight);
    $definition = reset($candidates);
    return $definition['id'] ?? NULL;
}

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