When you add fields in a referenced entity in the IndexAddFieldsForm only base fields of the referenced entities are shown.

For example:

  • create a new content type
  • add a reference field to a taxonomy
  • in the taxonomy add a text field in the UI
  • create a search api index of type content
  • go to add fields
  • expand the taxonomy reference field you created in the second step
  • only the base fields of the taxonomy are shown, but the the new text field is missing and can't be indexed
CommentFileSizeAuthor
#29 2680405-29--nested_non_base_fields.patch11.35 KBdrunken monkey
#29 2680405-29--nested_non_base_fields--interdiff.txt5.62 KBdrunken monkey
#27 2680405-27--nested_non_base_fields.patch7.68 KBdrunken monkey
#27 2680405-27--nested_non_base_fields--interdiff.txt569 bytesdrunken monkey
#21 2680405-21--nested_non_base_fields.patch7.37 KBdrunken monkey
#21 2680405-21--nested_non_base_fields--interdiff.txt6.8 KBdrunken monkey
#19 interdiff-2680405-14-19.txt1.81 KBjohnchque
#19 expose_non_base_fields-2680405-19.patch4.59 KBjohnchque
#14 fields_in_ui-2680405-14.patch2.78 KBjjcarrion
#9 fields_in_ui-2680405-9.patch4.5 KBjjcarrion
#6 fields_in_ui-2680405-6.patch2.49 KBjjcarrion
#4 interdiff.txt746 bytesjjcarrion
#4 fields_in_ui-2680405-4.patch1.52 KBjjcarrion
#2 fields_in_ui-2680405-1.patch1.51 KBmiiimooo
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

miiimooo created an issue. See original summary.

miiimooo’s picture

Title: Expose non-base fields in Search API » Expose non-base fields in referenced entities in Add Fields
Related issues: +#2169813: Support deriving fields from entity definitions with multiple bundles
FileSize
1.51 KB

I'm attaching a patch that fixes the problem. The issue is caused by the getPropertyDefinitions method in core/lib/Drupal/Core/Entity/TypedData/EntityDataDefinition.php

It doesn't correctly get the bundles (and even if it did it would only work if there was only one bundle see https://www.drupal.org/node/2169813)

The patch has a small cosmetic problem: it adds the fields with a parent of Array which I hope is easy to fix.

miiimooo’s picture

Status: Active » Needs review
jjcarrion’s picture

I have fixed the "cosmetic" problem renaming the $label. I'm attaching the patch and the interdiff.

It works fine for me and I'll test it more during these days. Great work miiimooo!

jjcarrion’s picture

Status: Needs review » Needs work

It's seems that with the patch we have lost some filters in the view:

  • "Search: Fulltext search" is not there anymore
  • The filter on the field that we have been able to add with this patch doesn't appear on the view either.

Anyway, in the database the information is stored properly.

Anyone can point me where should be the problem?

jjcarrion’s picture

I have solved the issue that doesn't show the fields in the view with this patch.

But I'm getting a new error when I save the view.

The website encountered an unexpected error. Please try again later.

InvalidArgumentException: The configuration property display.default.display_options.filters.name_4.value.min doesn't exist. in Drupal\Core\Config\Schema\ArrayElement->get() (line 79 of core/lib/Drupal/Core/Config/Schema/ArrayElement.php).

Drupal\Core\Config\StorableConfigBase->castValue('display.default.display_options.filters.name_4.value.min', '') (Line: 216)
Drupal\Core\Config\StorableConfigBase->castValue('display.default.display_options.filters.name_4.value', Array) (Line: 216)
Drupal\Core\Config\StorableConfigBase->castValue('display.default.display_options.filters.name_4', Array) (Line: 216)
Drupal\Core\Config\StorableConfigBase->castValue('display.default.display_options.filters', Array) (Line: 216)
Drupal\Core\Config\StorableConfigBase->castValue('display.default.display_options', Array) (Line: 216)
Drupal\Core\Config\StorableConfigBase->castValue('display.default', Array) (Line: 216)
Drupal\Core\Config\StorableConfigBase->castValue('display', Array) (Line: 217)
Drupal\Core\Config\Config->save() (Line: 285)
Drupal\Core\Config\Entity\ConfigEntityStorage->doSave('search', Object) (Line: 397)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 264)
Drupal\Core\Config\Entity\ConfigEntityStorage->save(Object) (Line: 363)
Drupal\Core\Entity\Entity->save() (Line: 642)
Drupal\Core\Config\Entity\ConfigEntityBase->save() (Line: 1003)
Drupal\views_ui\ViewUI->save() (Line: 318)
Drupal\views_ui\ViewEditForm->save(Array, Object)
call_user_func_array(Array, Array) (Line: 116)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 56)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 588)
Drupal\Core\Form\FormBuilder->processForm('view_edit_form', Array, Object) (Line: 319)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 53)
Drupal\Core\Entity\EntityFormBuilder->getForm(Object, 'edit', Array) (Line: 232)
Drupal\views_ui\Controller\ViewsUIController->edit(Object, 'page_1')
call_user_func_array(Array, Array) (Line: 128)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 577)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 129)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 102)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array) (Line: 139)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 62)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 62)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 53)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 55)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 637)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

I have seen that this error is similar to #2664286: Cannot use date filters on index but in this case it's not a date type.

If somebody knows where could be the problem I can try to solve it. Thanks in advance!

drunken monkey’s picture

Are you sure that error is caused by this patch? It seems rather unconnected.
In any case, thanks for creating this issue and working on it, the solution already looks quite good.

