As a long time user of Drupal I think a lot of people expect certain info available such as:

function _lupus_ce_renderer_response_alter(array &$data, BubbleableMetadata $bubbleable_metadata, Request $request) {
  $site_config = \Drupal::config('system.site');
  $data['site_info'] = [
    'name' => $site_config->get('name'),
    'slogan' => $site_config->get('slogan'),
    'mail' => $site_config->get('mail'),
  ];
}

For us this works super well as then can we add the name, slogan and email as defaults within the Nuxt theme.

> We need some configuration form or README that documents how to use it

Added readme.

> We need some basic test coverage before merge!

Tests pass.

> Error handling.

I added the checks to a default message if the config_name:config_key is missing.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

glynster created an issue. See original summary.

glynster’s picture

Issue summary: View changes
glynster’s picture

Issue summary: View changes
fago’s picture

Title: Basic Site Info » Expose Site Info / Config Settings to the frontend

While I can see the use-case for needing this information, I don't think it makes sense to ship this information on every ce-api page response, since it would grow all the responses, while the information is usually only needed once. Thus, it seems to be better solved with a dedicated endpoint + a separate request to fetch it from nuxt. When done so, we shuold be able to leverage caching in nuxt such that the request is done only once in the server instance, and cached then.

On how to providing the information, I don't think there is a pre-existing API for it. So it makes sense to add it.

However, when adding it: The question becomes, what exactly shall we add? It seems very use-case specific what shall be added, there are definitely more config-settings that one might want to use in the frontend, for example a configured google analytics key.

Thus, I feel like we could build a solution for exposing any kind of config as "Site-info" and make it configurable, e.g. by specifying the keys you want:
system.site:name
system.site:slogan
system.site:mail

and just output a data structure like

{ system.site : {
  name: "..",
  slogan: "..",
  mail: "..."
}}

and make the result a public API, e.g. at /lupus/site-info

I guess, that would make sense to provide as a new optional module, Lupus Decoupled Site Info ?

glynster’s picture

Sounds like a perfect idea to me, and as you say it can be added to depending on your needs. Totally agree on the lookup once and cached on Nuxt end. We can achieve this via useState

useernamee made their first commit to this issue’s fork.

useernamee’s picture

Status: Active » Needs review

I created the Lupus Decoupled Site Info module and added some initial configuration.

glynster’s picture

@useernamee great news. This will be very helpful. My only suggesting is the API route /api/lupus/site-info changes to /api/site-info seems more appropriate to me. Great job!

fago’s picture

Status: Needs review » Needs work

that seems pretty good already, thank you!

A few remarks:
- please check the MR comment
- I am not sure about the /api/lupus prefix also, what about just /api/site-info or /lupus-site-info ?
- We need some configuration form or README that documents how to use it
- We need some basic test coverage before merge!

useernamee’s picture

Issue summary: View changes

> I am not sure about the /api/lupus prefix also, what about just /api/site-info or /lupus-site-info ?

I went with the /api/site-info but this is also a limitation of the Rest resource which I picked for the task, since it would seem nicer to have a resource at the endpoint of /site-info, for uniformity.

useernamee’s picture

Status: Needs work » Needs review
fago’s picture

Status: Needs review » Needs work

thank you! that looks pretty good now. I've added one remark with this missing value though, please check MR review.

useernamee’s picture

Status: Needs work » Needs review

I removed the missing config option and added a watchdog error for that case instead:

{"system.site":{"name":"Drush Site-Install","slogan":"lupus.digital Publishing","mail":"admin@example.com","missing":null},"missing":{"missing":null}}
fago’s picture

thank you! Reviewed it and edited a bit. Seems good to go now!

However, I did not test it yet. So it still needs a manual test before it's ready for merge!

arthur_lorenz’s picture

Status: Needs review » Needs work

Test failed, cache is not being invalidated after config changes.

To reproduce:

  • Install lupus_decoupled_site_info module
  • Go to /api/site-info -> Info is correct
  • Go to /admin/config/system/site-information and update the slogan
  • Go to /api/site-info -> Info is outdated
useernamee’s picture

Status: Needs work » Needs review

Thank you @arthur. I wasn't paying attention to caching.

- Added cacheable dependencies to the response.
- Cache is tested.

arthur_lorenz’s picture

Status: Needs review » Reviewed & tested by the community

Great, thank you :)

Tested it locally -> RTBC ✅

  • fago committed 619f2133 on 1.x authored by useernamee
    Issue #3503890 by useernamee, glynster, arthur_lorenz: Allow exposing...
fago’s picture

Status: Reviewed & tested by the community » Fixed

Thank you! Merged!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.