{"id":714,"date":"2022-08-30T15:05:57","date_gmt":"2022-08-30T15:05:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/field-collections-of-entity-references-in-views-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:05:57","modified_gmt":"2022-08-30T15:05:57","slug":"field-collections-of-entity-references-in-views-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/field-collections-of-entity-references-in-views-collection-of-common-programming-errors\/","title":{"rendered":"Field Collections of Entity References in Views-Collection of common programming errors"},"content":{"rendered":"<p>I have been tinkering with this problem and trying to research for a few hours. I am still getting familiar with the entity API for Drupal 7, and have been trying to work with PHP but to no avail&#8211;I think I am missing something VERY obvious as a result of my tired brain over-thinking things.<\/p>\n<p>I have two Content Types, CT1 and CT2. CT1 has a field that can hold multiple Field Collection values. In this Field Collection (field_coll_ct2) one of the fields (field_ct2_ref) is an Entity Reference to a node of content type CT2.<\/p>\n<p>Now, when I am looking at a single node of type CT1, I want a block View that will show me all of the CT2 nodes referenced in that CT1 node&#8217;s field_coll_ct2 field collection field.<\/p>\n<p>My primary approach, which has worked on non-field collection fields of the same kind of relation, is to add a contextual filter to the view on Content:NID. From there, since it is a block and URL parameter passing is out of the question (there are so many of these kinds of relations between 7-8 different content types, at different tuples per node, it is absolutely not feasible to use the URL for parameter-passing) I have it set to &#8216;Provide default value&#8217; of type &#8216;PHP&#8217; and &#8216;allow multiple values&#8217; and here is where it gets hairy.<\/p>\n<p>I need to get all of the field_ct2_ref entity references in the currently-viewed node&#8217;s field_coll_ct2 field collections. I&#8217;ve tried using:<\/p>\n<pre><code>$wrapper = entity_metadata_wrapper($entity_type, $entity);\nreturn $wrapper-&gt;field_coll_ct2-&gt;field_ct2_ref-&gt;value();\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>$node=menu_get_object(); \nforeach ($node-&gt;field_coll_ct2['und'] as $record) \n{ \n$values[]= $record['value'];\n} \nforeach($values as $val){\n$tgts[]=$val['every'][index]['possible'];\n}\nreturn $tgts[omg];\n<\/code><\/pre>\n<p>and probably close to a hundred variations of the above trying to get it to return something that vaguely resembled an entity reference to a CT2 type node.<\/p>\n<p>I can&#8217;t seem to dig deeply enough into the entity\/field references to get to the field_ct2_ref[&#8216;und&#8217;][0][&#8216;target_id&#8217;] that has worked for all non-field collection fields with this kind of relationship. Or I get AJAX errors telling me &#8216;Unknown data property field_coll_ct2&#8217; or console errors saying that every index I try to throw at these array objects (to even just get ONE of the values) is wrong.<\/p>\n<p>Is there an easier way of doing this? Am I missing something simple and obvious&#8211;either in the way I&#8217;m implementing my View or the PHP itself?<\/p>\n<p>For what it&#8217;s worth, I&#8217;ve been able to narrow down the view results without any contextual filter by selecting a relation to &#8216;Referencing Entity: field_ct2_ref&#8217; but it shows ALL CT2 nodes referenced by ANY CT1 type node rather than the specific CT1 type node I&#8217;m looking at.<\/p>\n<p>Thank you!<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:43:02. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have been tinkering with this problem and trying to research for a few hours. I am still getting familiar with the entity API for Drupal 7, and have been trying to work with PHP but to no avail&#8211;I think I am missing something VERY obvious as a result of my tired brain over-thinking things. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-714","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=714"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/714\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}