Block config not always of the same type in block PHP section
Context :
a block that define a config according to the documentation.
Case 1 :
Block added to the page, nothing changed in content, open/close the config inspector, page saved.
The config is available in the PHP section of the block as a sdtClass.
{
+"size": ""
+"bg_color": ""
}
Case 2 :
Block added to the page, nothing changed in content, page saved.
The config is available in the PHP section of the block as an array.
array:2 [
"size" => null
"bg_color" => null
]
Workaround :
Strictly typing the configuration,
As an array :
$config = json_decode(json_encode($this->config), true);
As an stdClass :
$config = json_decode(json_encode($this->config));
Can we have a PR that just always converts it to either a stdClass or an array?
Can we have a PR that just always converts it to either a stdClass or an array?
It's on my to-do list ... but I'm running out of time.
A PR draft to add sorting on relationships has already taken up a lot of my time, but I'm getting there I think.
Which type do you think is best? @bennothommo, perhaps you have an opinion on the subject?
Since configs are ultimately stored through Halcyon (ie. the static content files in the theme), I think it should be an array for the best compatibility with that.
Block configs do not have to be used only with static pages, so I wouldn't necessarily use that as the deciding factor @bennothommo unless it actively causes issues to be in an stdClass form.
But yes, for simplicities sake I would prefer they were stored as arrays.
@LukeTowers @bennothommo Why not use the Illuminate\Support\Fluent class?
It provides access to data in two ways:
use Illuminate\Support\Fluent;
$dataArray = ['color'=> 'red', 'size'=> null];
$dataStdClass = (object) $dataArray;
// From an array
$dataFluent = new Fluent($dataArray);
$dataFluent['size']; // null
$dataFluent['color']; // red
$dataFluent->size; // null
$dataFluent->color; // red
// From an sdtClass
$dataFluent = new Fluent($dataStdClass);
$dataFluent['size']; // null
$dataFluent['color']; // red
$dataFluent->size; // null
$dataFluent->color; // red
@damsfx I'm happy to approve a PR adding that :)