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.
Problem/Motivation
Render entity with Datetime Range field throws error:
Error: Unsupported operand types in Drupal\datetime_range\Plugin\Field\FieldFormatter\DateRangeDefaultFormatter->viewElements() (line 64 of core/modules/datetime_range/src/Plugin/Field/FieldFormatter/DateRangeDefaultFormatter.php).
Proposed resolution
Needs to initialize array before assignment.
Remaining tasks
N/A
User interface changes
N/A
API changes
N/A
Data model changes
N/A
Comment | File | Size | Author |
---|---|---|---|
#10 | interdiff-07-10.txt | 2.46 KB | mpdonadio |
#10 | 2811725-10.patch | 7.55 KB | mpdonadio |
#7 | 2811725-07.patch | 7.28 KB | mpdonadio |
datetime_range-unsupported_operand_types.patch | 856 bytes | iamdroid | |
Comments
Comment #2
mpdonadioCan you confirm what version of PHP you saw this with? We also need a test to demonstrate this error. If this is a problem we should also be seeing it with DateTimeDefaultFormatter and GenericFileFormatter.
Comment #3
iamdroid CreditAttribution: iamdroid commentedI catch this error on php 7.0.8.
I just added Datetime Range field to node type and set the value to some nodes. Then nodes with date range setted don't loading more.
In patch I added one line that has been missed:
In GenericFileFormatter It exists, and in DateTimeDefaultFormatter attributes array defines in block above.
Comment #4
mpdonadioWill look at this tonight. A bit baffled why this hasn't popped up with any of the automated testing since this was committed to 8.2 and 8.3.
Comment #5
mpdonadioOk, this isn't showing in tests up because with a standard profile w/o any extra modules, `$item->_attributes` is always empty, so the innards of the if never get executed.
This can probably be simplified to
since we aren't seeing the #attributes above, and $item->_attributes should be an array.
We are going to probably need a test to demonstrate bug and to prevent a regression (especially since this will be an area with upcoming refactoring).
Comment #6
mpdonadioKudos to @tim.plunkett for pointing me in the right direction here.
The test would essentially need to add to entity_test_entity_prepare_view() to add an attribute to a daterange field on entity_test entity, that we can then check in our own test. Wedging this into DateRangeFieldTest() isn't easy since it uses random machine names, so I think we want a separate test class for this (ideally a kernel test).
Comment #7
mpdonadioWe don't have a wrapper (yet) around daterange fields. So, this patch lets the $item->_attributes propagate to the field wrapper on proper ranges, but be on the item when start and end are the same.
This test change makes me cry a little, but the hook_entity_prepare_view() wasn't firing from DateTestBase::renderTestEntity().
Edit: this patch is best read applied. The actual change to DateRangeDefaultFormatter is a little hard to interpret just from the diff itself.
Comment #9
jhedstromCan a
@see entity_test_entity_prepare_view()
note be added here, so in the future it's easy to find the hook responsible for the data in these tests?I might be missing something here, but what changed that is now making it fire? (Edit, I see now--the changes to the test)
Comment #10
mpdonadio#9, good idea.
As a followup, and part of the long term plans we really need to refactor these tests (and the datetime ones) into unit/kernel/btb as necessary, and item/widgets/formatters. These are pretty messy right now, and they are way to big to manage.
Comment #11
jhedstromThis looks good to go. Refactoring tests can perhaps happen during the conversion/split from webtestbase to browsertestbase + kernel?
Comment #12
alexpottCommitted and pushed 36bc912 to 8.3.x and 1137253 to 8.2.x. Thanks!
Comment #15
iamdroid CreditAttribution: iamdroid commentedUnfortunately this commit doesn't fixes the issue.
I don't see any changes in
DateRangeDefaultFormatter.php
that changes behavior for#attributes
array, and$item->_attributes
still concatenates with undefined array.And this behaviour causes this error.
Actually I think, that this situation exist because this operation was missed during copy paste.
In GenericFileFormatter there is code for define array before concatention:
But in
DateTimeDefaultFormatter
it was removed, because it doing in block above.So in
DateRangeDefaultFormatter.php
there isn't that block and there isn't code for defining#attributes
array.And datetime_range-unsupported_operand_types.patch just fixes this.
Comment #16
mpdonadio#15, what version of Drupal are you using? This isn't in a point release yet, so you would have to be applying the patch manually or using -dev?
The relevant portion of applied-patch code is:
In the TRUE path of the if, we defer using the `$item->_attributes` inside the formatter itself since we won't have a wrapper; instead it will be output with the field template. This is covered in lines 146, 305, 466, and 544 of DateRangeFieldTest. In the FALSE path, the `$elements[$delta]['#attributes']` has been set already from `$this->buildDateWithIsoAttribute` (this sets the datetime attribute for the time element) and covered by lines 214 and 381 of DateRangeFieldTest. These six lines should cover the combinations of date+time, date-only, and all-date with start==and and start!=end for the default formatter.
If you are still having problems, please post a followup issue with as many details as possible (a failing test would be ideal), so we can see what we missed.
Comment #17
iamdroid CreditAttribution: iamdroid commentedSorry, I didn't noticed that current 8.2.x (8.2.1+87-dev) don't includes changes yet, and just download last build without applying patch manually.
Now all works fine. Thank You very much!