TransWikia.com

Set a boolean (single on/off checkbox) value while creating a node

Drupal Answers Asked on December 6, 2021

I am creating a node (in Drupal 8) with the following code.

use DrupalnodeEntityNode;

$node = Node::create([
            'type' => 'calendar_data',
            'title' => 'testing finaly 1111111111111111111111111 ',
            'field_current_month' => date('Y-m-d'),
            'field_date_type_value' => '1',
        ]);
$node->save();

The value of field_date_type_value is set to 0, not the expected value (1) I am using in the code.

2 Answers

Try

$node = Node::create([
        'type' => 'calendar_data',
        'title' => 'testing finaly 1111111111111111111111111 ',
        'field_current_month' => date('Y-m-d'),
        'field_date_type_value' => ['value' => '1'],
    ]);

$node->save();

Answered by Vernit Gupta on December 6, 2021

Sorry for late response, but I had the same problem few days ago. What worked for me was first of all creating the node and then setting the boolean value:

$node = Node::create([
        'type' => 'calendar_data',
        'title' => 'testing finaly 1111111111111111111111111 ',
        'field_current_month' => date('Y-m-d'),
    ]);
$node->field_date_type_value->value = true,

$node->save();

The 'inline' field setting didn't work neither for some other fields for me. But the explicit version is working very well.

Answered by Ju1yy on December 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP