Be sure to checkout our newest add-on, Speedy — Advanced Caching for ExpressionEngine.

Compatibility updates:

  • Publisher is EE5 compatible, but it does not currently support the Fluid field (it's in the works).

Ticket: Saving to Bloqs field with ChannelEntry model

Status Resolved
Add-on / Version Bloqs 4.1.1
Severity
EE Version 5.3.0

Ruarri Serpa

Nov 12, 2019

Hello, Looking for some advice on how to save data to a bloqs field (ultimately an ansel image),

I’ve got the following, trying to copy what is submitted when an entry is saved through the CP (field_id_50 being my Bloqs field):

$entry = ee('Model')->get('ChannelEntry', 1747)->fields('field_id_50')->first();
$row = [
           'field_id_50' => [
               'blocks_new_block_1' => [
                   'blockdefinitionid' => 8,
                   'order' => 1,
                   'draft' => 0,
                   'values'=> [
                       'col_id_14' => 'hero--simple',
                       'col_id_13' => [
                           'placeholder' => 'placeholder',  
                       ]
                   ]
               ],
               'tree_order' => [ 
                   (object)array(
                    "id" => "blocks_new_block_1",
                    "name" => "Hero",
                    "definition_id" => 8,
                    "parent_id" => null,
                    "parent_definition_id" => null,
                    "depth" => 0,
                    "lft" => 1,
                    "rgt" => 2)
                ],
                'blocks_new_block_0' => [ 
                    'values' => ['col_id_9' => null],
                    'blockdefinitionid' => 6,
                    'order' => 2,
                    'draft' => 0,
                    'values' =>[
                        'col_id_14' => 'hero--simple'
                    ],
                    'values' =>['col_id_13' => [
                        'placeholder' => 'placeholder'
                        ]
                    ],
                    'blockdefinitionid' => 8,
                    'order' => 3,
                    'draft' => 0,
                    'values' =>[
                        'col_id_2' => [
                            'placeholder' => 'placeholder'
                        ]
                    ],
                    'blockdefinitionid' => 2,
                    'order' => 4,
                    'draft' => 0,
                    'values' =>['col_id_4' => null],
                    'values' =>['col_id_11' => 1],
                    'blockdefinitionid' => 4,
                    'order' => 5,
                    'draft' => 0,
                ]
           ]
       ];
$entry->set($row);
$entry->save();

I get no errors, but no Bloqs row is created.

#1

BoldMinded (Brian)

Nov 14, 2019

Hi, Ruarri. I haven’t tried anything like that so I’m not sure if I can help.

#2

BoldMinded (Brian)

Nov 14, 2019

Using the model isn’t going to work, there is a lot more processing that Bloqs does to save data. Your best bet is to mimic what happens at lines 348-365 of ft.bloqs.php, https://d.pr/i/A70nOD, which is instantiating the PublishController and then calling save().

#3

BoldMinded (Brian)

Nov 14, 2019

Or, creating a new instance of Bloqs_ft and calling post_save() directly. Either method is untested, but it might work.

#4

Ruarri Serpa

Nov 15, 2019

Thanks for those suggestions. Stepping back though, my goal is just to write a script to import entries from our EE2 site to a fresh EE5. Are there better ways to import content into Bloqs fields?

#5

BoldMinded (Brian)

Nov 15, 2019

Not that I know of.

If you had Blocks installed in EE 2, it should migrate to EE 5 just fine, Blocks for EE 2 and Bloqs for EE 5 have the same data structure.

#6

Ruarri Serpa

Nov 15, 2019

Ah - well, EE2 didn’t have Bloqs.

#7

BoldMinded (Brian)

Nov 15, 2019

It did, it was just called Blocks at the time, not Bloqs, which is EE 4+

#8

Ruarri Serpa

Nov 15, 2019

Sorry - I meant our EE2 install didn’t. I’m trying to go from a single File field to a Bloqs field with this upgrade.

Looks like Bloqs_ft is necessary even to use the PublishController, unless I’m missing something. I’m a little out of my element with this, so I’m not even sure how to instantiate Bloqs_ft from my script.

#9

Ruarri Serpa

Nov 18, 2019

Turns out you can do this with the model!*

*As long as there is no row in the “exp_channel_data_field_XX” table for the entry. Not sure why it doesn’t get removed when deleting all bloqs for the field in the Control Panel, but removing it from the table allows me to run the above. Challenge now is to figure out how to remove it before importing/updating hundreds of new entries.

Looking in ft.bloqs.php and PublishController.php, I saw I could drop the whole “blocks_new_block_0” property altogether.

Progess…

#10

BoldMinded (Brian)

Nov 21, 2019

Good to hear! If you get a working solution maybe post it back to this ticket in-case someone else wants to do the same thing?

Login to reply

Contact

For add-on support, please use the Support section. General inquries and pre-sale questions can be sent to support@boldminded.com.