0

I have tried for some time to find out a way to populate a new page with a few paragraphs to save time on most of the pages (but which might change position or even get dropped for some pages) but so far haven't found a way to do.

I think I have found out how to do the paragraph adding part, but what I am missing is how to fire the code when a new node of relevant content type is created.

I have tried hook THEME_node_create and THEME_node_insert, but they seem not to be fired when a new node is requested. I read that not all hooks are implemented for a theme but how then to achieve this functionality?

I probably (as most of the times) am missing something trivial, but please, help me find the light.

wbr

hank

0

1 Answer 1

1

Actually I found a solution using a module. It was not the first time to find the solution after asking it here so it was not in vain - even if I found it myself...

To help possible otrhers, here is my solution from my module's .module file

function acme_pre_populate_node_create(\Drupal\node\NodeInterface $node) {
  $i=0;
  if ($node->getType() == 'acme_basic_page') {
    $paragraph = \Drupal::entityTypeManager()->getStorage('paragraph')->create([
        'title' => 'Paragraph ' . $i,
        'type' => 'hero_image',
      ]);
      $node->field_paragraph_list[] = $paragraph;
      $i++;
      $paragraph = \Drupal::entityTypeManager()->getStorage('paragraph')->create([
          'title' => 'Paragraph ' . $i,
          'type' => 'one_column_text',
          'field_one_column_text' => "<h1>Change title here</h1><p>And contents here</p>",
    ]);
        $node->field_paragraph_list[] = $paragraph;
       $i++;
  }
}

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