0

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) {
      continue;
    }
    $node->set('field_tags', $new_tid);
    $node->save();

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

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

| improve this answer | |

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.