Loading a paragraph field entity for use within node.html.twig.
In order to load a field in a paragraph referenced entity, we need to first load the referenced entities
$my_paragraphs = $variables['node']->my_paragraph_field_name->referencedEntities();
Then, we look through the paragraphs to get the correct language, and in turn, pull the field from the paragraph.
foreach ($my_paragraphs as $paragraph) {
// Remember to check if translation exists
if ($stock->hasTranslation($variables['node']->language()->getId())) {
$paragraph = $paragraph->getTranslation($variables['node']->language()->getId());
}
$url = $paragraph->get('the_retrieved_field_from_the_paragraph')->getValue();
}
The full option within my preprocess_node function to pull a url that was living in a paragraph's entity reference.
/**
* Implements template_preprocess_node().
*/
function MYTHEME_preprocess_node(&$variables) {
if ($variables['view_mode'] == 'VIEW_MODE' && $variables['node']->bundle() == 'NODE_TYPE') {
// Paragraphs/referencedEntities
$paragraphs = $variables['node']->MY_REFERENCED_PARAGRAPH_FIELD->referencedEntities();
foreach ($paragraphs as $paragraph) {
// Remember to check if translation exists
if ($paragraph->hasTranslation($variables['node']->language()->getId())) {
$paragraph = $paragraph->getTranslation($variables['node']->language()->getId());
}
$url = $paragraph->get('MY_LINK_FIELD_IN_THE_PARAGRAPH')->getValue();
}
if (!empty($url)) {
$variables['new_url'] = Url::fromUri($url[0]['uri']);
}
}
}
Once that code is within the preprocess function, we can then output the URL in our node.twig.html file with: {{ new_url }}