There are several variants to this question on the interwebs, but none seem to cover the specific task of rendering custom data from custom modules at the D8/D9+ theme layer. I have several external data sources from local/remote database tables that need to be rendered to users. However, I do not seem to be able to execute custom class calls in the .theme file to pass this data into the $vars[] array in a preprocess function.
function theme_name_preprocess(&$vars, $hook){ //Works $vars['current_path'] = Drupal::request()->getRequestUri(); //Works $vars['uid'] = Drupal::currentUser()->id(); //Does not work - Error: Class MyModuleClass not found $vars['custom_value'] = MyModuleClass::public_static_function(); } //Does not work - Error: Class MyModuleClass not found $vars['custom_value'] = Drupalmy_moduleMyModuleClass::public_static_function(); }
I need to be able to send a lot of data from different sources, so if there’s a useful way to include custom code in the .theme file for preprocess functions, it would be most helpful to know. In the mean time, I can accomplish this with custom blocks, but this seems like it should be easy to do.