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');
}
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.

  • How to get $node object? – Sugandh Khanna Aug 8 '17 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 Aug 8 '17 at 10:52
  • edited, need referenced nodes in hook_form_alter and need to alter referenced nodes to be required=True. – Sugandh Khanna Aug 8 '17 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. – Sugandh Khanna Aug 8 '17 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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