Skip to content
Snippets Groups Projects
Commit 5cc77b5f authored by Mikael Meulle's avatar Mikael Meulle
Browse files

Merge branch '3490387-2.0.0-beta5-allow-more' into '2.0.x'

Issue #3490387 by just_like_good_vibes: Allow more specfic typed data to be marked compatible with ui patterns prop types associated with less specific typed data

See merge request !274
parents 51e6a635 fe5bab97
No related branches found
No related tags found
No related merge requests found
Pipeline #353980 passed
......@@ -18,7 +18,7 @@ use Drupal\ui_patterns\PropTypePluginBase;
default_source: 'number',
schema: ['type' => ['number', 'integer']],
priority: 1,
typed_data: ['decimal', 'float', 'integer', 'timestamp']
typed_data: ['decimal', 'float', 'integer']
)]
class NumberPropType extends PropTypePluginBase {
......
......@@ -19,7 +19,7 @@ use Drupal\ui_patterns\PropTypePluginBase;
convert_from: ['number', 'url', 'identifier'],
schema: ['type' => 'string'],
priority: 1,
typed_data: ['datetime_iso8601', 'email', 'string', 'machine_name', 'filter_format']
typed_data: ['string']
)]
class StringPropType extends PropTypePluginBase {
......
......@@ -7,6 +7,9 @@ use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\TypedData\PrimitiveBase;
use Drupal\Core\TypedData\TypedData;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\ui_patterns\SchemaManager\CompatibilityChecker;
/**
......@@ -33,12 +36,15 @@ class PropTypePluginManager extends DefaultPluginManager implements FallbackPlug
* The module handler to invoke the alter hook with.
* @param \Drupal\ui_patterns\SchemaManager\CompatibilityChecker $compatibilityChecker
* The compatibility checker.
* @param \Drupal\Core\TypedData\TypedDataManagerInterface $typedDataManager
* The typed data manager.
*/
public function __construct(
\Traversable $namespaces,
CacheBackendInterface $cache_backend,
ModuleHandlerInterface $module_handler,
protected CompatibilityChecker $compatibilityChecker,
protected TypedDataManagerInterface $typedDataManager,
) {
parent::__construct(
'Plugin/UiPatterns/PropType',
......@@ -109,21 +115,68 @@ class PropTypePluginManager extends DefaultPluginManager implements FallbackPlug
* A list of prop type IDs.
*/
public function getAllPropTypeByTypedData(string $data_type): array {
$definitions = $this->getDefinitions();
$cache_id = "ui_patterns_typed_data_prop_types_" . $data_type;
$prop_types = &drupal_static($cache_id);
if (is_array($prop_types)) {
return $prop_types;
}
$prop_types = [];
$compatibility_map = $this->getTypedDataCompatibilityMap();
$definitions = $this->getDefinitions();
$less_specific_data_types = $compatibility_map[$data_type] ?? [];
foreach ($definitions as $prop_type_id => $definition) {
if (empty($definition['typed_data'])) {
continue;
}
foreach ($definition['typed_data'] as $item) {
if ($data_type === $item) {
if (($data_type === $item) || in_array($item, $less_specific_data_types, TRUE)) {
$prop_types[] = $prop_type_id;
break;
}
}
}
return $prop_types;
}
/**
* Get typed data compatibility map.
*
* @return array<string, array<string>>
* Typed data compatibility map.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function getTypedDataCompatibilityMap() : array {
$compatibility_map = &drupal_static(__METHOD__);
if (is_array($compatibility_map)) {
return $compatibility_map;
}
$compatibility_map = [];
$typed_data_definitions = $this->typedDataManager->getDefinitions();
$keys = array_keys($typed_data_definitions);
$classMap = array_combine(
$keys,
array_map(function ($v, $key) {
return $v["class"];
}, $typed_data_definitions, $keys)
);
$skipped_parent_classes = [TypedData::class, PrimitiveBase::class];
foreach ($classMap as $typed_data_id => $typed_data_class) {
$compatibility_map[$typed_data_id] = [];
if (in_array(get_parent_class($typed_data_class), $skipped_parent_classes, TRUE)) {
continue;
}
foreach ($classMap as $keyY => $classY) {
if (($typed_data_id !== $keyY) && is_subclass_of($typed_data_class, $classY)) {
$compatibility_map[$typed_data_id][] = $keyY;
}
}
}
return $compatibility_map;
}
/**
* Get reachable identifiers with their convert paths.
*
......
......@@ -14,6 +14,7 @@ services:
parent: default_plugin_manager
arguments:
- "@ui_patterns.schema_compatibility_checker"
- "@typed_data_manager"
plugin.manager.ui_patterns_prop_type_adapter:
class: Drupal\ui_patterns\PropTypeAdapterPluginManager
parent: default_plugin_manager
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment