Created
November 19, 2021 14:03
-
-
Save levmyshkin/7ca3fff3700211438eac4b99742abb8e to your computer and use it in GitHub Desktop.
Drupal Working with Paragraph fields programmatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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