Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ContextDefinition.php

function ContextDefinition::isSatisfiedBy

Overrides ContextDefinitionInterface::isSatisfiedBy

File

core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php, line 293

Class

ContextDefinition
Defines a class for context definitions.

Namespace

Drupal\Core\Plugin\Context

Code

public function isSatisfiedBy(ContextInterface $context) {
    $definition = $context->getContextDefinition();
    if (!$this->dataTypeMatches($context)) {
        return FALSE;
    }
    // Get the value for this context, either directly if possible or by
    // introspecting the definition.
    if ($context->hasContextValue()) {
        $values = [
            $context->getContextData(),
        ];
    }
    elseif ($definition instanceof self) {
        $values = $definition->getSampleValues();
    }
    else {
        $values = [];
    }
    $validator = $this->getTypedDataManager()
        ->getValidator();
    foreach ($values as $value) {
        $constraints = array_values($this->getConstraintObjects());
        if ($definition->isMultiple()) {
            $violations = new ConstraintViolationList();
            foreach ($value as $item) {
                $violations->addAll($validator->validate($item, $constraints));
            }
        }
        else {
            $violations = $validator->validate($value, $constraints);
        }
        foreach ($violations as $delta => $violation) {
            // Remove any violation that does not correspond to the constraints.
            if (!in_array($violation->getConstraint(), $constraints)) {
                $violations->remove($delta);
            }
        }
        // If a value has no violations then the requirement is satisfied.
        if (!$violations->count()) {
            return TRUE;
        }
    }
    return FALSE;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal