Problem/Motivation
I created a node with a PER field, and set two values on it.
In PHP code, I load the node and iterate the field like this:
foreach ($node->field_test_per as $value) {
dsm($value);
}
This outputs 3 items, instead of the expected 2.
The last item has 'target_id' of 0 and primary of 0.
This is stored in the DB too. I can reproduce this by saving a host node with the checkboxes widget -- a DB row is made in the field table for EVERY item in the widget, not just the selected ones.
Steps to reproduce
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
Comments
Comment #2
joachim commentedComment #3
bluegeek9 commentedComment #6
bluegeek9 commentedComment #8
joachim commentedThanks for the quick fix!
if (!isset($value['target_id']) || (string) $value['target_id'] === '' || (int) $value['target_id'] === 0) {This line would be a lot simpler as a check for empty().
Comment #9
joachim commentedAlso, I don't think there's a case where you'd get a value of 0 for the entity ID?
Comment #11
bluegeek9 commentedI guess when it is blank.
Comment #14
joachim commentedThe case $value['target_id']= ''; was already covered by the cast to (string).
Can I ask if you're using an LLM for working on this module's code?