Skip to content
Snippets Groups Projects
Commit 01911807 authored by Mikael Meulle's avatar Mikael Meulle Committed by christian.wiedemann
Browse files

Issue #3455076 by just_like_good_vibes: [2.0.0-alpha3] Move Field-related...

Issue #3455076 by just_like_good_vibes: [2.0.0-alpha3] Move Field-related sources from ui_pattern_field_formatters to ui_patterns
parent 51cdafbe
Branches
Tags
1 merge request!118Resolve #3455076 "2.0.0 alpha3 move field related"
...@@ -23,16 +23,6 @@ class RequirementsContext extends Context { ...@@ -23,16 +23,6 @@ class RequirementsContext extends Context {
return new static(RequirementsContextDefinition::fromRequirements([], $label), $values); return new static(RequirementsContextDefinition::fromRequirements([], $label), $values);
} }
/**
* Adds values to the context.
*
* @param array<string> $values
* Requirements to add to the context.
*/
public function addValues(array $values): void {
$this->contextValue = array_merge(is_array($this->contextValue) ? $this->contextValue : [], $values);
}
/** /**
* Check if a values is present in the context. * Check if a values is present in the context.
* *
...@@ -43,7 +33,29 @@ class RequirementsContext extends Context { ...@@ -43,7 +33,29 @@ class RequirementsContext extends Context {
* True if the value is present in the context. * True if the value is present in the context.
*/ */
public function hasValue(string $value) : bool { public function hasValue(string $value) : bool {
return is_array($this->contextValue) && in_array($value, $this->contextValue); return in_array($value, $this->getContextValue() ?? []);
}
/**
* Add values to the context_requirements context.
*
* @param array<string> $values
* The values to add to context_requirements context.
* @param array<\Drupal\Core\Plugin\Context\ContextInterface> $contexts
* The contexts.
*
* @return array<\Drupal\Core\Plugin\Context\ContextInterface>
* The contexts.
*/
public static function addToContext(array $values, array $contexts): array {
if (array_key_exists("context_requirements", $contexts) && $contexts["context_requirements"] instanceof RequirementsContext) {
$contexts["context_requirements"] = static::fromValues(array_merge($contexts["context_requirements"]->getContextValue(), $values));
}
else {
$contexts["context_requirements"] = static::fromValues($values);
}
return $contexts;
} }
} }
<?php <?php
namespace Drupal\ui_patterns_field_formatters\Plugin\Derivative; namespace Drupal\ui_patterns\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase; use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Component\Plugin\PluginBase; use Drupal\Component\Plugin\PluginBase;
...@@ -65,6 +65,10 @@ abstract class FieldPropertiesSourceDeriverBase extends DeriverBase implements C ...@@ -65,6 +65,10 @@ abstract class FieldPropertiesSourceDeriverBase extends DeriverBase implements C
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDerivativeDefinitions($base_plugin_definition): array { public function getDerivativeDefinitions($base_plugin_definition): array {
if (!array_key_exists('config_dependencies', $base_plugin_definition) ||
!is_array($base_plugin_definition['config_dependencies'])) {
$base_plugin_definition['config_dependencies'] = [];
}
// For each fieldable entity type, get the field map. // For each fieldable entity type, get the field map.
foreach ($this->entityFieldManager->getFieldMap() as $entity_type_id => $entity_field_map) { foreach ($this->entityFieldManager->getFieldMap() as $entity_type_id => $entity_field_map) {
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id); $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
...@@ -128,7 +132,7 @@ abstract class FieldPropertiesSourceDeriverBase extends DeriverBase implements C ...@@ -128,7 +132,7 @@ abstract class FieldPropertiesSourceDeriverBase extends DeriverBase implements C
$field_name, $field_name,
]), ]),
// Partially filled in. // Partially filled in.
'ui_patterns_field_formatter' => [ 'metadata' => [
'entity_type' => $entity_type_id, 'entity_type' => $entity_type_id,
'field_name' => $field_name, 'field_name' => $field_name,
], ],
...@@ -139,9 +143,6 @@ abstract class FieldPropertiesSourceDeriverBase extends DeriverBase implements C ...@@ -139,9 +143,6 @@ abstract class FieldPropertiesSourceDeriverBase extends DeriverBase implements C
], ],
]); ]);
// Add config_dependencies. // Add config_dependencies.
if (!array_key_exists("config_dependencies", $derivative_for_this_field)) {
$derivative_for_this_field["config_dependencies"] = [];
}
foreach ($bundles as $bundle) { foreach ($bundles as $bundle) {
$field_definitions = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle); $field_definitions = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle);
if (empty($field_definitions[$field_name])) { if (empty($field_definitions[$field_name])) {
...@@ -192,7 +193,7 @@ abstract class FieldPropertiesSourceDeriverBase extends DeriverBase implements C ...@@ -192,7 +193,7 @@ abstract class FieldPropertiesSourceDeriverBase extends DeriverBase implements C
$derivative['id'], $derivative['id'],
$property_id, $property_id,
]); ]);
$derivative['ui_patterns_field_formatter'] = array_merge($derivative['ui_patterns_field_formatter'], [ $derivative['metadata'] = array_merge($derivative['metadata'], [
'property' => $property_id, 'property' => $property_id,
]); ]);
// If no compatible prop types were inferred, we discard the plugin. // If no compatible prop types were inferred, we discard the plugin.
......
<?php <?php
namespace Drupal\ui_patterns_field_formatters\Plugin\Derivative; namespace Drupal\ui_patterns\Plugin\Derivative;
use Drupal\Core\TypedData\DataDefinitionInterface; use Drupal\Core\TypedData\DataDefinitionInterface;
/** /**
* Provides entity field block definitions for every field. * Provides a source for every field prop.
* *
* @internal * @internal
* Plugin derivers are internal. * Plugin derivers are internal.
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
declare(strict_types=1); declare(strict_types=1);
namespace Drupal\ui_patterns_field_formatters\Plugin\UiPatterns\Source; namespace Drupal\ui_patterns\Plugin\UiPatterns\Source;
use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
...@@ -68,7 +68,7 @@ abstract class FieldPropertySourceBase extends FieldSourceBase implements Source ...@@ -68,7 +68,7 @@ abstract class FieldPropertySourceBase extends FieldSourceBase implements Source
* The entity * The entity
*/ */
protected function getEntity(): ?EntityInterface { protected function getEntity(): ?EntityInterface {
$entity = $this->getContextValue('entity'); $entity = parent::getEntity();
if ($entity instanceof EntityInterface) { if ($entity instanceof EntityInterface) {
return $entity; return $entity;
} }
...@@ -102,12 +102,12 @@ abstract class FieldPropertySourceBase extends FieldSourceBase implements Source ...@@ -102,12 +102,12 @@ abstract class FieldPropertySourceBase extends FieldSourceBase implements Source
/** @var \Drupal\Core\Entity\ContentEntityBase $entity */ /** @var \Drupal\Core\Entity\ContentEntityBase $entity */
$entity = $this->getEntity(); $entity = $this->getEntity();
if (!$entity) { if (!$entity) {
$this->getLogger('ui_patterns_field_formatters') $this->getLogger('ui_patterns')
->error('Entity not found in context'); ->error('Entity not found in context');
return NULL; return NULL;
} }
if (!$entity->hasField($field_name)) { if (!$entity->hasField($field_name)) {
$this->getLogger('ui_patterns_field_formatters') $this->getLogger('ui_patterns')
->error('Entity %entity_type %bundle has no field %field_name', [ ->error('Entity %entity_type %bundle has no field %field_name', [
'%entity_type' => $entity->getEntityTypeId(), '%entity_type' => $entity->getEntityTypeId(),
'%bundle' => $entity->bundle() ?? "", '%bundle' => $entity->bundle() ?? "",
...@@ -192,26 +192,6 @@ abstract class FieldPropertySourceBase extends FieldSourceBase implements Source ...@@ -192,26 +192,6 @@ abstract class FieldPropertySourceBase extends FieldSourceBase implements Source
return $value; return $value;
} }
/**
* Get ui patterns field formatter metadata stored in the plugin definition.
*
* @param string $key
* The key name of plugin definition to get data.
*
* @return null|mixed
* The data inside plugin definition or false if error.
*/
protected function getCustomPluginMetadata(string $key): mixed {
$plugin_definition = $this->getPluginDefinition();
if (empty($plugin_definition) ||
!is_array($plugin_definition) ||
!is_array($plugin_definition['ui_patterns_field_formatter']) ||
!array_key_exists($key, $plugin_definition['ui_patterns_field_formatter'])) {
return NULL;
}
return $plugin_definition['ui_patterns_field_formatter'][$key];
}
/** /**
* Returns part of the plugin config specific to UI patterns field formatter. * Returns part of the plugin config specific to UI patterns field formatter.
* *
...@@ -227,7 +207,7 @@ abstract class FieldPropertySourceBase extends FieldSourceBase implements Source ...@@ -227,7 +207,7 @@ abstract class FieldPropertySourceBase extends FieldSourceBase implements Source
* @see setComponentSettingsExtra() * @see setComponentSettingsExtra()
*/ */
protected function getConfiguredSettingsExtra(?string $key = NULL): mixed { protected function getConfiguredSettingsExtra(?string $key = NULL): mixed {
$parents = ['settings', 'ui_patterns_field_formatter']; $parents = ['settings', 'extra'];
if ($key) { if ($key) {
$parents[] = $key; $parents[] = $key;
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
declare(strict_types=1); declare(strict_types=1);
namespace Drupal\ui_patterns_field_formatters\Plugin\UiPatterns\Source; namespace Drupal\ui_patterns\Plugin\UiPatterns\Source;
/** /**
* Plugin implementation of the prop source. * Plugin implementation of the prop source.
...@@ -12,7 +12,7 @@ namespace Drupal\ui_patterns_field_formatters\Plugin\UiPatterns\Source; ...@@ -12,7 +12,7 @@ namespace Drupal\ui_patterns_field_formatters\Plugin\UiPatterns\Source;
* label = @Translation("Field Property (Props)"), * label = @Translation("Field Property (Props)"),
* description = @Translation("Field property source plugin for props."), * description = @Translation("Field property source plugin for props."),
* deriver = * deriver =
* "Drupal\ui_patterns_field_formatters\Plugin\Derivative\FieldPropertiesSourcePropDeriver" * "Drupal\ui_patterns\Plugin\Derivative\FieldPropertiesSourcePropDeriver"
* ) * )
*/ */
class FieldPropertySourceProp extends FieldPropertySourceBase { class FieldPropertySourceProp extends FieldPropertySourceBase {
...@@ -22,7 +22,6 @@ class FieldPropertySourceProp extends FieldPropertySourceBase { ...@@ -22,7 +22,6 @@ class FieldPropertySourceProp extends FieldPropertySourceBase {
*/ */
public function getPropValue(): mixed { public function getPropValue(): mixed {
$items = $this->getEntityFieldItemList(); $items = $this->getEntityFieldItemList();
// Get field index or 0 (first if formatter all).
$delta = $this->getConfiguredSettingsExtra("field_index") ?? 0; $delta = $this->getConfiguredSettingsExtra("field_index") ?? 0;
$lang_code = $this->getConfiguredSettingsExtra("lang_code") ?? "und"; $lang_code = $this->getConfiguredSettingsExtra("lang_code") ?? "und";
$property = $this->getCustomPluginMetadata('property'); $property = $this->getCustomPluginMetadata('property');
......
...@@ -2,8 +2,10 @@ ...@@ -2,8 +2,10 @@
declare(strict_types=1); declare(strict_types=1);
namespace Drupal\ui_patterns_field_formatters\Plugin\UiPatterns\Source; namespace Drupal\ui_patterns\Plugin\UiPatterns\Source;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelTrait; use Drupal\Core\Logger\LoggerChannelTrait;
...@@ -12,7 +14,7 @@ use Drupal\ui_patterns\SourcePluginBase; ...@@ -12,7 +14,7 @@ use Drupal\ui_patterns\SourcePluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Base class for all ui_patterns_field_formatter source plugins. * Base class for all field source plugins.
*/ */
abstract class FieldSourceBase extends SourcePluginBase implements SourceInterface { abstract class FieldSourceBase extends SourcePluginBase implements SourceInterface {
...@@ -62,6 +64,8 @@ abstract class FieldSourceBase extends SourcePluginBase implements SourceInterfa ...@@ -62,6 +64,8 @@ abstract class FieldSourceBase extends SourcePluginBase implements SourceInterfa
* *
* @return string * @return string
* The field name. * The field name.
*
* @throws \Drupal\Component\Plugin\Exception\ContextException
*/ */
protected function getFieldName(): string { protected function getFieldName(): string {
return $this->getContextValue('field_name'); return $this->getContextValue('field_name');
...@@ -72,6 +76,8 @@ abstract class FieldSourceBase extends SourcePluginBase implements SourceInterfa ...@@ -72,6 +76,8 @@ abstract class FieldSourceBase extends SourcePluginBase implements SourceInterfa
* *
* @return string * @return string
* The bundle. * The bundle.
*
* @throws \Drupal\Component\Plugin\Exception\ContextException
*/ */
protected function getBundle(): string { protected function getBundle(): string {
return $this->getContextValue('bundle'); return $this->getContextValue('bundle');
...@@ -82,9 +88,26 @@ abstract class FieldSourceBase extends SourcePluginBase implements SourceInterfa ...@@ -82,9 +88,26 @@ abstract class FieldSourceBase extends SourcePluginBase implements SourceInterfa
* *
* @return string|null * @return string|null
* The entity type id. * The entity type id.
*
* @throws \Drupal\Component\Plugin\Exception\ContextException
*/ */
protected function getEntityTypeId(): ?string { protected function getEntityTypeId(): ?string {
return $this->getContextValue('entity_type_id'); if ($entity = $this->getEntity()) {
return $entity->getEntityTypeId();
}
throw new ContextException("Entity not found");
}
/**
* Returns the entity from context.
*
* @return \Drupal\Core\Entity\EntityInterface|null
* The entity
*
* @throws \Drupal\Component\Plugin\Exception\ContextException
*/
protected function getEntity(): ?EntityInterface {
return $this->getContextValue('entity');
} }
/** /**
......
...@@ -38,11 +38,14 @@ interface SourceInterface extends ConfigurableInterface, PluginInspectionInterfa ...@@ -38,11 +38,14 @@ interface SourceInterface extends ConfigurableInterface, PluginInspectionInterfa
public function alterComponent(array $element): array; public function alterComponent(array $element): array;
/** /**
* Returns contexts used to restrict plugin discovery. * Get metadata stored in the plugin definition.
* *
* @return array * @param string $key
* An array of context definitions. * The key name of plugin definition to get data.
*
* @return null|mixed
* The data inside plugin definition or false if error.
*/ */
public static function getDiscoveryContexts(): array; public function getCustomPluginMetadata(string $key): mixed;
} }
...@@ -13,7 +13,6 @@ use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait; ...@@ -13,7 +13,6 @@ use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
use Drupal\Core\Plugin\ContextAwarePluginInterface; use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginTrait; use Drupal\Core\Plugin\ContextAwarePluginTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\ui_patterns\Plugin\Context\RequirementsContext;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
...@@ -281,37 +280,15 @@ abstract class SourcePluginBase extends PluginBase implements ...@@ -281,37 +280,15 @@ abstract class SourcePluginBase extends PluginBase implements
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getContextDefinitions() { public function getCustomPluginMetadata(string $key): mixed {
return static::getDiscoveryContexts(); $plugin_definition = $this->getPluginDefinition();
} if (empty($plugin_definition) ||
!is_array($plugin_definition) ||
/** !is_array($plugin_definition['metadata']) ||
* {@inheritdoc} !array_key_exists($key, $plugin_definition['metadata'])) {
*/ return NULL;
public static function getDiscoveryContexts() : array {
return [];
}
/**
* Add values to the context_requirements context.
*
* @param array<\Drupal\Core\Plugin\Context\ContextInterface> $context
* The contexts.
* @param array<string> $values
* The values to add to context_requirements context.
*
* @return array<\Drupal\Core\Plugin\Context\ContextInterface>
* The contexts.
*/
public static function addRequirementsToContext(array $context, array $values): array {
if (array_key_exists("context_requirements", $context) && $context["context_requirements"] instanceof RequirementsContext) {
$context["context_requirements"]->addValues($values);
}
else {
$context["context_requirements"] = RequirementsContext::fromValues($values);
} }
return $plugin_definition['metadata'][$key];
return $context;
} }
} }
...@@ -4,7 +4,6 @@ declare(strict_types=1); ...@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Drupal\ui_patterns; namespace Drupal\ui_patterns;
use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\Context\ContextAwarePluginManagerInterface; use Drupal\Core\Plugin\Context\ContextAwarePluginManagerInterface;
...@@ -50,13 +49,6 @@ class SourcePluginManager extends DefaultPluginManager implements ContextAwarePl ...@@ -50,13 +49,6 @@ class SourcePluginManager extends DefaultPluginManager implements ContextAwarePl
*/ */
public function processDefinition(&$definition, $plugin_id) { public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id); parent::processDefinition($definition, $plugin_id);
$pluginClass = ($definition instanceof PluginDefinitionInterface) ? $definition->getClass() : $definition['class'];
if (method_exists($pluginClass, 'getDiscoveryContexts')) {
$definition["context_definitions"] = array_merge(
array_key_exists("context_definitions", $definition) ? $definition["context_definitions"] : [],
$pluginClass::getDiscoveryContexts()
);
}
if (array_key_exists("context_requirements", $definition) && count($definition["context_requirements"]) > 0) { if (array_key_exists("context_requirements", $definition) && count($definition["context_requirements"]) > 0) {
$definition["context_definitions"]["context_requirements"] = RequirementsContextDefinition::fromRequirements($definition["context_requirements"]); $definition["context_definitions"]["context_requirements"] = RequirementsContextDefinition::fromRequirements($definition["context_requirements"]);
} }
...@@ -68,7 +60,8 @@ class SourcePluginManager extends DefaultPluginManager implements ContextAwarePl ...@@ -68,7 +60,8 @@ class SourcePluginManager extends DefaultPluginManager implements ContextAwarePl
public function getDefinitionsForPropType(string $prop_type_id, array $contexts = [], array $tag_filter = []): array { public function getDefinitionsForPropType(string $prop_type_id, array $contexts = [], array $tag_filter = []): array {
$definitions = $this->getDefinitionsForContexts($contexts); $definitions = $this->getDefinitionsForContexts($contexts);
$definitions = array_filter($definitions, function ($definition) use ($prop_type_id, $tag_filter) { $definitions = array_filter($definitions, function ($definition) use ($prop_type_id, $tag_filter) {
if (!in_array($prop_type_id, $definition['prop_types'])) { $supported_prop_types = array_key_exists("prop_types", $definition) ? $definition['prop_types'] : [];
if (is_array($supported_prop_types) && (count($supported_prop_types) > 0) && !in_array($prop_type_id, $supported_prop_types)) {
return FALSE; return FALSE;
} }
if (count($tag_filter) > 0) { if (count($tag_filter) > 0) {
...@@ -88,18 +81,24 @@ class SourcePluginManager extends DefaultPluginManager implements ContextAwarePl ...@@ -88,18 +81,24 @@ class SourcePluginManager extends DefaultPluginManager implements ContextAwarePl
/** /**
* Returns the default source identifier for a prop type. * Returns the default source identifier for a prop type.
*/ */
public function getPropTypeDefault(string $prop_type_id, array $contexts = []): ?string { public function getPropTypeDefault(string $prop_type_id, array $contexts = [], array $tag_filter = []): ?string {
// First try with prop type default source. // First try with prop type default source.
$prop_type_definition = $this->prop_type_manager->getDefinition($prop_type_id); $prop_type_definition = $this->prop_type_manager->getDefinition($prop_type_id);
$source_id = $prop_type_definition["default_source"] ?? NULL; $default_source_id = $prop_type_definition["default_source"] ?? NULL;
if ($source_id && $this->isApplicable($source_id, $contexts)) { if (!$tag_filter && $default_source_id && $this->isApplicable($default_source_id, $contexts)) {
return $source_id; return $default_source_id;
} }
// If no default source, take the first one. // If no default source, take the first one.
$definitions = $this->getDefinitionsForPropType($prop_type_id, $contexts); $definitions = $this->getDefinitionsForPropType($prop_type_id, $contexts, $tag_filter);
$source_id = array_key_first($definitions) ?? NULL; if ($tag_filter && $default_source_id && array_key_exists($default_source_id, $definitions) &&
if ($source_id && $this->isApplicable($source_id, $contexts)) { $this->isApplicable($default_source_id, $contexts)) {
return $source_id; return $default_source_id;
}
$source_ids = array_keys($definitions);
foreach ($source_ids as $source_id) {
if ($source_id && $this->isApplicable($source_id, $contexts)) {
return $source_id;
}
} }
return NULL; return NULL;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment