class MenuLinkDepthConstraintValidator
Validates the MenuLinkDepthConstraint constraint.
Hierarchy
- class \Drupal\Core\Validation\Plugin\Validation\Constraint\RangeConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator
- class \Drupal\Core\Menu\Plugin\Validation\Constraint\MenuLinkDepthConstraintValidator extends \Drupal\Core\Validation\Plugin\Validation\Constraint\RangeConstraintValidator implements \Drupal\Core\DependencyInjection\ContainerInjectionInterface
Expanded class hierarchy of MenuLinkDepthConstraintValidator
File
-
core/
lib/ Drupal/ Core/ Menu/ Plugin/ Validation/ Constraint/ MenuLinkDepthConstraintValidator.php, line 18
Namespace
Drupal\Core\Menu\Plugin\Validation\ConstraintView source
class MenuLinkDepthConstraintValidator extends RangeConstraintValidator implements ContainerInjectionInterface {
public function __construct(MenuLinkTreeInterface $menuLinkTree) {
}
/**
* {@inheritdoc}
*/
public function validate(mixed $value, Constraint $constraint) : void {
assert($constraint instanceof MenuLinkDepthConstraint);
// The depth can never exceed the maximum depth of a menu tree.
$constraint->max = $this->menuLinkTree
->maxDepth();
$base_level = $constraint->baseLevel;
// The base level might be a dynamic name that needs to be resolved.
if (is_string($base_level)) {
$base_level = TypeResolver::resolveDynamicTypeName($base_level, $this->context
->getObject());
}
if (!is_numeric($base_level)) {
throw new RuntimeException('The `base` option must be a number, or an expression that resolves to one.');
}
// Clamp $base_level (which is zero-based) to at least 0 and no more than
// the maximum depth of a menu tree.
$base_level = max(0, $base_level);
$base_level = min($base_level, $constraint->max);
$constraint->max -= $base_level;
// We're validating a depth -- i.e., the number of levels in the menu tree
// that should be visible. 0 effectively means "show only one level", but
// it's not a valid value: we can't show 0 levels of the tree, after all (
// the minimum number of levels we *could* show is 1). The discrepancy is
// due to $base_level being zero-based, so adjust for that.
if ($constraint->max === 0) {
$constraint->max = 1;
}
parent::validate($value, $constraint);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) : static {
return new static($container->get(MenuLinkTreeInterface::class));
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
MenuLinkDepthConstraintValidator::create | public static | function | Instantiates a new instance of this class. | Overrides ContainerInjectionInterface::create |
MenuLinkDepthConstraintValidator::validate | public | function | Overrides RangeConstraintValidator::validate | |
MenuLinkDepthConstraintValidator::__construct | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.