Problem/Motivation

ContextDefinition::isSatisfiedBy does not check if the definition is multi-valued. This can cause an error when checking violations.

Steps to reproduce

The following code produces an error.

$definition = EntityContextDefinition::create('node');
$definition->setMultiple();

$nodes = Node::loadMultiple([1, 1]);
$context = new \Drupal\Core\Plugin\Context\Context($definition, $nodes);

$satisfied = $definition->isSatisfiedBy($context);

Error: Call to a member function getEntityTypeId() on array in Drupal\Core\Entity\Plugin\Validation\Constraint\EntityTypeConstraintValidator->validate() (line 22 of /var/www/html/web/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/EntityTypeConstraintValidator.php).

Proposed resolution

Check $definition->isMultiple() inside of ContextDefinition::isSatisfiedBy and tweak how $validator->validate() runs.

Remaining tasks

Write a patch.

User interface changes

n/a

API changes

n/a

Data model changes

n/a

Release notes snippet

n/a

Issue fork drupal-3314469

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

alecsmrekar created an issue. See original summary.

immaculatexavier made their first commit to this issue’s fork.

alecsmrekar’s picture

StatusFileSize
new2.46 KB

Added patch

alecsmrekar’s picture

Status: Active » Needs review
plach’s picture

Looks good to me, posting a test-only version of the patch to verify that the test does provide coverage for the bug.

plach’s picture

plach’s picture

Version: 9.4.x-dev » 10.1.x-dev

Status: Needs review » Needs work

The last submitted patch, 5: 3314469_context_multiple_definition-4.test.patch, failed testing. View results

plach’s picture

Status: Needs work » Reviewed & tested by the community

Ok, test patch failing as expected!

Patch to commit at #3.

  • catch committed f225ff2 on 10.0.x
    Issue #3314469 by alecsmrekar, plach: ContextDefinition::isSatisfiedBy...
  • catch committed 6dcb915 on 10.1.x
    Issue #3314469 by alecsmrekar, plach: ContextDefinition::isSatisfiedBy...
  • catch committed 30e79f4 on 9.4.x
    Issue #3314469 by alecsmrekar, plach: ContextDefinition::isSatisfiedBy...
  • catch committed 23f2510 on 9.5.x
    Issue #3314469 by alecsmrekar, plach: ContextDefinition::isSatisfiedBy...
catch’s picture

Version: 10.1.x-dev » 9.4.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 10.1.x, cherry-picked back through to 9.4.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.