wn-blocks-plugin icon indicating copy to clipboard operation
wn-blocks-plugin copied to clipboard

Block config not always of the same type in block PHP section

Open damsfx opened this issue 2 years ago • 4 comments

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));

damsfx avatar Nov 14 '23 15:11 damsfx

Can we have a PR that just always converts it to either a stdClass or an array?

LukeTowers avatar Nov 14 '23 15:11 LukeTowers

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?

damsfx avatar Nov 15 '23 15:11 damsfx

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.

bennothommo avatar Nov 16 '23 00:11 bennothommo

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 avatar Nov 16 '23 15:11 LukeTowers

@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 avatar Nov 15 '24 17:11 damsfx

@damsfx I'm happy to approve a PR adding that :)

LukeTowers avatar Nov 16 '24 09:11 LukeTowers