I have saved a node programmatically, in the following way:

  $node_storage = \Drupal::entityManager()->getStorage('node');
  foreach ($entity_ids as $id) {
    $node = $node_storage->load($id);
    if (!$node) {
    $node->set('field_tags', $new_tid);

After saving the node I am trying to edit it and I am getting the follow error upon submit.

InvalidArgumentException: The state '' does not exist in workflow 'draft' in Drupal\workflows\Entity\Workflow->getState()

I believe that this means that my node has no state.

How could I set the node's state?

1 Answer 1


There's a code for creating a node and indicating its status, maybe it would help:

<?php // add node properties $newNode = (object) NULL; $newNode->type = '{NODE_TYPE}'; $newNode->title = '{NODE_TITLE}' $newNode->uid = {USER_ID}; $newNode->created = strtotime("now"); $newNode->changed = strtotime("now"); $newNode->status = 1; $newNode->comment = 0; $newNode->promote = 0; $newNode->moderate = 0; $newNode->sticky = 0;

// add CCK field data $newNode->field_{YOUR_CUSTOM_FIELD_1}[0]['value'] = '{DATA_1}'; $newNode->field_{YOUR_CUSTOM_FIELD_2}[0]['value'] = '{DATA_2}';

// save node node_save($newNode); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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