Accessing site settings from other sites

Hi,

With my module, I added to the site settings a new field of the type Asset that works as an image or thumbnail that represents the site. I am able to get the option like this:

$site_settings = $site->getServiceLocator()->get('Omeka\Settings\Site');
echo ($site_settings->get('site_image')); // This would print the chosen asset ID

The problem is that, since I’m using getServiceLocator, the settings that I get are always the ones corresponding to the current site (where the view happens or the default site, if I try to access them for an admin view), even if I use another site, which I get with:

$sites = $this->api->search('sites')->getContent(); // Array of Omeka\Api\Representation\SiteRepresentation

I haven’t found any way to get Site Settings with the API. Is there any way to get the settings from an arbitrary site, without using SQL?

$connection = $serviceLocator->get('Omeka\Connection'); // I'd like to avoid this

Sorry for the odd question. The result will be a site-grid block :slight_smile:
Thank you.

The site settings object has a method setTargetId that allows you to change which site you’ll get settings for.

It’s going to be relatively inefficient to get settings for many different sites that way, since the system is optimized for the usual case of getting several settings in a single request all from one site, so we eagerly load all the settings for a site. So if you’re dealing with many sites, the SQL method is going to end up as much more performant. With a relatively small number it probably won’t matter much, though.

1 Like

Excellent, thank you very much. Since there shouldn’t be too many sites, for now I’ll just use setTargetId, at least for the admin interface. I might change that for the public view in the future.

Thank you for being so attentive and always giving such quick responses.