1

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.

1

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->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.

  • what type of field should I select? workid is a really long string – Haris Khan Feb 19 '18 at 21:50
  • well, how long is really long? more than 256 characters? Anything less than that is fine as a string/"Text (plain)", if it is even longer then you have to use a string_long/"Text (plain, long)". – Berdir Feb 19 '18 at 22:24
  • That is resolved now, the problem I am facing is that I am using hook_node_presave and the value of field_workid is not getting saved in the database even when I am trying $node->save(); I am looking into node_revision_field_workid table in the database – Haris Khan Feb 19 '18 at 22:31
  • Hard to say without seeing your code, I'd suggest creating a new question. You should definitely not call $node->save() in the presave hook – Berdir Feb 19 '18 at 22:57
  • Thank you sir you have helped a lot. I have not cleared my cache and that was the issue – Haris Khan Feb 19 '18 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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