Skip to content

Commit

Permalink
Fix defaults not being applied
Browse files Browse the repository at this point in the history
  • Loading branch information
RicLeP committed May 12, 2023
1 parent 945018e commit 5e0e8ce
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ class Block implements \IteratorAggregate, \JsonSerializable
/**
* @var Collection all the fields for the Block
*/
private Collection $_fields;
protected Collection $_fields;

/**
* @var Page|Block reference to the parent Block or Page
*/
private mixed $_parent;
protected mixed $_parent;

/**
* @var array default values for fields
Expand Down Expand Up @@ -94,13 +94,21 @@ public function __construct($content, $parent = null)
}

/**
* Returns the containing every field of content
* Returns the every field of content
*
* @return Collection
*/
public function content(): Collection
{
return $this->_fields;
$fields = $this->_fields;

foreach ($fields as $key => $field) {
if ($field === null) {
$fields[$key] = $this->_defaults[$key] ?? null;
}
}

return $fields;
}

/**
Expand Down Expand Up @@ -289,12 +297,12 @@ public function __get($key) {
return $this->$accessor();
}

if ($this->has($key)) {
return $this->_fields[$key];
if (array_key_exists($key, $this->_defaults) && $this->has($key) && !$this->_fields[$key]) {
return $this->_defaults[$key];
}

if (array_key_exists($key, $this->_defaults)) {
return $this->_defaults[$key];
if ($this->has($key)) {
return $this->_fields[$key];
}

return null;
Expand Down

0 comments on commit 5e0e8ce

Please sign in to comment.