Skip to content

Instantly share code, notes, and snippets.

@DuaelFr
Last active January 9, 2017 11:37
Show Gist options
  • Save DuaelFr/a03202e6e28dbc4838c0e95c23ae3c3a to your computer and use it in GitHub Desktop.
Save DuaelFr/a03202e6e28dbc4838c0e95c23ae3c3a to your computer and use it in GitHub Desktop.
Preset paragraph on node creation
<?php
use Drupal\node\Entity\Node;
/**
* Implements hook_ENTITY_TYPE_create().
*/
function my_module_node_create(Node $node) {
if ($node->bundle() == 'my_bundle') {
$field = $node->get('field_my_paragraph_field');
if ($field->isEmpty()) {
$paragraphs = \Drupal::entityTypeManager()->getStorage('paragraph')->create([
'type' => 'my_paragraph_type',
]);
$field->appendItem($paragraphs);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment