access the node id of a field collection How do I access the node id of a d8 field collection that is attached to the node in  hook_preprocess_field(&amp;$variables)?
It works for me. You can do it as follows-

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

Hope it works for you too.