Score:-4

Running a custom cron job

sm flag

I'm trying to implement a newsletter subscription on Drupal 10 that will send an email to all the subscribers automatically. I work in a restrictive environment where I'm not allowed to use custom modules and the choice of modules available is pretty restrictive so I cant hook the cron job using modules. Is there anyway I can implement a cron job from scratch or hook it using the theme file. I've thought about hooking into the theme_page and then comparing the time to a stored variable but that would probably make it more complicated because of concurrency issues.

leymannx avatar
ne flag
Are you sure hook_cron is not triggered when it lives in a *.theme file?
apaderno avatar
us flag
@leymannx Given the code used by [`Cron::invokeCronHandlers()`](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Cron.php/function/Cron%3A%3AinvokeCronHandlers/9), only module implementations of `hook_cron()` are invoked. It would also be a bad idea to implement such hooks in a theme. It is also the wrong workaround for a "do not use custom modules" rule that is not thought to force developers to put code that should go in a custom module in a theme. I can understand the reason to put such restriction, even if I find it too restrictive.
Kevin avatar
in flag
```I work in a restrictive environment where I'm not allowed to use custom modules and the choice of modules available is pretty restrictive so I cant hook the cron job using modules.``` this is going to eliminate 99.9% of whats possible in a CMS or any platform. It will also make any question under this circumstance next to impossible to answer in a reasonable way.
apaderno avatar
us flag
If none of the modules you are allowed to install help in creating a newsletter subscription, there is not much you can do, as @Kevin said. If newsletter subscription is a feature to implement on a site, you can only say that is not possible to implement with those restrictions. I assume that newsletter subscription is really the feature you are asked to implement.
DrupalDev avatar
sm flag
@apaderno Is hooking the preprocess page not recommended?
apaderno avatar
us flag
The purpose of theme hooks like `hook_preprocess_node()` is to change how a page is rendered or provide that information to template files. It is not a replacement for other hooks you cannot implement because you are not allowed to use custom modules. This means theme hooks cannot be used to send emails or even find a list of specific nodes that should be shown in the page, for example because they are related to the shown node.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.