0

Within a page template, how could I check if a paragraph field if present?

I'm using this and it works:

$node->hasField($field_paragraphfield)

But how can I modify this to look for a field within that field?

I tried this but it doesn't work:

$node->hasField($field_paragraphfield_banner)
2
  • A node's paragraph field can hold multiple different paragraph entities (a paragraph is a full scale entity, not just a simple field. The node's paragraph field is just a pointer to that entity). And each of those paragraph entities can have a completely different field structure, e.g. the first might not have the field, but the third and the fourth might have this field.
    – Hudri
    Commented Feb 5 at 14:10
  • To check the field on the first entity, use $paragraph = $node->get('field_paragraph')?->entity; if ($paragraph?->hasField('field_banner')) {...}
    – Hudri
    Commented Feb 5 at 14:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.