Error: Call to a member function getConfigDependencyName() on null

Created on 12 March 2025, about 2 months ago

Problem/Motivation

Getting `Error: Call to a member function getConfigDependencyName() on null` on pages.

Not really sure what got me in this state, walked away and it was there when I came back.

Looking at the code it seems clear _how_ it can happen though.

      // If the dependencies are entities we need to convert them into objects.
      if ($type == 'config' || $type == 'content') {
        $affected_dependencies[$type] = array_map(function ($name) use ($type) {
          if ($type == 'config') {
            return $this->loadConfigEntityByName($name);
          }
          else {
            // Ignore the bundle.
            [$entity_type_id,, $uuid] = explode(':', $name);
            return $this->entityRepository->loadEntityByConfigTarget($entity_type_id, $uuid);
          }
        }, $affected_dependencies[$type]);
      }

     // ...
    // Key the entity arrays by config dependency name to make searching easy.
    foreach (['config', 'content'] as $dependency_type) {
      $affected_dependencies[$dependency_type] = array_combine(
        array_map(function ($entity) {
          return $entity->getConfigDependencyName();
        }, $affected_dependencies[$dependency_type]),
        $affected_dependencies[$dependency_type]
      );
    }

Since loadConfigEntityByName can return null and the later result uses the value without checking any weirdness in the dependency list can trigger an error.

Steps to reproduce

Proposed resolution

A fix might be as easy as an array filter? It seems like if the load failed, no future code is going to be able to interact with it. Alternatively though, we could just pass the names around and skip the load? its not immediately clear why we need to load the entity before mapping back to the entity name.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component

configuration system

Created by

πŸ‡ΊπŸ‡ΈUnited States neclimdul Houston, TX

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

Comments & Activities

  • Issue created by @neclimdul
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    Not really sure what got me in this state, walked away and it was there when I came back.

    For us, this occurred thusly:
    1. We maintain a feature module that includes configuration that can be installed newly on multiple sites.
    2. The configuration in this module included configuration for a contrib module that subsequently removed that part of the configuration.
    3. When we tried to install this feature module using the latest version of the contrib module, we got the error above.
    4. This was resolved by removing the no-longer-relevant config from our .yml files.

    Reference: https://www.drupal.org/project/facets/issues/3514174#comment-16049977 πŸ› Facets throws error on install Active

    I agree that the simplest resolution would seem to be to skip the processing if the the attempt to load results in a null object, e.g.

        foreach (['config', 'content'] as $dependency_type) {
          $affected_dependencies[$dependency_type] = array_combine(
            array_map(function ($entity) {
    +          if (is_null($entity) || !method_exists($entity, 'getConfigDependencyName')) {
    +            return '';
    +          }
              return $entity->getConfigDependencyName();
            }, $affected_dependencies[$dependency_type]),
            $affected_dependencies[$dependency_type]
          );
        }
    
Production build 0.71.5 2024