function Mapping::processRequiredKeyFlags

Same name in other branches
  1. 10 core/lib/Drupal/Core/Config/Schema/Mapping.php \Drupal\Core\Config\Schema\Mapping::processRequiredKeyFlags()

Validates optional `requiredKey` flags, guarantees one will be set.

For each key-value pair:

  • If the `requiredKey` flag is set, it must be `false`, to avoid pointless information in the schema.
  • If the `requiredKey` flag is not set and the `deprecated` flag is set, this will set `requiredKey: false`: deprecated keys are always optional.
  • If the `requiredKey` flag is not set, nor the `deprecated` flag, will set `requiredKey: true`.

Parameters

\Drupal\Core\TypedData\MapDataDefinition $definition: The config schema definition for a `type: mapping`.

Return value

void

Throws

\LogicException Thrown when `requiredKey: true` is specified.

1 call to Mapping::processRequiredKeyFlags()
Mapping::__construct in core/lib/Drupal/Core/Config/Schema/Mapping.php
Constructs a TypedData object given its definition and context.

File

core/lib/Drupal/Core/Config/Schema/Mapping.php, line 229

Class

Mapping
Defines a mapping configuration element.

Namespace

Drupal\Core\Config\Schema

Code

protected function processRequiredKeyFlags(MapDataDefinition $definition) : void {
    foreach ($definition['mapping'] as $key => $key_definition) {
        // Validates `requiredKey` flag in mapping definitions.
        if (array_key_exists('requiredKey', $key_definition) && $key_definition['requiredKey'] !== FALSE) {
            throw new \LogicException('The `requiredKey` flag must either be omitted or have `false` as the value.');
        }
        // Generates the `requiredKey` flag if it is not set.
        if (!array_key_exists('requiredKey', $key_definition)) {
            // Required by default, unless this key is marked as deprecated.
            // @see https://www.drupal.org/node/3129881
            $definition['mapping'][$key]['requiredKey'] = !array_key_exists('deprecated', $key_definition);
        }
    }
}

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