Score:-1

How do I set a plain text field value to be empty?

to flag
<?php
function MY_MODULE_node_update(NodeInterface $node)
{
    if ($node instanceof \Drupal\node\NodeInterface) {

        $nid = $node->id();
        $nodeType = $node->getType();
    
        $vid = \Drupal::entityTypeManager()
                ->getStorage('node')
                ->getLatestRevisionId($nid);

        $comments = $node->get('field_workflow_comment')->getValue();

        foreach($comments as $comment){
            $revision_log = ($comment['value']);
        }

        // $empty = array();
        // $commentEmpty = $node->set('field_workflow_comment', $empty);
        
        $node->field_workflow_comment = [];

        $timestamp = REQUEST_TIME;

        $connection = Database::getConnection();

        $add_revision_log = $connection->update('node_revision')
                            ->fields([
                                'revision_log' => $revision_log
                            ])
                            ->condition('nid', $nid)
                            ->condition('vid', $vid)
                            ->condition('revision_timestamp', $timestamp)
                            ->execute(); 
        $node->save();
     
    }
}

This does not work for me on hook_node_update()

Thanks in advance

Score:0
de flag

You can simply set the field to an empty array:

$node->field_name = [];
$node->save();
Sundar Raj Ponnulingam avatar
to flag
Hi Jaypan, but while I try to save this is the error message i get Drupal\Core\Entity\EntityStorageException: Update existing 'node' entity revision while changing the revision ID is not supported. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 815 of core\lib\Drupal\Core\Entity\Sql\SqlContentEntityStorage.php). how to solve this ?
Jaypan avatar
de flag
Can't tell, need to see more of your code.
Sundar Raj Ponnulingam avatar
to flag
Updated kindly check jaypan
Jaypan avatar
de flag
You're trying to update the node, in a hook that is called when the node is updated. This will cause an infinite loop.
Sundar Raj Ponnulingam avatar
to flag
Thanks Jaypan, Understood
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.