When using field collections with a module that deals with entities generically and therefore just uses the Entity API property metadata (like the Search API, for example), it is currently not possible to work with the host entity in a meaningful way, since the property has simply type entity. That means, no fields or properties will generally be available for the host entity, unless a specific field collection item is used. This restricts the functionality of those modules when working with field collections.

My suggestion for improving this situation would be to add new properties, like "Host entity (node)" (with the appropriate types, of course) for all entity types that have field collection fields. The property would contain the host entity for an item if it is of the specific entity type, and be empty otherwise.
This would, for example, solve the issues #2429355: Using and filtering field collections on nodes with facets and #2469621: Adding parent node to field collection indexes.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

drunken monkey’s picture

Status: Active » Needs review
FileSize
2.14 KB

Patch attached, please test/review!

splitsplitsplit’s picture

This worked correctly and allowed me to add host entity fields (specifically nids in my case) to the search api index, in my case using Search API with a solr backend.

Tested with Field Collection 7.x-1.0-beta8 so far!

JOINSO’s picture

Hi!

It doesnt work in my case.

I'm using:

Field collection: 7.x-1.0-beta8+11-dev
Search API: 7.x-1.15
Search facets: 7.x-1.15
Facet API: 7.x-1.5

When i applied the patch, i can add the field collection in my search index.
But when i add this field collection, the mapping of the fields are not correct.
In my case, i have this fields in this field collection:

Field A: Text
Field B: Long text
Field C: File
Field D: Term Reference A
Field E: Term Reference B

The "Term Reference A" is a standard taxonomy.
The "Term Reference B" has additional fields:
Field B.1: Boolean
Field B.2: Image

When I add the field collection to search index, this patch show this wrong combination:

Field Collection >> Field D (Term Reference A) >> Field B.2

Also if I use this combination:

Files » Field D » Name, the index return no entries.

If use this:

Files » Host entity (Content) » Field Collection » Field D » Name 1
it returns no data....

If I don't use the patch, I get this:

Field Collection >> Field D >> Name, and the index get this strange list of data:

and (197)
the (189)
for (181)
nbsp (154)
with (147)
this (135)
rhino (132)
you (131)
use (123)
can (117)
that (111)
com (102)
grasshopper (102)
file (93)
have (93)
please (92)
from (88)
install (84)
components (81)
not (78)
are (74)
your (72)
any (70)
.....

Any idea?

Regards,
JOINSO

mihai_brb’s picture

Why don't we add the host entities on bundle properties level. This way we have less and more relevant host entities in the UI (only the shared field collections).
Attached a patch that does this.

JOINSO’s picture

It doesn't work.

Which patches I need to apply? Only the last one?

mihai_brb’s picture

@JOINSO yes, the last one.
However as I don't like patching modules I've created a small sandbox that does the same via hook_entity_property_info_alter().
https://www.drupal.org/sandbox/mihai_brb/2510552

drunken monkey’s picture

+++ b/field_collection.info.inc
@@ -24,6 +24,24 @@ class FieldCollectionItemMetadataController extends EntityDefaultMetadataControl
+          'getter callback' => 'field_collection_item_get_host_entity',

Wouldn't this lead to errors when the host entity is of a wrong type? That's why I had the separate getter callback in my patch.

I don't completely understand the code changes you made, but adding the properties on the bundle level seems like a good idea, I agree. It won't de-clutter the UI in the Search API (as that pretty much ignores bundles), but it might of course help in other places.

@JOINSO: I'm not sure I follow. Are you indexing the field collections themselves or, e.g., nodes that contain field collections? In the latter case, the patch will be pretty useless, as it would just allow you to use a double-indirection to arrive at the original node you are indexing anyways.
Also, the problem you cite with the nested properties of the two term reference fields is a general problem of the Search API, nothing related to this patch.
If you are still having trouble with this, please try to explain it again (maybe with screenshots). Most important to know would be, as said, the "Item type" you've selected for your index.

JOINSO’s picture

Hi, drunken monkey!

I indexed nodes that contains fields collections.

I tried this option:

Files » Platform

but it doesn't work. It doesn't return anything.
(Platform is a term reference inside Files, and Files is a field collection inside a content type).

Also I tried this:

Files » Host entity (Content) » Files » Platform

with the same results.

It seems that nothing is indexed.

And a final note, I'm using the last sandbox proposed:
https://www.drupal.org/sandbox/mihai_brb/2510552

Regards,
JOINSO

mihai_brb’s picture

Wouldn't this lead to errors when the host entity is of a wrong type? That's why I had the separate getter callback in my patch.

@drunken monkey Why do you think this would lead to errors?

drunken monkey’s picture

@drunken monkey Why do you think this would lead to errors?

E.g., if there are field collections attached to both users and nodes. Then, when you have a wrapper for a field collection on a user, both $wrapper->host_entity_user and $wrapper->host_entity_node would actually have a value – in both cases the user object, but in the latter the Entity API expects a node object. Then I'd expect an EntityMalformedException because the bundle property is missing, but even if there isn't an exception, I don't think this is useful behavior.
With my patch, you'd only get $wrapper->host_entity_user in that case, $wrapper->host_entity_node would be empty.

Nishruu’s picture

I confirm the EntityMalformedException occuring with patch 4.
When search api try to index my field collection, I see that in entity_extract_ids that $this->type is "node" but the entity is a field_collection_item.
Patch 1 works fine.

arthur_drupal’s picture

Well for me, i remove "['bundles'][$field_name]" from patch 4 and it worked with Rules Data Selector.
I am going to understand why it does not work with.

Best,

arko0’s picture

Hi

"drunken monkey" I tried both patches and that sandbox, well it works for indexing.
But the problem is that when trying for filter the field collection items with faceted API and Search API it returns all items. Hope that you guys take a look of this issue.

NWOM’s picture

@drunken monkey: Do you recommend going with #1, #4, or the sandbox module mentioned in #6? Would love to give this a shot, since I'm needing similar functionality, but would hate to go with the method that won't be developed further. Thanks in advance!

Edit: Nevermind, i'm also getting the EntityMalformedException error on the sandbox module and #6. I'll try my luck with #1 and report back.

NWOM’s picture

Status: Needs review » Needs work
NWOM’s picture

#1 worked perfectly. Setting this to Needs Work then, since the newest patches don't work in comparison. However, #1 is potentially RTBC. Further clarification from both patch authors is most likely needed.

drunken monkey’s picture

Status: Needs work » Reviewed & tested by the community

As explained, I'm pretty sure my patch is correct and #4 won't work. If it's the same in your tests, and since mihai_brb doesn't respond anymore, let's set this to RTBC for patch #1.

willcode4food’s picture

Hello,

I'd like to use this in my Drupal 8 site. Would it be possible to get a port?

thanks

mtem27’s picture

+ 1

This works for Rules as well, I was able to retrieve host entity of nodes.

Thanks!

BYUStudies’s picture

Patch #1 was working great for me up until Search API updated to 7.x-1.23 a few days ago. Now I'm back to getting no results when I search for values from fields that are in a field collection. Any suggestions?

drunken monkey’s picture

Release 1.23 was broken in general, which is likely the problem you see. Update to 7.x-1.24 instead.

julia.klimovsky’s picture

Reroll patch to the HEAD of 7.x-1.x

  • ram4nd committed d638900 on 7.x-1.x authored by drunken monkey
    Issue #2489142 by drunken monkey, mihai_brb, julia.klimovsky: Add entity...
ram4nd’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.