Skip to content

Instantly share code, notes, and snippets.

@levmyshkin
Created November 19, 2021 14:03
Show Gist options
  • Save levmyshkin/7ca3fff3700211438eac4b99742abb8e to your computer and use it in GitHub Desktop.
Save levmyshkin/7ca3fff3700211438eac4b99742abb8e to your computer and use it in GitHub Desktop.
Drupal Working with Paragraph fields programmatically
<?php
$my_paragraph = null;
foreach ($node->get('field_paragraph_reference') as $paragraph) {
if ($paragraph->entity->getType() == 'your_paragraph_type') {
$my_paragraph = $paragraph->entity;
}
}
if (!empty($my_paragraph)) {
print $my_paragraph->field_somefield->value;
print $my_paragraph->title->value; // It doesn't work!
}
else {
print "The node doesn't have this paragraph type.";
}
//Getting the type of paragraph:
$my_paragraph->getType();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment