0

I am creating a Rules Action that will update the node title after it is created.

Here is my code, but when I try to create new content using Postman, it keeps loading. I dont get response in Drupal logs.

...
class ContentTitle extends RulesActionBase {

  protected function doExecute(EntityInterface $entity) {

    $entity_id = $entity->id(); // Getting dynamic entity ID
    $controller = \Drupal::entityManager()->getStorage('node');
    $node = $controller->load($entity_id);

    $title = $entity->label();
    $item = $node->get('field_xyz')->value;
    $new_title = "Title is " . $title . $item;

    \Drupal::logger('custom_rules')->notice('new_title: ' . $new_title);

    $data = array(
      'type' => 'custom_content',
      'title' => [
        'value' => $new_title,
      ]
    );
    $node = Drupal::entityManager()
      ->getStorage('node')
      ->create($data);
    $node->save();
  }
}

Im getting the expected output in the logger.

I dont think I am setting the title in $data properly. Any suggestion is greatly appreciated. Thanks!

0

You can create node like below also:

use \Drupal\node\Entity\Node;

class ContentTitle extends RulesActionBase {

  protected function doExecute(EntityInterface $entity) {
    $entity_id = $entity->id(); // Getting dynamic entity ID
    $controller = \Drupal::entityManager()->getStorage('node');
    $node = $controller->load($entity_id);
    $title = $entity->label();
    $item = $node->get('field_xyz')->value;
    $new_title = "Title is " . $title . $item;
    \Drupal::logger('custom_rules')->notice('new_title: ' . $new_title);
    $node = Node::create([
      'type' => 'content_type',
      'title' => $new_title,
    ]);
    $node->save();
  }
}

In your code "title" should be only $new_title not in the array as value. Even your code also looks fine except that title. I didn't any try of your code but I am suggesting to use above one.

  • Thanks for your reply. Im getting 172.18.0.1 - - [19/Jun/2018:10:11:13 +0000] "-" 408 - "-" "-" in my access log – John Rey Tanquinco Jun 19 '18 at 10:15
0

Simple answer from https://drupal.stackexchange.com/a/249771/70281 did the trick for me.

$node->set('title', $new_title);
$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.