The documentation at this point is pretty confusing/contradictory, but it seems theme functions were deprecated with Drupal 8, and are now scheduled for removal with Drupal 10.0.0 (see #3109480: Properly deprecate theme functions for Drupal 10).
Therefore, we should change all of our theme functions to templates, or adapt the code to use '#theme' => 'table' instead. (It seems all our theme functions are only used in a single place each.)

One question was how we could do this without any disruption to people who override these theme functions in some way. Hardly seems possible, unfortunately, and probably not worth it. Quoting @Berdir in #14:

I'm not sure it's worth it to worry about BC there. I have a hard time coming up with a reason to override them. afaik, they just exist because you had no other choice in D7 to have a table with form elements, not to make it themeable. Why would anyone bother creating an admin theme to override these theme functions which are just about showing those backend UI's

So IMHO it's enough to do a change notice, mention it in the release notes and move on.

All of these theme functions are just for the admin UI, so at least even the worst case scenario is that just the admin UI is broken, not the visitor-facing parts of the site/search.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

drunken monkey created an issue. See original summary.

mbovan’s picture

Assigned: Unassigned » mbovan

I've been working on converting tables to use '#type' => 'table' but there is a core issue with '#access' => FALSE that affects node_grants in Search API. Created #3123459: Table element renders empty rows if the row #access is set to false.

I am going to work on converting theme functions templates before going into further improvements.

mbovan’s picture

Connecting with #3023170: Test for compatibility with Drupal 9 as it has related discussion about this issue.

mbovan’s picture

Assigned: mbovan » Unassigned
FileSize
35.88 KB

Here is the patch that converts theme functions to templates and aims to make as fewer disruptions as possible regarding the markup.

mbovan’s picture

Status: Active » Needs review
Berdir’s picture

Any chance we can keep the functions in the .theme.inc file to keep this reviewable? At least as in intermediate step. It's not pretty and the downside is that we need to load it on every request, but for example field.module does a require_once __DIR__ . '/field.purge.inc';.

mbovan’s picture

Addressed #6 and leaving preprocess functions in search_api.theme.inc for eaiser reviewing.

drunken monkey’s picture

Thanks a lot for your work on this, great job!
I’m pretty bad with the theme layer, but it looks pretty good to me. I just had a few corrections (some of which I’m not 100% sure about, though) – please see the attached patch, and please test/review!

Also, once this is RTBC, should we still move the functions to search_api.module, or should we keep the separate file?
To be honest, it seems a good idea to me to keep it this way, as it clearly keeps the theming-related functions separate, and avoids increasing the size of the module file considerably.
But not sure where others would expect to find these functions, and if this separation would be confusing.

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

> and avoids increasing the size of the module file considerably.

For the record, that only benefits humans. Since we always load the extra file unconditionally, having it in a separate file actually makes it a tiny amount slower, but it's just one of hundreds of files being loaded on a typical requests ;)

I don't really care if we keep it or not, up to you.

Didn't review the documentation changes too closely, but looks like it's just fixing copy & paste errors.

mbovan’s picture

#8 looks good. Thanks for cleaning that up, +1 on RTBC.

Regarding separation of the theme functions, I usually look for those at *.module file, but since theme functions eat already more than 50% of the current number of lines in search_api.module, I agree it might make sense to keep them in .theme.inc.

drunken monkey’s picture

Thanks a lot for your input and feedback on this! Then let’s just keep those functions in the search_api.theme file.

I actually already wanted to commit this, but then read the IS again and saw an open question:

One remaining question is how we could do this without any disruption to people who override these theme functions in some way? Hardly seems possible, unfortunately, from all I know. (Which, luckily, isn’t much regarding the Theme layer, though.)
If we don’t find a solution for this, we should probably at least warn about this in the release notes beforehand (i.e., in a release before actually committing this) and then commit soon after a release, to give people plenty of time before the next one.
In any case, though, all of these theme functions are just for the admin UI, so at least even the worst case scenario is that just the admin UI is broken, not the visitor-facing parts of the site/search.

Do you have any input regarding that? I unfortunately forgot to add a note about this to the 1.16 release, so if we’d really wait for first having a release with a warning, it would take a while.

drunken monkey’s picture

Issue tags: +Needs change record

In any case, I guess this will need a change record?

phenaproxima’s picture

Answering #11: this might be an insane idea, but what if the preprocess functions we're adding in this patch would, before doing anything else, query the theme registry to see if the theme function has been overridden, and if so, throw a deprecation notice and then invoke that theme function directly to get the output? For example:

function template_preprocess_search_api_admin_fields_table(array &$variables, $hook) {
  $registry = \Drupal::service('theme.registry')->get();
  if (isset($registry[$hook]['function'])) {
    @trigger_error("The $hook theme function is deprecated, please override the template instead.", E_USER_DEPRECATED);
    $variables['table'] = call_user_func($registry[$hook]['function'], $variables);
    return;
  }

  // Do the normal preprocess stuff here...
}

Would this even work? Or am I misunderstanding the inspectability of the theme system? :) This might be total BS, but I thought I'd throw it out there.

Berdir’s picture

From Slack: I'm not sure it's worth it to worry about BC there. I have a hard time coming up with a reason to override them. afaik, they just exist because you had no other choice in D7 to have a table with form elements, not to make it themeable. Why would anyone bother creating an admin theme to override these theme functions which are just about showing those backend UI's

So IMHO it's enough to do a change notice, mention it in the release notes and move on.

phenaproxima’s picture

Issue summary: View changes

Works for me. Updating the issue summary to reflect this.

phenaproxima’s picture

Issue tags: -Needs change record
borisson_’s picture

I agree with #14 there is no need to worry about BC too much here. RTBC+1

drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Already then, if you all agree, then great! Committed. Thanks again, everyone!
Also thanks a lot for creating the change record, phenaproxima! And sorry for the delay.

jhuhta’s picture

Just FYI, after this change Acquia Search users can't access the search configuration anymore and they (we) need to downgrade/freeze the search_api version while waiting for Acquia to change the connector code accordingly:

Error: Call to undefined function theme_search_api_index() in acquia_search_theme_search_api_index() (line 618 of modules/contrib/acquia_connector/acquia_search/acquia_search.module).

So one could argue afterwards that a change notice wasn't quite enough. But maybe next time then.

Acquia has now issued a note in their knowledgebase: https://support.acquia.com/hc/en-us/articles/360048786374-Upgrading-Sear...

drunken monkey’s picture

Thanks for the information! You’re right, then, a change notice really wasn’t enough. We should probably have just deprecated the theme_*() functions, not completely removed them right away. Well, hindsight is 20/20, as they say. I’ll try to remember this the next time when removing code or something similar.

Status: Fixed » Closed (fixed)

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