- Merge request !21feat: #3570441 Add Explicit Type Casting in Primary Item Sort Comparison β (Merged) created by bluegeek9
The preSave() method in PrimaryEntityReferenceFieldItemList uses the spaceship operator (<=>) to sort items by their 'primary' property. While Drupal's typed data system should enforce the correct boolean type, adding explicit type casting provides defense in depth and makes the code's intent clearer.
Location: src/Plugin/Field/PrimaryEntityReferenceFieldItemList.php, lines 32-34
public function preSave() {
usort($this->list, function ($a, $b) {
return $b->get('primary')->getValue() <=> $a->get('primary')->getValue();
});
parent::preSave();
}
"0" (string) and 0 (integer) to compare differently than expectedLow - Drupal's typed data system enforces types and the 'primary' property is defined as a boolean in the field schema. However, explicit type casting improves code robustness and clarity.
Add explicit type casting to ensure consistent integer comparison regardless of the actual data type stored.
public function preSave() {
usort($this->list, function ($a, $b) {
// Explicit casting ensures consistent comparison
return (int) $b->get('primary')->getValue() <=> (int) $a->get('primary')->getValue();
});
parent::preSave();
}
(int) type casting to both sides of the spaceship operatorNone
None. This is an internal implementation detail that doesn't affect the public API.
The behavior should remain unchanged for correctly typed data. If any edge cases existed where incorrect types were being compared, those will now be handled consistently.
None
Active
1.0
Code
Not all content is available!
It's likely this issue predates Contrib.social: some issue and comment data are missing.