Add twig function to render site setting by machine name

Created on 7 November 2023, about 1 year ago

Problem/Motivation

In version 2 (alpha) we have the following twig function to render a single site setting:
{{ site_setting([id]) }}

This would be fine if those IDs were static and known ahead of time, but much like trying to render a specific node by ID it is problematic for the themer who doesn't know these IDs as they often haven't been created yet.

Steps to reproduce

  1. Create a site setting and name it 'test'
  2. Don't create the entity for the setting yet
  3. Try to place the future output somewhere in a twig template

Proposed resolution

It would be great to have a twig function that renders the site setting by machine name which is something that doesn't change. This way the site builder can set up the site settings but not create the actual content entities and the themer can output the result in the template. Then when the content editors work on the site settings they will populate them with content and create the entities which will output properly by machine name in the template.

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

🇮🇹Italy tanc Italy

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

Comments & Activities

  • Issue created by @tanc
  • @tanc opened merge request.
  • Status changed to Needs review about 1 year ago
  • 🇮🇹Italy tanc Italy

    One thing I haven't considered is that I think you can have multiple entities for a single site setting. So maybe this function should load and render all of them associated to that machine name?

  • Status changed to Needs work about 1 year ago
  • 🇬🇧United Kingdom scott_euser

    Thanks for the work on this! Good idea!

    Other things that we should do are:

    1. Add to the SiteSettingsFullLoaderTwigExtensionTest test
    2. Update README.md with example of this
    3. (Post-merge) Update module homepage with example of this
  • 🇬🇧United Kingdom scott_euser

    One thing I haven't considered is that I think you can have multiple entities for a single site setting. So maybe this function should load and render all of them associated to that machine name?

    Yes I suppose either we always return an array of site setting entities, or only an array if the type supports multiple. Perhaps the latter would be more in line with what developers might expect?

  • 🇮🇹Italy tanc Italy

    Thanks for the feedback @scott_euser

    I've updated it to make things a bit simpler and just return an array of all associated entities for a given site setting. I think this approach is easier to understand and fits better with the current code (changes less). I've updated the MR.

    If you're happy with this approach I'll write a test and update the README

  • Status changed to Needs review about 1 year ago
  • Status changed to Fixed about 1 year ago
  • 🇬🇧United Kingdom scott_euser

    Excellent, thanks for contributing this!

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

Production build 0.71.5 2024