0

I have a content type with reference field of type paragraph.

Using below code only gives me fields of content type and does not give referenced fields. How can I get referenced fields of a particular node type?

function hook_form_alter(array &$form, FormStateInterface &$form_state, $form_id) {
    $entityManager = \Drupal::service('entity.manager');
    $fields = $entityManager->getFieldDefinitions('node','media');
}
1

1 Answer 1

0

Once you have a $node object, you can get the referenced field value by the following code:

$node->get('field_reference')->referencedEntities();

Here, field_reference is a field of type Entity reference revisions referencing to the paragraph entity.

4
  • How to get $node object? Commented Aug 8, 2017 at 10:50
  • @SugandhKhanna Depends on where you write this code. Can you add more details to the question stating where (function?) you need the referenced entities.
    – AjitS
    Commented Aug 8, 2017 at 10:52
  • edited, need referenced nodes in hook_form_alter and need to alter referenced nodes to be required=True. Commented Aug 8, 2017 at 10:56
  • And I can't load the node for this, because I need to alter referenced fields at the time of adding a node not at edit. Commented Aug 8, 2017 at 11:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.