verbb / vizy

A flexible visual editor for Craft CMS
Other
44 stars 8 forks source link

How can I modify a field within a node value programatically in a module? #313

Closed bleepsandblops closed 2 weeks ago

bleepsandblops commented 2 weeks ago

Question

I'm hoping to modify some fields within a Vizy node programatically. See below what I've tried, it's just copying the value of a dropdown into another dropdown:

        $director = Entry::find()->section('directors')->slug('XXX')->one();
        $newContentSuper = $director->contentSuper;
        foreach ($newContentSuper->all() as $row) {
            foreach($row->blocks->all() as $node) {
                if ($node->handle == 'project') {
                    $newContentSuper->set
                    $node->setFieldValue('sizeProject', $node->size->value);
//                    $node->sizeProject->value = $node->size->value;
                }
            }
        }
// save
        $director->setFieldValue('contentSuper', $newContentSuper);
        Craft::$app->elements->saveElement($director);

Am I going anywhere with this?

Thank you!

Additional context

Vizy 2.1.9 Craft 4.10.0

engram-design commented 2 weeks ago

It's honestly probably a tad overkill to do all this for Vizy, which after all is just JSON data. The only real benefit of doing things this way is for fields to be able to dictate how to save content, but if you're just looking to copy data over from one field to another, that's pretty basic for Vizy.

Assume you have the following content for your Vizy field:

[
    {
        "type":"vizyBlock",
        "attrs":{
            "id":"vizy-block-7BQx04FPZ5",
            "enabled":true,
            "collapsed":false,
            "values":{
                "type":"new31-8105",
                "content":{
                    "fields":{
                        "myDropdownField":"some-value"
                    }
                }
            }
        }
    }
]

It really is a matter of just adding the content to the other Dropdown field in your Vizy Block Type definition.

bleepsandblops commented 2 weeks ago

@engram-design thanks for getting back so quickly. I'm not sure I follow, are you saying I should go in the db and manually modify the JSON? Because I need to do this for dozens of entries.

NB: If the context is relevant, it's because the client wants to change the default value for "original dropdown" but I'm using that dropdown in other types of nodes where I don't want to change the default.

engram-design commented 2 weeks ago

You can still do what you're doing programmatically, I'm just saying you don't need to dive into the individual Vizy Block fields. It's note quite clear from your example what the Vizy field is, but something along the lines of:

$entry = Entry::find()->one();

$vizyContent = $entry->vizyFieldHandle->getRawNodes();

// Make your changes to the JSON content
// ...

$entry->setFieldValue('vizyFieldHandle', $vizyContent);

Craft::$app->getElements()->saveElement($entry);
bleepsandblops commented 2 weeks ago

@engram-design thank you, my Vizy field is within a Supertable block (in my example above contentSuper is the Supertable block and blocks is my Vizy field) so am trying to get this to work like in your example but struggling a bit.

EDIT: realised I can save the super table block directly like an entry and it seems to work thank you

engram-design commented 2 weeks ago

Ah, yes then if it's a Super Table (or Matrix), you've just got an extra step where you get the content on the block, and save the block instead of the entry itself, as that's the "owner" of the content.