DX of \Drupal::config() is extremely confusing: doesn't complain when requesting non-existing config

Created on 9 December 2016, almost 9 years ago
Updated 2 September 2025, 28 days ago

Problem/Motivation

My colleague Nick_vh β†’ ran into a problem with a cached page not being invalidated when necessary. He adds a toolbar item depending on some configuration. The config was bla.settings. He was accessing bla.settings.some_existing_key in his toolbar-related code, and adding the cache tag for that. Of course, that didn't match the actual configuration's cache tag. Hence things failed.

So apparently the D8 config system allows for this:

  $c1 = \Drupal::config('rest.settings');
  $c2 = \Drupal::config('rest.settings.bc_entity_resource_permissions');
  $c3 = \Drupal::config('rest.settings.foobar');
  $c4 = \Drupal::config('foobar');
  1. Is good.
  2. Is an existing key in rest.settings, but should not work. Yet it pretends nothing is wrong, the raw data is just the empty array. So it doesn’t allow you to access this config, but it also doesn’t complain.
  3. Is a non-existing key in rest.settings. Same behavior as 2.
  4. Is something completely non-existing. Same behavior as 2.

GΓ‘bor Hojtsy pointed out that this is so you can create new config and then ->save() it. Having a separate API for creating config would be a lot better.

We also complain when you Node::load('non-existing-id'). Why not do the same for config? The 90% use case is loading config (if not 99%), why do we make that case so frustrating (or at least easy to shoot yourself in the foot) for the rare creation case?

Proposed resolution

TBD

Remaining tasks

TBD

User interface changes

None.

API changes

TBD

Data model changes

TBD

πŸ“Œ Task
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

configuration system

Created by

πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

Live updates comments and jobs are added and updated live.
  • DrupalWTF

    Worse Than Failure. Approximates the unpleasant remark made by Drupal developers when they first encounter a particular (mis)feature.

  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

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

    Thank you for creating this issue to improve Drupal.

    We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

  • I think this confusing API should be changed.

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

    Thanks! Just leaving the tags for stats, want to track when gets moved back to active or NW

  • Sorry! I thought the "stale-issue-cleanup" tag was a signal for the bot to close this in n days.

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    Implemented this...

    What is interesting is that \Drupal\Core\Config\ConfigInstaller::createConfiguration() does not use the config factory so it is unaffected. In my mind this calls into question whether we actually need the method to create configuration. Let's see what the deprecations bring up... maybe creating config entities will need some changes...

  • Pipeline finished with Failed
    27 days ago
    #588880
  • Pipeline finished with Failed
    27 days ago
    #588899
  • Pipeline finished with Failed
    27 days ago
    #588913
  • Pipeline finished with Failed
    27 days ago
    #588954
  • Pipeline finished with Failed
    27 days ago
    #588992
  • Pipeline finished with Running
    27 days ago
    #589159
  • πŸ‡§πŸ‡ͺBelgium borisson_ Mechelen, πŸ‡§πŸ‡ͺ

    This makes a lot of sense, but it seems like there's still some tests that need fixing.

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    Yeah and it is going to be a bit painful... because atm services get instantiated during the installer before config is imported and rely on the current behaviour.

Production build 0.71.5 2024