Review Drupal\Core\Menu\StaticMenuLinkOverrides::saveOverrides for performance improvements

Created on 31 August 2018, over 6 years ago
Updated 22 January 2025, 6 days ago

service ID: menu_link.static.overrides
class: Drupal\Core\Menu\StaticMenuLinkOverrides
method: saveOverride($id, array $definition)

To make it easy to see, copy the method's code here exactly as it was::

  public function saveOverride($id, array $definition) {
    // Only allow to override a specific subset of the keys.
    $expected = [
      'menu_name' => '',
      'parent' => '',
      'weight' => 0,
      'expanded' => FALSE,
      'enabled' => FALSE,
    ];
    // Filter the overrides to only those that are expected.
    $definition = array_intersect_key($definition, $expected);
    // Ensure all values are set.
    $definition = $definition + $expected;
    if ($definition) {
      // Cast keys to avoid config schema during save.
      $definition['menu_name'] = (string) $definition['menu_name'];
      $definition['parent'] = (string) $definition['parent'];
      $definition['weight'] = (int) $definition['weight'];
      $definition['expanded'] = (bool) $definition['expanded'];
      $definition['enabled'] = (bool) $definition['enabled'];

      $id = static::encodeId($id);
      $all_overrides = $this->getConfig()->get('definitions');
      // Combine with any existing data.
      $all_overrides[$id] = $definition + $this->loadOverride($id);
      $this->getConfig()->set('definitions', $all_overrides)->save(TRUE);
    }
    return array_keys($definition);
  }

the line:

$all_overrides[$id] = $definition + $this->loadOverride($id);

the "+" operation makes no sense,It doesn't do anything,Because all the keys already exist.
And the $id is also wrong,It has been processed by the encodeId()
While there will be no errors and the tests will be no problems,
but this wastes performance and is not elegant
needs to be fixed

📌 Task
Status

Active

Version

11.0 🔥

Component

menu system

Created by

🇨🇳China yunke

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

Comments & Activities

Not all content is available!

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

Production build 0.71.5 2024