Check of $value not covering LinkItem cases in link contraint validators

Created on 13 September 2023, over 1 year ago
Updated 15 January 2024, 11 months ago

Problem/Motivation

In certain cases, the $value parameter passed into the validators inside core/modules/link/src/Plugin/Validation/Constraint seems to be an instance of LinkItemInterface. All these validators check if the value is actually set using the following condition:

  public function validate($value, Constraint $constraint) {
    if (isset($value)) {

In the case of an LinkItem with an empty value of its uri main property this check above will pass, even though the LinkItem->getEmpty() would return FALSE. This is weird and will lead to notices like the following in case the a LinkItem with an empty uri is passed in because of the $value->getUrl(); statement further below for example in \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator::validate():

Deprecated function: preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated in Drupal\Core\Url::fromUri() (line 281 of core/lib/Drupal/Core/Url.php).

This is sort of related to the discussion in https://www.drupal.org/project/drupal/issues/3262935#comment-15005255 ๐Ÿ“Œ Link field validation constraints don't give enough detail Needs work

Steps to reproduce

See steps in https://www.drupal.org/project/drupal/issues/3348390#comment-15228424 ๐Ÿ“Œ Improve the way entity forms are "disabled" early when an entity is being edited in a workspace Needs work as long as that patch is not applied/committed.

  • Install the workspaces module
  • Add a link field to the page content type, cardinality unlimited, standards for other settings
  • Create a content of type page, adding a link with values and clicking the "Add another item" button not populating the 2nd link item
  • Save the node and re-open the edit form

OR

  • Go to admin/config/development/logging, select 'All' and save.
  • Add a link field with Unlimited cardinality to article content type.
  • Create a new article, add one link and save
  • Add a form after to article edit form
  • Load the node and call validate() method.[$entity = $form_state->getFormObject()->getEntity(); $entity->validate();]
  • Now try to edit the previously created node.

Proposed resolution

Check if the value passed in is LinkItemInterface instance and if that is empty skip the validation.

๐Ÿ› Bug report
Status

Needs work

Version

10.1 โœจ

Component
Linkย  โ†’

Last updated 3 days ago

Created by

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024