fix(portalblocks): handle null/unset rows in sparse layout arrays#69
fix(portalblocks): handle null/unset rows in sparse layout arrays#69ralflang merged 1 commit intoFRAMEWORK_6_0from
Conversation
When block layout arrays have gaps (sparse arrays), count() returns lastindex+1 ignoring omitted indices and this causes all kinds of errors. isset() and is_array() checks before calling count() fix this now.
| if (!isset($this->_layout[$row]) || !is_array($this->_layout[$row])) { | ||
| continue; | ||
| } | ||
|
|
There was a problem hiding this comment.
I am not sure this is the right thing to do.
If I undestand correctly, the _layout array can have non-consecutive indexes. And we want to go over each one that exists.
But the for loop only goes up to count($this->_layout), which could miss some indexes.
Example:
$this->_layout = [ 0 => $value1, 10 => $value2 ];in this case $rows = 2 and the for loop would only check row 0 and 1, but not row 10.
Possible solutions:
- use
foreach, need to redesign "if (isset($field['height'])) {" block as it refers to other rows (could they also be non-consecutive?) - alternatively, avoid sparse arrays or make it non-sparse in the constructor?
| @@ -93,6 +93,11 @@ | |||
| $emptyrows = []; | |||
|
|
|||
There was a problem hiding this comment.
Drop $emptyrows (looks like it is assigned but never used).
| if (!isset($this->_layout[$row]) || !is_array($this->_layout[$row])) { | ||
| return true; | ||
| } | ||
|
|
There was a problem hiding this comment.
This should be moved to rowExists method (it has to be redesigned as it does not work correctly for sparse arrays).
| public function rowExists($row) | ||
| { | ||
| return $row < count($this->_layout); | ||
| } |
There was a problem hiding this comment.
Possibly change to this to support sparse _layout array:
return isset($this->_layout[$row]) && is_array($this->_layout[$row]);And then it can be used in the contructor.
When block layout arrays have gaps (sparse arrays), count() returns
lastindex+1 ignoring omitted indices and this causes all kinds of errors.
isset() and is_array() checks before calling count() fix this now.