Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Use hook_search_api_index_enabled()
(resp. hook_search_api_index_disabled()
) to determine when a new index is "created" or an old one "deleted", and insert / remove the corresponding entries in {search_api_item}.
Comments
Comment #1
drunken monkey@ fago: Are
hook_search_api_index_enabled()
andhook_search_api_index_disabled()
called for newly created entities in the database, too? The documentation says no, but the code says (or seems to me to say) yes …If the latter is the case, you should make that clear in .api.php — otherwise, sorry.
Comment #2
drunken monkeyLet's use this issue also for documenting the changes permitted by #651240: Allow modules to react to changes to an entity: i.e., use the newly available
$entity->original
property inhook_entity_update()
(and, therefore,hook_ENTITY_update()
) to recognize what changed in a modified index or server, instead of the less accurate, ugly (and hack-inducing)$op
parameter.Therefore, marked #964816: Provide own EntityController for fixing hook invocations as a duplicate of this issue.
Comment #3
fagoad #1, hm, the docs say yes too:
ad #2:
ok. What is a bit strange for exportable entities though, is that you can use hook insert to react on changes too, because an entity might exist even before it is inserted in the db, as default in code.
Comment #4
fago@status: see #978832-7: enable modules to apply configuration (exportable entities): Currently the feature requires the core patch + is not able to deal with an "activation" status flag yet.
Comment #5
drunken monkeySorry, my bad — it seems I mixed up "exportable entities" and "entities in code".
Oh, so this isn't actually working yet? Good to know. Hope the core patch will still make it in …
Comment #6
drunken monkeyOK, since there are a number of bugs solved by this and fago will have to fix the hook invocation without core patch anyways, I'm gonna go ahead and commit this. Please be aware that until #978832: enable modules to apply configuration (exportable entities) is fixed there will be problems when disabling modules that define default indexes (especially when they are later re-enabled).
There are quite a few API changes in this patch, here is a (hopefully complete) summary:
hook_ENTITY_update()
:$op
is gone, use$entity->original
instead.Likewise,
$op
is also gone from the entity objects'save()
methods.hook_search_api_index_reindex
for recognizing when an index was scheduled for reindexing or cleared (becausehook_search_api_index_update()
won't be called anymore).SearchApiServiceInterface::postUpdate()
doesn't take$fields
parameter anymore (see above).SearchApiServiceInterface
'spostEnable()
andpostDisable()
methods are gone, usepostUpdate()
instead. (Which should have been possible anyways — what was I thinking adding those other two methods?)SearchApiServer
's andSearchApiIndex
'supdate($fields)
methods can now also be used for changing the "enabled" field. Also, they are now just helper methods, the normalsave()
method can be used just as well.SearchApiServer
's andSearchApiIndex
'senable($enabled)
methods are gone.Now finally easily possible, I've also added "Enabled" checkboxes to the server and index edit forms.
Comment #7
drunken monkeyComment #8
fagoGreat work!