Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
If I use Entity argument validator with custom entity without bundles I got the following warning:
Warning: count(): Parameter must be an array or an object that implements Countable in Drupal\views\Plugin\views\argument_validator\Entity->validateEntity() (line 203 of core/modules/views/src/Plugin/views/argument_validator/Entity.php).
Drupal\views\Plugin\views\argument_validator\Entity->validateEntity(Object) (Line: 180)
Drupal\views\Plugin\views\argument_validator\Entity->validateArgument('22522') (Line: 999)
Drupal\views\Plugin\views\argument\ArgumentPluginBase->validateArgument('22522') (Line: 1034)
Drupal\views\Plugin\views\argument\ArgumentPluginBase->setArgument('22522') (Line: 1100)
Drupal\views\ViewExecutable->_buildArguments() (Line: 1264)
Drupal\views\ViewExecutable->build() (Line: 390)
Drupal\views\Plugin\views\display\PathPluginBase->execute() (Line: 180)
Drupal\views\Plugin\views\display\Page->execute() (Line: 1627)
Drupal\views\ViewExecutable->executeDisplay('page_users', Array) (Line: 77)
Drupal\views\Element\View::preRenderViewElement(Array)
call_user_func(Array, Array) (Line: 378)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 226)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 227)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 156)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 664)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Comment | File | Size | Author |
---|---|---|---|
#15 | entity-argument-validator-fix-php72-2969262-15.patch | 2.17 KB | FeyP |
#15 | entity-argument-validator-fix-php72-2969262-15-test-only.patch | 851 bytes | FeyP |
Views-entity-argument-validator.png | 42.97 KB | BlacKICEUA |
Comments
Comment #2
BlacKICEUAProposed solution.
Comment #3
cilefen CreditAttribution: cilefen as a volunteer commentedComment #4
dawehnerI'm curious how it was possible that this value is empty in the first place? It feels like just adding some checks here might hide an underlying bug.
Once we do understand the underlying bug, maybe this patch is exactly though the right solution.
Comment #5
BlacKICEUAAs I think, this piece of code produce this behavior (Drupal\views\Plugin\views\argument_validator\Entity):
So we haven't element and that's why we haven't any values in the 'submitOptionsForm' for 'bundles' form element.
Comment #6
dawehnerI fear some level of tests are needed here to resolve the issue.
Comment #7
bkosborneI ran into this when testing an upgrade to PHP 7.2 as well. This is indeed a minor bug in the views argument validator for entities. This validator works by allowing you to validate that an argument provided is an entity of a specific type. Additionally, if the entity type you're validating has bundles, it allows you to validate that the passed in argument matches one of the bundles.
The problem occurs when you add the validator for an entity type that doesn't have bundles. In that case, the "bundles" form option is not used, and when the form is submitted, it has no value. But the submit handler still tries to set a value:
In this case, $options['bundles'] is NULL (key does not exist), so array_filter also returns NULL, so it saves NULL for the option.
We can fix that submit callback so that it doesn't save a NULL value and instead saves an empty array. That will fix it for people that re-save the argument config form or create a new argument, but doesn't help existing argument plugin configs. For that we could write a database update hook, but that's seriously overkill IMO. We can just handle the NULL case gracefully.
The previous patch works, but we can combine is_array() and count() > 0 into just a simple !empty().
Comment #8
bkosborneComment #9
bkosbornesmall fix to comment.
Comment #11
joelpittetdoesn't
is spelled asdoens't
array_key_exists() can have a null value.
Consider
!empty($options['bundles'])
because it's faster than array_key_exists(), but maybe cleaner just to cast it to an(array)
?Comment #12
bkosborneAddressed feedback
Comment #13
oriol_e9gAll feedback addressed.
Comment #14
alexpottWe must be missing test coverage as we run nightly tests on PHP 7.2 (and 7.3 now). Given this is now causing warnings we need to add that test coverage here. As per @dawehner / #6.
Comment #15
FeyP CreditAttribution: FeyP as a volunteer and at werk21 commentedAttached is a patch against 8.8.x, which extends the existing unit test for the argument validator to cover this issue. No interdiff, since it's trivial.
Comment #17
FeyP CreditAttribution: FeyP as a volunteer and at werk21 commentedTest results as expected on PHP7.2. The test fail in PHP 7.1 looks suspiciously like another random unrelated test fail in ManageGitIgnoreTest, so let's try again.
Comment #18
FeyP CreditAttribution: FeyP as a volunteer and at werk21 commentedRetest came back green, filed #3070853: Race condition during composer cache clear in functional composer scaffolding plugin tests for the test fail.
Comment #19
bkosborneLooks good
Comment #21
FeyP CreditAttribution: FeyP as a volunteer and at werk21 commentedThe test fail was most likely caused by #3085697-31: Allow scaffold plugin to append to non-scaffolded files. Retest came back green. Setting back to RTBC per #19.
Comment #23
siva01 CreditAttribution: siva01 at Ciklum Western Europe for BurdaForward commentedPatch for Drupal 8.8 with PHP 7.3 doesn't work.
Comment #25
SpokjeRandom test fail, retest was green, back to RTBC per #19
Comment #26
alexpottCommitted and pushed 73b8d10170 to 9.0.x and 9ade85c270 to 8.9.x and 74a79f60aa to 8.8.x. Thanks!
Backported to 8.8.x as a low risk bugfix.
Comment #33
quietone CreditAttribution: quietone at PreviousNext commentedClosed #2834801: Schema validation fails on the "entity" argument_validator when the "bundles" form key is not set as a duplicate, adding credit and tagging.