Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
damsfx opened this issue Nov 14, 2023 · 6 comments
Open

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

damsfx opened this issue Nov 14, 2023 · 6 comments

Comments

@damsfx
Copy link
Contributor

damsfx commented Nov 14, 2023

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));
@LukeTowers
Copy link
Member

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

@damsfx
Copy link
Contributor Author

damsfx commented Nov 15, 2023

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?

@bennothommo
Copy link
Member

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.

@LukeTowers
Copy link
Member

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.

@damsfx
Copy link
Contributor Author

damsfx commented Nov 15, 2024

@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 = $dataArray;

// From an array
$dataFluent = new Fluent($dataArray);

$dataFluent['size'];     // null
$dataFluent['color'];    // red
$dataFluent->size;       // null
$dataFluent->color;      // red

// From an array
$dataFluent = new Fluent($dataStdClass);

$dataFluent['size'];   // null
$dataFluent['color'];  // red
$dataFluent->size;     // null
$dataFluent->color;    // red

@LukeTowers
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants