1

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

use \Drupal\node\Entity\Node;

$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.

  • Are you sure it's called field_date_type_value and not field_date_type? – Clive Feb 19 '18 at 14:46
0

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.

  • It should be $node->field_date_type_value->value = true. =, not =>. – Keven May 13 at 20:29
0

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.