How to access site settings in PHP files using full loader?

Created on 18 April 2025, 6 days ago

After upgrading from 1.x to 2.x, I found that I had to switch from the Flattened Site Settings Loader to the Full loader in order to fix a WSOD.

However, when updating custom module code, I found that there was no intuitive way to load the "full" site settings.

The best I could figure out was to update the code from this (which works with the flattened loader and in v1):

$scholar_ledger_path = $site_settings->loadAll()['scholar_ledger']['scholar_ledger_path_alias'];

To this:

$scholar_ledger_path = $site_settings->loadAll()[7]->field_settings_text[0]->getValue()['value'];

Surely there's a more intuitive, self-documenting way to go about this? The project page and README don't elaborate in the "How to access the settings in php files" section beyond using the loadAll() function.

πŸ’¬ Support request
Status

Active

Version

2.0

Component

Documentation

Created by

πŸ‡ΊπŸ‡ΈUnited States caesius

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @caesius
  • πŸ‡ΊπŸ‡ΈUnited States caesius

    Since I figure it'll be asked, the WSOD was:

    TypeError: Drupal\site_settings\Plugin\SiteSettingsLoader\FlattenedSiteSettingsLoader::renderDefault(): Return value must be of type Drupal\Component\Render\MarkupInterface, string returned in Drupal\site_settings\Plugin\SiteSettingsLoader\FlattenedSiteSettingsLoader->renderDefault() (line 488 of modules/contrib/site_settings/src/Plugin/SiteSettingsLoader/FlattenedSiteSettingsLoader.php).

    And I did go through the update workflow (updb etc.)

    Again, it works with the Full loader, which seems to be the "more correct" and forward-compatible method anyway, but it seems to be lacking utility functions outside of Twig (unless I overlooked something obvious)

  • πŸ‡¬πŸ‡§United Kingdom scott_euser

    In the latest version as a result of https://www.drupal.org/project/site_settings/issues/3498435 ✨ Add developer helptext (when access available) to output some site settings via twig Active (2.0.1 onwards) you should see copy-pasteable twig snippets throughout the UI to be able to output values from the full loader. These snippets match the examples on the module homepage. They are found where site settings are configured (admin structure rather than admin content area)

    Still we should fix the fatal error as well though so will leave this open.

  • πŸ‡ΊπŸ‡ΈUnited States caesius

    I don't have the option of using twig since I'm using an access hook in a module. When I do use twig the helptext works great, but it's not applicable here. There's no utility functions to simplify or navigate the structure of the site settings that are brought in through loadAll() (that I could find, anyway)

    If I find steps to reproduce for the WSOD I'll split off a new issue.

  • πŸ‡¬πŸ‡§United Kingdom scott_euser

    Ah okay, in that case of you look in the module source code you could copy how fields are rendered from the twig functions in your own code, e.g. https://git.drupalcode.org/project/site_settings/-/blob/2.0.x/src/Twig/T...

    But ultimately Stie Settings are just field able entities like Nodes or Terms so getting a field value is the same, e.g. $entity->field_name->value.

  • πŸ‡ΊπŸ‡ΈUnited States caesius

    Thanks!

    For the record, this is what I came up with:

        $site_settings = \Drupal::service('site_settings.loader');
        $settings = $site_settings->loadAll();
        $scholar_ledger_path = $settings['scholar_ledger']['scholar_ledger_path_alias'];
    

    became

        $setting = \Drupal::entityTypeManager()->getStorage('site_setting_entity')
          ->loadByProperties(['type' => 'scholar_ledger_path_alias']);
        $scholar_ledger_path = reset($setting)
          ->get('field_settings_text')
          ->getString();
    

    But now I'm not sure in what context I'd need to use the site settings loader instead...

    Anyway, this issue can probably be closed, unless there's maybe a documentation issue that wasn't considered? I only laser-focused on trying to use the site settings loader instead of following the TwigExtension.php examples because that's what the project page said to use.

Production build 0.71.5 2024