0

I'm working on a project with this requirement: there will be a couple of custom content types where many fields comes from taxonomies references.

Many of these taxonomies have plenty of terms (50+). These terms are periodically updated (added/corrected/removed) by a third party system in charge of this task, which later exposes these 'most current' terms list through a webservice.

So I don't need to force a human operator to manually check these terms lists and update D8 taxonomies accordingly, I thing that a good approach would be to periodically run a task to verify that webservice and then updated D8 with what have changed.

I've carefully read the following references:

The one that come closer to the project needs is the 4th one in the above list. It would suffice the requirements for now.

But I didn't quite get where this code should be typed. Does it need to be a custom module or should it go into themes/mytheme/mytheme.theme or so?

From where I stand, I think that an elegant solution is to have a routine somewhere (where?) that I could call via D8's cron so I can retrieve-then-update taxonomies periodically. But I have no clue on how I could achieve that goal.

Can someone provide any good references so I can fulfill this desire with code?

1 Answer 1

2

A simple way to set this up would be a cron hook.

function hook_cron

Perform periodic actions.

Modules that require some commands to be executed periodically can implement hook_cron(). The engine will then call the hook whenever a cron run happens, as defined by the administrator. Typical tasks managed by hook_cron() are database maintenance, backups, recalculation of settings or parameters, automated mailing, and retrieving remote data.

Short-running or non-resource-intensive tasks can be executed directly in the hook_cron() implementation.

Long-running tasks and tasks that could time out, such as retrieving remote data, sending email, and intensive file tasks, should use the queue API instead of executing the tasks directly. To do this, first define one or more queues via a \Drupal\Core\Annotation\QueueWorker plugin. Then, add items that need to be processed to the defined queues.

As mentioned a better way for D8 would be to use the queue API.

Or try the dev version of Feeds, which can do this without code:

https://www.drupal.org/project/feeds

3
  • Hi, @4k4. I'll looking into your proposed approaches, dive into the code and if they provide a working solution, I'm going to mark this as the accepted answer. Thank you very much for your answer.
    – denydias
    Commented Aug 11, 2016 at 21:42
  • Dear @4k4, your answer worked like a charm. I'm managing ~10k terms as a breeze. I did it in a way that I have this script to get the terms for many taxonomies from various 3rd party systems into files, each file named after the D8 taxonomy system name it applies to. Then, asynchronously, a custom made D8 module implementing hook_cron() runs, detect these files, parses them and bang! Lots of terms, from many sources, inside D8. I'm going to polish this module a bit further then release its code in the wild. Thank you @4k4 for the first spark.
    – denydias
    Commented Sep 2, 2016 at 15:41
  • A last minute note. @4k4 tip was as much important as the ones by Ashok Modi in this article: Saving and retrieving taxonomy terms programmatically for Drupal 8.
    – denydias
    Commented Sep 8, 2016 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.