Skip to content
Snippets Groups Projects
Commit 04619ae6 authored by Stephen Lucero's avatar Stephen Lucero
Browse files

Issue #3470358 by slucero, krisahil: Enable option to suppress pattern schema...

Issue #3470358 by slucero, krisahil: Enable option to suppress pattern schema warnings per environment
parent 8a4af53f
No related branches found
No related tags found
1 merge request!127#3470358: Add configurable option to suppress schema warnings.
Pipeline #342377 passed with warnings
......@@ -151,6 +151,7 @@ services:
- '@module_handler'
- '@serialization.json'
- '@patternkit.schema.schema_walker_factory'
- '@state'
patternkit.schema.schema_factory:
class: Drupal\patternkit\Schema\SchemaFactory
arguments:
......
......
......@@ -7,6 +7,7 @@ use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Drupal\patternkit\Asset\LibraryNamespaceResolverInterface;
use Drupal\patternkit\Asset\PatternDiscoveryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -23,20 +24,6 @@ class PatternkitSettingsForm extends ConfigFormBase {
*/
const SETTINGS = 'patternkit.settings';
/**
* The library namespace resolver service for loading library definitions.
*
* @var \Drupal\patternkit\Asset\LibraryNamespaceResolverInterface
*/
protected LibraryNamespaceResolverInterface $libraryNamespaceResolver;
/**
* The pattern discovery service for loading patterns.
*
* @var \Drupal\patternkit\Asset\PatternDiscoveryInterface
*/
protected PatternDiscoveryInterface $patternDiscovery;
/**
* Constructor for PatternkitSettingsForm.
*
......@@ -48,16 +35,16 @@ class PatternkitSettingsForm extends ConfigFormBase {
* The library namespace resolver service.
* @param \Drupal\patternkit\Asset\PatternDiscoveryInterface $patternDiscovery
* The pattern discovery service.
* @param \Drupal\Core\State\StateInterface $state
* The state management service.
*/
public function __construct(
ConfigFactoryInterface $config_factory,
TypedConfigManagerInterface $typed_config_manager,
LibraryNamespaceResolverInterface $libraryNamespaceResolver,
PatternDiscoveryInterface $patternDiscovery,
protected LibraryNamespaceResolverInterface $libraryNamespaceResolver,
protected PatternDiscoveryInterface $patternDiscovery,
protected StateInterface $state,
) {
$this->libraryNamespaceResolver = $libraryNamespaceResolver;
$this->patternDiscovery = $patternDiscovery;
parent::__construct($config_factory, $typed_config_manager);
}
......@@ -70,6 +57,7 @@ class PatternkitSettingsForm extends ConfigFormBase {
$container->get('config.typed'),
$container->get('patternkit.library.namespace_resolver'),
$container->get('patternkit.pattern.discovery'),
$container->get('state'),
);
}
......@@ -164,6 +152,13 @@ class PatternkitSettingsForm extends ConfigFormBase {
'#default_value' => $config->get('entity_cache_tag_invalidation') ?? FALSE,
];
$form['advanced_settings']['unknown_schema_logging_enabled'] = [
'#type' => 'checkbox',
'#title' => $this->t('Unknown schema logging enabled'),
'#description' => $this->t('If enabled, warnings will be logged when unknown schemas are encountered during Pattern processing.'),
'#default_value' => $this->state->get('patternkit.unknown_schema_logging_enabled', TRUE),
];
return parent::buildForm($form, $form_state);
}
......@@ -191,6 +186,11 @@ class PatternkitSettingsForm extends ConfigFormBase {
->set('entity_cache_tag_invalidation', $form_state->getValue('entity_cache_tag_invalidation'))
->save();
// Store the log suppression setting in state instead of config, so it may
// be managed independently per deployment environment.
$this->state->set('patternkit.unknown_schema_logging_enabled',
$form_state->getValue('unknown_schema_logging_enabled', TRUE));
$libraries = $this->libraryNamespaceResolver->getLibraryDefinitions();
$count = count($libraries);
$this->messenger()->addStatus($this->t('Rebuilt Patternkit Library Cache with @count libraries.', ['@count' => $count]));
......
......
......@@ -7,6 +7,7 @@ use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\State\StateInterface;
use Drupal\patternkit\Annotation\PatternFieldProcessor as PatternFieldProcessorAnnotation;
use Drupal\patternkit\Attribute\PatternFieldProcessor as PatternFieldProcessorAttribute;
use Drupal\patternkit\Entity\PatternInterface;
......@@ -38,6 +39,8 @@ class PatternFieldProcessorPluginManager extends DefaultPluginManager implements
* The serialization service for unserializing schemas.
* @param \Drupal\patternkit\Schema\SchemaWalkerFactory $schemaWalkerFactory
* The SchemaWalkerFactory service.
* @param \Drupal\Core\State\StateInterface $state
* The state manager service.
*/
public function __construct(
\Traversable $namespaces,
......@@ -45,6 +48,7 @@ class PatternFieldProcessorPluginManager extends DefaultPluginManager implements
ModuleHandlerInterface $module_handler,
protected readonly SerializationInterface $serializer,
protected readonly SchemaWalkerFactory $schemaWalkerFactory,
protected readonly StateInterface $state,
) {
parent::__construct(
'Plugin/PatternFieldProcessor',
......@@ -183,6 +187,7 @@ class PatternFieldProcessorPluginManager extends DefaultPluginManager implements
*/
public function traverseSchema(string $schema_json, object|array $values, callable $callback, ...$args): mixed {
$schemaWalker = $this->schemaWalkerFactory->createFromString($schema_json, $values);
$logging_enabled = $this->state->get('patternkit.unknown_schema_logging_enabled', TRUE);
// Iterate over the values provided for the schema.
foreach ($schemaWalker as $key => $value) {
......@@ -198,14 +203,20 @@ class PatternFieldProcessorPluginManager extends DefaultPluginManager implements
else {
$path = $schemaWalker->getSubIterator()->getSchema()->getDocumentPath();
$valuePath = PointerUtil::getDataPointer($path);
$this->logger->debug("Unknown property schema for %key at %path given the value:"
if ($logging_enabled) {
$this->logger->debug(
"Unknown property schema for %key at %path given the value:"
. "<br /><pre>@propertyValue</pre>"
. "<br /><br /><strong>Full value set</strong><br /><pre>@values</pre>", [
. "<br /><br /><strong>Full value set</strong><br /><pre>@values</pre>",
[
'%key' => $key,
'%path' => $valuePath,
'@propertyValue' => var_export($value, TRUE),
'@values' => var_export($values, TRUE),
]);
]
);
}
}
}
......
......
......@@ -7,6 +7,7 @@ use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\State\StateInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\Tests\patternkit\Traits\JsonDecodeTrait;
use Drupal\patternkit\Entity\Pattern;
......@@ -123,6 +124,13 @@ class PatternFieldProcessorPluginManagerTest extends UnitTestCase {
*/
protected LoggerInterface&MockObject $logger;
/**
* A mocked state service.
*
* @var \Drupal\Core\State\StateInterface&\PHPUnit\Framework\MockObject\MockObject
*/
protected StateInterface&MockObject $state;
/**
* The Drupal container to configure for dependency services.
*
......@@ -164,6 +172,7 @@ class PatternFieldProcessorPluginManagerTest extends UnitTestCase {
$this->moduleHandler = $this->createMock(ModuleHandlerInterface::class);
$this->serializer = new Json();
$this->logger = $this->createMock(LoggerInterface::class);
$this->state = $this->createMock(StateInterface::class);
// Expect no errors to be logged unless overridden in a specific test.
$this->logger->expects($this->never())->method('error');
......@@ -189,6 +198,7 @@ class PatternFieldProcessorPluginManagerTest extends UnitTestCase {
$this->moduleHandler,
$this->serializer,
$this->schemaWalkerFactory,
$this->state,
);
$this->pluginManager->setLogger($this->logger);
......@@ -274,6 +284,7 @@ class PatternFieldProcessorPluginManagerTest extends UnitTestCase {
$this->moduleHandler,
$this->serializer,
$this->schemaWalkerFactory,
$this->state,
);
$pluginManager->setLogger($errorLogger);
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment