Loading Pattern Entities
Previously, loading patterns was most commonly handled using the static entity creation method \Drupal\patternkit\Entity\Pattern::create() and could often require loading the pattern definition from another service beforehand. The new PatternRepository service (patternkit.pattern.repository) service aims to streamline this process by simplifying the requirements to only the namespaced identifier for the pattern to be loaded.
The Old Way
$pattern_definition = \Drupal::service('patternkit.pattern.discovery')
->getPatternDefinition('@patternkit/atoms/example/src/example');
$pattern = \Drupal\patternkit\Entity\Pattern::create($pattern_definition);
The New Way
$pattern = \Drupal::service('patternkit.pattern.repository')->getPattern('@patternkit/atoms/example/src/example');
Listing All Patterns
The Pattern Repository service also offers an easy way to get a list of all known patterns. Previously this required loading and iterating through all pattern definitions, but now there is a simple $patternRepository->getAllPatternNames() method to get all the pattern names. The examples below illustrate the difference in iterating over all known patterns to perform some operation assuming only the pattern name is needed.
The Old Way
$patternDiscovery = \Drupal::service('patternkit.pattern.discovery');
foreach ($patternDiscovery->getPatternDefinitions() as $namespace => $patterns) {
foreach ($patterns as $name => $definition) {
// Do something with each pattern.
}
}
The New Way
$patternRepository = \Drupal::service('patternkit.pattern.repository');
foreach ($patternRepository->getAllPatternNames() as $pattern) {
// Do something with each pattern.
}
Identify All Pattern Dependencies
Previously there was no server-side identification of dependencies within patterns. All dependencies were handled clientside when the schema form was loaded and the references would be resolved through the /api/patternkit/ controller. Now, all immediate dependencies referenced within a pattern’s schema are stored in the pattern definition and may be fetched with the new $pattern->getDependencies() method. A new service has also been added to recursively identify all of a pattern’s dependencies. To do this, use the PatternDependencyResolver service (patternkit.pattern.dependency_resolver).
Getting a Pattern’s Immediate Dependencies
Identifying the references within a pattern’s schema may now be done directly from the pattern entity since they are determined and stored in the pattern’s definition on discovery.
$pattern = \Drupal::service('patternkit.pattern.repository')
->getPattern('@patternkit/atoms/example_ref/src/example_ref');
print_r($pattern->getDependencies());
//Array
//(
// [0] => Array
// (
// [value] => @patternkit/atoms/example/src/example
// )
//)
Getting All Pattern Dependencies
When working with complex patterns composed of many, sometimes multilevel, references, the PatternDependencyResolver service may be used to recursively identify all schema dependencies from a pattern. Using the getOrderedDependencies() method, it will also recommend the sequence for loading them from least to most dependencies in order to ensure patterns are loaded efficiently.
$dependencies = \Drupal::service('patternkit.pattern.dependency_resolver')
->getOrderedDependencies('@my/super/pattern');
Loading a Bundled Schema
For pattern ecosystems with a lot of interrelations and composed elements, loading references may become an intensive process. When taken to extremes, this can even impact how long it takes to load a pattern’s edit form if there are a lot of references to be loaded. To solve this, the Pattern Repository service also offers a method to bundle dependent pattern schemas to prevent having to load them asynchronously.
$bundled_schema = \Drupal::service('patternkit.pattern.repository')
->getBundledPatternSchema('@patternkit/atoms/example/src/example');
$json_content = json_encode($bundled_schema);
Sample Schema: @my/example/pattern
{
"type": "object",
"properties": {
"my_text_property": {
"$ref": "@my/text/pattern"
}
}
}
Sample Schema: @my/text/pattern
{
"type": "string"
}
Bundled Schema
{
"type": "object",
"properties": {
"my_text_property": {
"$ref": "#/definitions/my_text_pattern"
}
},
"definitions": {
"my_text_pattern": {
"type": "string"
}
}
}