0

If I create a module, by standard, it doesn't have database fields.

If I use this module to call an API it works, but what if the API is down then my module shows blank. Plus there's the downside of the API call possibly being slow or taking a while.

Is there a way to create tables for my module and then store the data there? Or does it have to be a component?

Or if this is not a mandatory thing, because I'm sure you can add any code to a module, is it best practice for some reason to make this a component?

1 Answer 1

1

The module is relevant to where and how it is displayed on the page, not what it does or where it gets its information to be displayed.

In answer to your first question, a number of core modules retrieve data from tables of related components so module do have database fields, the issue to me is how do you create a table on installation of a module.

As part of the installation of the module you could use an install script to check for and create the table(s). I am not sure that the elements is available or processed for modules .xml files, but I could be wrong.

Depending on the amount and type of data to be displayed you could call the API to retrieve the data, save the data to a table and then load the data from the table into the module layout. If the API is down then the processing could continue and load the last saved data from the table for display. However this may delay the loading of the module and the page while you wait for the API call to return the data.

You could load the module with the last saved data and display that and then issue the API call after the module is loaded, or not wait for the results, and when or if results are returned processing would then update the data being displayed using some JS to write the newly retrieved data to the module layout.

There is a hybrid of the two above but it really comes down to the data to be displayed and whether you wait for the API call to complete before continuing with the processing.

1
  • Great options, thank you for that.
    – Eoin
    Commented Sep 4 at 15:46

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.