Postponed (maintainer needs more info)
Project:
Drupal core
Version:
main
Component:
path.module
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
30 Aug 2017 at 02:29 UTC
Updated:
25 Nov 2025 at 14:26 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
tedbowHere is test that fails and shows the problem.
Comment #3
tedbowComment #4
tedbowOverrided
count()to callensureLoaded(). This changescount()to return 1 on an empty field before and afterisEmpty()call.Comment #6
wim leersComment #7
berdirfix makes sense I think, we should IMHO definitely also extend \Drupal\Tests\path\Kernel\PathItemTest which has a ton of test coverage around loading/changing computed values in the path field. Not sure if we still need a unit test then, feels a bit like we're testing an implementation detail there.
I could imagine that e.g. #2392845: Add a trait to standardize handling of computed item lists will change how the implementation looks like exactly, and then we'd need to update the unit test.
Comment #8
wim leers@Berdir++ — thanks for the feedback!
Comment #9
tedbowOk I added to PathItemTest.
The only that doesn't feel real intuitive to me is:
So the field is not empty after a save even though the count has not changed.
$node->get('path')->getValue()will equalso it is not empty because the field has language code.
Comment #11
amateescu commentedThis a big problem: for non-computed fields, the
count()of an empty field is 0, so it should be the same for computed ones as well.The problem here, as you mentioned in #9, is that
\Drupal\path\Plugin\Field\FieldType\PathItem::ensureLoaded()always sets the langcode property, even if there's no alias for that entity.So that's what we need to fix, IMO :)
Comment #12
tedbowAgreed!
The reason an empty PathItemList was returning 1 for count was because in
PathFieldItemList::ensureLoadedwe actually created an empty field item.
I tried to remove that all together but then PathItemTest would fail here:
Basically when you reset cache and load the entity with a path item:
$this->listinitially has no items.ensureLoaded()which initially be empty.PathFieldItemList::isEmptywill callensureLoaded()then callparent::isEmpty()ItemList::isEmptywhich will callPathItem::isEmptyon every item.PathItem::isEmptywill callPathItem::ensureLoadedwhich loads the alias value if it exists.This works great for entities that will have an alias value. But entities that do not have a alias item this leaves an empty PathItem in
PathFieldItemList::$list.This causes
count($node_with_no_path->get('path') == 1because there is an empty PathItem. But$node_with_no_path->get('path')->isEmpty() == TRUEbecauseisEmtpty:So the solutions is in
PathFieldItemList::ensureLoadedto immediately callisEmpty()on newly created PathItem which load the alias if it exists and remove the item if it is empty.This ensures that an empty PathItem will not remain in
$this->list[0]but it will contain the correct value if it does exist.Comment #13
berdirI expect/hope that this will break some tests because the path widget relies on having the correct langcode set. I'll look at this ASAP.
Comment #14
amateescu commentedInstead of doing this manually, we should simply call
$this->filterEmptyItems()at the bottom of the method.Unneeded spaces :)
Since the actual bug was that
count()was returning the wrong thing, I don't we need to test with various methods being called before other methods, a simple check thatcount()returns the expected value in every scenario (new node without any alias, new node with an alias, saved node without any alias, saved node with an alias) is enough.Comment #16
tedbow@Berdir yep forgot about that. I think we can rely on having an empty field item except for langcode though. This patch checks if langcode exists and uses the entity langcode if it does not.
This fixes the failing test for me locally.
#14.1 fixed.
2. fixed
3. actually the bug as reported in the title is that count behaves differently if isEmpty() has been called. So the bug did only happen if isEmpty was called before.
This code will show 2 different counts in HEAD
So it seems like it is good to have test make sure we don't accidently cause this to happen again.
Comment #17
amateescu commentedAs far as I see, it's not only about
isEmpty()being called before, it's about any method that would callensureLoaded()internally :) So the test could also try toget()the field item list instead of callingisEmpty(), maybe that would be a more reliable way to test the bug.. not sure.Comment #19
tedbowOk the only remaining failure is from the unit test this patch add PathFieldItemListTest but that is probably not needed anymore because we are adding this coverage to PathItemTest as suggested in by @Berdir in #7. The test failing because it was still expecting
count()to return 1 for an empty PathFieldItemList which we determined is not correct. So I am removing the test.#17 I am not sure what we should do here.
ensureLoaded()is called by 4 public functions on PathFieldItemList now so callingcount()before and after any of those functions would have resulted in a different value before this patch I think. The bug just happened to be caught onisEmpty. I don't think any of the methods is better or worse to test on. If we need the test at all.Comment #20
wim leers#9: that is indeed very confusing 🤔
#11++
#12: 😵
#17:
+1
#19:
Hm, I was thinking this too: what if we could remove all the tested permutations, and just test it once? So, basically what @amateescu is saying. I did that, it looks like the attached patch.
Comment #21
wim leersNit: Needs space before the ternary operator. Fixed.
As far as I'm concerned, this is ready, but I think only @amateescu or @Berdir can RTBC this.
Comment #24
amateescu commentedNote that I'm getting into the same problems with the path widget as #12 in #2916300-4: Use ComputedFieldItemListTrait for the path field type because that patch also fixes this issue :)
Also, I'm not qualified at all to RTBC the changes to the path widget, so we'll have to wait for @Berdir :)
Comment #25
wim leersComment #26
wim leersShould this be postponed on #2916300: Use ComputedFieldItemListTrait for the path field type (which is itself blocked on #2392845: Add a trait to standardize handling of computed item lists)? AFAICT it should.
@amateescu, what do you think?
Comment #27
amateescu commented@Wim Leers, yup, it would be good to wait a bit because those two issues will most likely fix this one as well.
Comment #28
wim leers👍
Comment #29
wim leersBoth #2916300: Use ComputedFieldItemListTrait for the path field type and #2392845: Add a trait to standardize handling of computed item lists are in. #21 no longer applied. Rebased.
But in rebasing, the conflicts were near 100% of the non-test changes. Only a single line change remains — all other changes have already been made in those two blockers. Which confirms @amateescu's prediction in #27 :)
However … then the test this issue was adding no longer works:
does not pass because
var_dump($node->get('path')->getValue());results in:This test should fail due to that reason.
The only possible explanation is that this patch's
PathFieldItemList::ensureLoaded()and/orPathItem::ensureLoaded()implementations were different from those implementations added in the blockers.Comment #30
wim leersThe root cause for #29:
This was being added in this issue, but not in the two blockers. Plus, #2916300: Use ComputedFieldItemListTrait for the path field type seems to have introduced incorrect logic for
PathItem::isEmpty(): the mere presence of thelangcodeproperty while the other two properties are absent is enough to make the field item "not empty". @amateescu in #2916300-8: Use ComputedFieldItemListTrait for the path field type specifically said it was up to this issue to fix this behavior.So, here's the intended fix AFAICT. Hopefully @amateescu can confirm :)
Comment #31
wim leersI doubt this change is necessary. Tests don't fail if I revert this change in any case.
Comment #33
amateescu commentedRe #31: Yes, that's not needed anymore, the change from #2916300-8: Use ComputedFieldItemListTrait for the path field type is the one that made it obsolete.
I don't think this is the correct fix. While I was working on the other issue I had a thought that we should move the default 'langcode' assignment to the item list class thus allowing the item class to behave normally (i.e. actually return 0 on
count()when the field is empty).Comment #35
wim leersI'm happy to let you decide how this should be fixed! You know far more about Entity/Field/Typed Data API than I do :)
Comment #47
smustgrave commentedThank you for reporting this problem. We rely on issue reports like this one to resolve bugs and improve Drupal core.
Since there has been no activity here for over 8 years we are asking if this problem persists on a currently supported version of Drupal. To help, add a comment explaining if the problem still occurs or not. Any extra detail you can provide can help others who experienced this.
Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.
Thanks!