How to programmatically manage taxonomies?

Drupal Answers Asked by Deny Dias on October 15, 2020

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?

One Answer

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 DrupalCoreAnnotationQueueWorker 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:

Correct answer by 4k4 on October 15, 2020

Add your own answers!

Related Questions

Ajax callback does not replace the form but add a new field

1  Asked on January 6, 2022 by billyjoe


How to change search placeholder?

1  Asked on January 6, 2022 by tritof


Using computed field value in Rules

2  Asked on January 4, 2022


Unable to save content language configuration

2  Asked on January 4, 2022 by philipp-michael


Workbench: How can I reorder display of Workbench Tabs?

1  Asked on January 2, 2022 by cocoskin


Getting the image URL from a field_image on a node

16  Asked on January 2, 2022 by rias


Drupal 8 CKEeditor license/pricing

1  Asked on January 2, 2022 by donquixote


Retain Guzzle Session During Batch Operations

1  Asked on January 2, 2022 by tyler-frankenstein


Fatal error while editing and deleting menu items

1  Asked on December 31, 2021 by ashutoshsngh


Field collection item not visible to anonymous user

1  Asked on December 28, 2021 by kevindeleon


Get Checkbox Value From Another Form

1  Asked on December 28, 2021


How do I change the uploading image file name

1  Asked on December 28, 2021 by i-am-batman


get value from content type B, in a view of content type A

1  Asked on December 28, 2021 by bapafe


Twig Template print field – if not node number

1  Asked on December 28, 2021 by juc1


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP