function ConfigFormBase::checkConfigOverrides

Form #after_build callback: Adds message if overrides exist.

File

core/lib/Drupal/Core/Form/ConfigFormBase.php, line 347

Class

ConfigFormBase
Base class for implementing system configuration forms.

Namespace

Drupal\Core\Form

Code

public function checkConfigOverrides(array $form, FormStateInterface $form_state) : array {
  // Determine which of those editable config keys have overrides.
  $override_links = [];
  $map = $form_state->get(static::CONFIG_KEY_TO_FORM_ELEMENT_MAP) ?? [];
  foreach ($map as $config_name => $config_keys) {
    $stored_config = $this->configFactory
      ->get($config_name);
    if (!$stored_config->hasOverrides()) {
      // The config has no overrides at all. Can be skipped.
      continue;
    }
    foreach ($config_keys as $key => $array_parents) {
      if ($stored_config->hasOverrides($key)) {
        $element = NestedArray::getValue($form, $array_parents);
        $override_links[] = [
          'attributes' => [
            'title' => $this->t("'@title' form element", [
              '@title' => $element['#title'],
            ]),
          ],
          'url' => Url::fromUri("internal:#{$element['#id']}"),
          'title' => $element['#title'],
        ];
      }
    }
  }
  if (!empty($override_links)) {
    $override_output = [
      '#theme' => 'links__config_overrides',
      '#heading' => [
        'text' => $this->t('These values are overridden. Changes on this form will be saved, but overrides will take precedence. See <a href="https://www.drupal.org/docs/drupal-apis/configuration-api/configuration-override-system">configuration overrides documentation</a> for more information.'),
        'level' => 'div',
      ],
      '#links' => $override_links,
    ];
    $form['config_override_status_messages'] = [
      'message' => [
        '#theme' => 'status_messages',
        '#message_list' => [
          'status' => [
            $override_output,
          ],
        ],
        '#status_headings' => [
          'status' => $this->t('Status message'),
        ],
      ],
      // Ensure that the status message is at the top of the form.
'#weight' => array_reduce(Element::children($form), fn(int $carry, string $key) => min($form[$key]['#weight'] ?? 0, $carry), 0) - 1,
    ];
  }
  return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.