How do I access the node id of a d8 field collection that is attached to the node in hook_preprocess_field(&$variables)?

  • If $variables contains already loaded Field Collection Item, then did you tried doing $fc->getHostId(), this should return the parent Node ID. Assuming that $fc is your Field Collection Item object. – Yogesh Dec 13 '16 at 4:59

It works for me. You can do it as follows-

/** * Function hook_preprocess_field. */ function [theme_name]_preprocess_field(&$variables) { if ($variables['field_name'] == [FIELD_COLLECTION_NAME]) { if ($variables['element']['#entity_type'] == "node") { $node_id = $variables['element']['#object']->id(); //Rest of the code. } } }

Hope it works for you too.

| improve this answer | |

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.