function SchemaCheckTrait::isViolationForIgnoredPropertyPath

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

Determines whether this violation is for an ignored Config property path.

Parameters

\Symfony\Component\Validator\ConstraintViolation $v: A validation constraint violation for a Config object.

Return value

bool

1 call to SchemaCheckTrait::isViolationForIgnoredPropertyPath()
SchemaCheckTrait::checkConfigSchema in core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php
Checks the TypedConfigManager has a valid schema for the configuration.

File

core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php, line 136

Class

SchemaCheckTrait
Provides a trait for checking configuration schema.

Namespace

Drupal\Core\Config\Schema

Code

protected static function isViolationForIgnoredPropertyPath(ConstraintViolation $v) : bool {
    // When the validated object is a config entity wrapped in a
    // ConfigEntityAdapter, some work is necessary to map from e.g.
    // `entity:comment_type` to the corresponding `comment.type.*`.
    if ($v->getRoot() instanceof ConfigEntityAdapter) {
        $config_entity = $v->getRoot()
            ->getEntity();
        assert($config_entity instanceof ConfigEntityInterface);
        $config_entity_type = $config_entity->getEntityType();
        assert($config_entity_type instanceof ConfigEntityType);
        $config_prefix = $config_entity_type->getConfigPrefix();
        // Compute the data type of the config object being validated:
        // - the config entity type's config prefix
        // - with as many `.*`-suffixes appended as there are parts in the ID (for
        //   example, for NodeType there's only 1 part, for EntityViewDisplay
        //   there are 3 parts.)
        // TRICKY: in principle it is possible to compute the exact number of
        // suffixes by inspecting ConfigEntity::getConfigDependencyName(), except
        // when the entity ID itself is invalid. Unfortunately that means
        // gradually discovering it is the only available alternative.
        $suffix_count = 1;
        do {
            $config_object_data_type = $config_prefix . str_repeat('.*', $suffix_count);
            $suffix_count++;
        } while ($suffix_count <= 3 && !array_key_exists($config_object_data_type, static::$ignoredPropertyPaths));
    }
    else {
        $config_object_data_type = $v->getRoot()
            ->getDataDefinition()
            ->getDataType();
    }
    if (!array_key_exists($config_object_data_type, static::$ignoredPropertyPaths)) {
        return FALSE;
    }
    foreach (static::$ignoredPropertyPaths[$config_object_data_type] as $ignored_property_path_expression => $ignored_validation_constraint_messages) {
        // Convert the wildcard-based expression to a regex: treat `*` nor in the
        // regex sense nor as something to be escaped: treat it as the wildcard
        // for a segment in a property path (property path segments are separated
        // by periods).
        // That requires first ensuring that preg_quote() does not escape it, and
        // then replacing it with an appropriate regular expression: `[^\.]+`,
        // which means: ">=1 characters that are anything except a period".
        $ignored_property_path_regex = str_replace(' ', '[^\\.]+', preg_quote(str_replace('*', ' ', $ignored_property_path_expression)));
        // To ignore this violation constraint, require a match on both the
        // property path and the message.
        $property_path_match = preg_match('/^' . $ignored_property_path_regex . '$/', $v->getPropertyPath(), $matches) === 1;
        if ($property_path_match) {
            return preg_match(sprintf("/^(%s)\$/", implode('|', $ignored_validation_constraint_messages)), (string) $v->getMessage()) === 1;
        }
    }
    return FALSE;
}

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