function ConfigFormBase::storeConfigKeyToFormElementMap

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Form/ConfigFormBase.php \Drupal\Core\Form\ConfigFormBase::storeConfigKeyToFormElementMap()
  2. main core/lib/Drupal/Core/Form/ConfigFormBase.php \Drupal\Core\Form\ConfigFormBase::storeConfigKeyToFormElementMap()

Render API callback: Stores a map of element names to config keys.

This function is assigned as a #after_build callback.

This will store an array in the form state whose keys are strings in the form of `CONFIG_NAME:PROPERTY_PATH`, and whose values are instances of \Drupal\Core\Form\ConfigTarget.

This callback is run in the form's #after_build stage, rather than #process, to guarantee that all of the form's elements have their final #name and #parents properties set.

Parameters

array $element: The element being processed.

\Drupal\Core\Form\FormStateInterface $form_state: The current form state.

Return value

array The processed element.

See also

\Drupal\Core\Form\ConfigFormBase::buildForm()

File

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

Class

ConfigFormBase
Base class for implementing system configuration forms.

Namespace

Drupal\Core\Form

Code

public function storeConfigKeyToFormElementMap(array $element, FormStateInterface $form_state) : array {
  // Empty the map to ensure the information is always correct after
  // rebuilding the form.
  $form_state->set(static::CONFIG_KEY_TO_FORM_ELEMENT_MAP, []);
  $element = $this->doStoreConfigMap($element, $form_state);
  // Use the map to set the cacheability metadata on the form.
  $map = $form_state->get(static::CONFIG_KEY_TO_FORM_ELEMENT_MAP) ?? [];
  $tags = [];
  foreach (array_merge(array_keys($map), $this->getEditableConfigNames()) as $config_name) {
    $tags = Cache::mergeTags($tags, $this->configFactory()
      ->getEditable($config_name)
      ->getCacheTags());
  }
  if (!empty($tags)) {
    CacheableMetadata::createFromRenderArray($element)->addCacheTags($tags)
      ->applyTo($element);
  }
  return $element;
}

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