+++ b/src/Form/IndexAddFieldsForm.php
@@ -368,7 +369,16 @@ class IndexAddFieldsForm extends EntityForm {
-        $nested_properties = $property->getPropertyDefinitions();
+        if ($property instanceof EntityDataDefinitionInterface) {
+          $bundles = \Drupal::entityManager()->getBundleInfo($property->getEntityTypeId());
+          foreach ($bundles as $bundle => $bundle_label) {
+            $bundle_properties = \Drupal::entityManager()->getFieldDefinitions($property->getEntityTypeId(), $bundle);
+            $nested_properties += $bundle_properties;

Contrary to the second change, in Field, it seems here you forgot to initialize $nested_properties for complex properties.

In any case, though, instead of duplicating code, we should maybe add this to the Utility class – either just for getting the child properties of the given properties, or already in a form that allows drilling down (if that makes sense and is possible to refactor/abstract.

Also, as always when breaking things that worked previously, it would be great if we could add a check somewhere in our testing suite that makes sure this won't happen again. Probably, just a few lines in IntegrationTest would suffice.

jjcarrion’s picture

Hi drunken monkey,

I'm not sure that the error that I'm getting because of the patch, but I get that error with the fields that now I can add after applying the patch.

Anyway, I'm agree with you that can be another issue, so I'll do that refactor and I'll try to do that test.

In my project I'm using a custom controller and I'm querying search_api from there because of the error, so I'll try to do this during the weekend.

Thanks for you help!

jjcarrion’s picture

I haven't made the test yet, but here it's the refactor to not duplicate the code. I have created the `getNestedProperties` method in the `Utility` class. I hope that could be Ok.

yobottehg’s picture

is it possible to take entity_reference_revisions new composite relationship into account so it is possible to index paragraphs in the future ?

The current patch works with paragraphs but i'm not sure it works after the migration to the new entity_reference_revisions composite relationship.

drunken monkey’s picture

@ jjcarrion: While #6 still applies for me, #9 somehow doesn't. Did you maybe make some mistake while creating it?

@ yobottehg: I'm sorry, but I can't take all contrib modules into account. If your module doesn't work with the Search API out-of-the-box, you can always override the datasource, or provide your own new one. I'll only fix problems if you can show me we're using the Entity API incorrectly, or if a change would be generally beneficial.
Also, in what way is that related to this issue?

jeroen.b’s picture

@yobottehg, we should still be fine as entity_reference_revisions DataType extends the EntityReference (which implements the DataReferenceInterface which search_api expects) class so "instanceof DataReferenceInterface" will still return TRUE on ERR field which should make the extraction work.

Didn't test it though.

yobottehg’s picture

@ drunken monkey sorry for crashing this issue, but it's related. And thanks @jeroen.b for clarifing.

jjcarrion’s picture

I have reroll the patch to make it work with the actual version, I was touching some code standards that were breaking the patch.

Here is the new patch, but nothing about the testing part yet. If somebody can point me a similar test that I can start with I can try it as well.

Thanks!

drunken monkey’s picture

Status: Needs work » Needs review
Issue tags: +Needs tests

I'd probably do it in IntegrationTest: just go to an appropriately nested path and see if one of the non-base fields there is present.
Problem is, though, I don't think there's any appropriate reference on nodes currently in there, so you'd have to add one – probably best just an entity reference to a different node, that way you don't have to add an additional module (I think).
Would be great, if you could work on that, thanks! (Also thanks for the re-roll!)

(Setting to "Needs review" for the test bot, still needs tests, as said.)

The last submitted patch, 9: fields_in_ui-2680405-9.patch, failed testing.

The last submitted patch, 9: fields_in_ui-2680405-9.patch, failed testing.

drunken monkey’s picture

Status: Needs review » Needs work

Good to see all existing tests pass, at least. Setting to "Needs work" for the tests.

johnchque’s picture

Thanks everyone for the previous patches, we are using this with entity reference revision and we found out that the patch #14 fixes our problem. I added some tests here, not really sure if this is enough or if we should check something else.

Berdir’s picture

Status: Needs review » Needs work

That's a good start, but that's just seeing the field.

I think we also want to test actually adding that field, indexing content that has such a reference and checking that it is being indexed (I guess the easiest way to do that is to just search for the text inside the reference?)

drunken monkey’s picture

Thanks for adding the test!
See the attached for a re-roll and a few minor fixes.

Berdir is right, though, we should also test indexing of the nested fields – and, since I just made a modification there, too, also that the dependencies for these fields are computed correctly.
Indexing will have to be tested in a different test, though, unless we add some way to inspect the indexed items with the test backend (which could be handy, though).

Status: Needs review » Needs work

The last submitted patch, 21: 2680405-21--nested_non_base_fields.patch, failed testing.

The last submitted patch, 21: 2680405-21--nested_non_base_fields.patch, failed testing.

The last submitted patch, 21: 2680405-21--nested_non_base_fields.patch, failed testing.

The last submitted patch, 21: 2680405-21--nested_non_base_fields.patch, failed testing.

johnchque’s picture

@drunken monkey thank you for the patch, I've been looking into it but could not realize why the tests are failing. Do the tests need an extra fix?

drunken monkey’s picture

borisson_’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

This looks like a good solution, it has test coverage as well. Woo!

drunken monkey’s picture

As noted in #20 and #21, it didn't have quite enough test coverage yet.
Should be fixed with the attached patch, though. If the test bot and one of you agree, I'd say this is RTBC.

johnchque’s picture

This looks good to me! Thank you so much @drunken monkey!

drunken monkey’s picture

Excellent, good to hear!
Committed.
Thanks again, everyone!

drunken monkey’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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