Add a field in node metadata I am trying to make a module that has two parts:


  1) It calls an API when article is set to published. The API returns a
  work id.
  
  2) The workid must be saved to the articles metadata or something
  similar so that another function uses that id to display specific html
  in that article.


I am done with the first part. Now I want to know how to save workid in the metadata, is there any option for that in drupal? because I have done this kind of work in joomla.
There no such thing as "node metadata", there are just fields.

So the easiest thing to do is to create a field (in the UI for example, you can then also export the configuration into your module), field_work_id or something, and in put in there with $node-&gt;set('field_work_id', $work_id).

You can do that in hook_node_presave(), but then you might not have an node ID yet for a node that is just being created and is already published. You could also use hook_node_insert()/hook_node_update(), but then you need resave the node and make sure you don't trigger a loop.

You can also save the work ID in your own table, or you can store the node together with the work ID in a way that allows you to do a lookup with a query.