function Block::validateRegion

Same name and namespace in other branches
  1. 11.x core/modules/block/src/Entity/Block.php \Drupal\block\Entity\Block::validateRegion()

Validates that a region exists in the active theme.

Parameters

null|string $region: The region to validate.

\Symfony\Component\Validator\Context\ExecutionContextInterface $context: The validation context.

File

core/modules/block/src/Entity/Block.php, line 379

Class

Block
Defines a Block configuration entity class.

Namespace

Drupal\block\Entity

Code

public static function validateRegion(?string $region, ExecutionContextInterface $context) : void {
  if ($theme = $context->getRoot()
    ->get('theme')
    ->getValue()) {
    $theme_extension = NULL;
    try {
      $theme_extension = \Drupal::service(ThemeHandlerInterface::class)->getTheme($theme);
    } catch (UnknownExtensionException) {
    }
    if (!$theme_extension || !array_key_exists($region, $theme_extension->listAllRegions())) {
      $context->addViolation('This is not a valid region of the %theme theme.', [
        '%theme' => $theme,
      ]);
    }
  }
  else {
    $context->addViolation('This block does not say which theme it appears in.');
  }
}

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