From 3e6c5642b6b3fa0c546e6c523e7fb623213899dd Mon Sep 17 00:00:00 2001 From: Maksym Kuzomko Date: Sat, 27 Jan 2018 16:21:38 +0200 Subject: [PATCH] Issue #2471481 by max-kuzomko, acrollet: Use suited widgets based on the data type. --- src/Context/ContextDefinition.php | 74 ++++++++++++++++++++++ src/Context/ContextDefinitionInterface.php | 26 ++++++++ 2 files changed, 100 insertions(+) diff --git a/src/Context/ContextDefinition.php b/src/Context/ContextDefinition.php index 7122d6ec..2a19aa62 100644 --- a/src/Context/ContextDefinition.php +++ b/src/Context/ContextDefinition.php @@ -4,12 +4,23 @@ use Drupal\Core\Plugin\Context\ContextDefinition as ContextDefinitionCore; use Drupal\Component\Plugin\Exception\ContextException; +use Drupal\Core\TypedData\Plugin\DataType\Email; +use Drupal\Core\TypedData\Type\DateTimeInterface; +use Drupal\Core\TypedData\Type\DurationInterface; +use Drupal\Core\TypedData\Type\FloatInterface; +use Drupal\Core\TypedData\Type\IntegerInterface; +use Drupal\Core\TypedData\OptionsProviderInterface; +use Drupal\Core\TypedData\Type\StringInterface; +use Drupal\Core\TypedData\Type\UriInterface; +use Drupal\typed_data\Widget\FormWidgetManagerTrait; /** * Extends the core context definition class with useful methods. */ class ContextDefinition extends ContextDefinitionCore implements ContextDefinitionInterface { + use FormWidgetManagerTrait; + /** * The mapping of config export keys to internal properties. * @@ -18,6 +29,7 @@ class ContextDefinition extends ContextDefinitionCore implements ContextDefiniti protected static $nameMap = [ 'type' => 'dataType', 'label' => 'label', + 'widget_id' => 'widgetId', 'description' => 'description', 'multiple' => 'isMultiple', 'required' => 'isRequired', @@ -27,6 +39,13 @@ class ContextDefinition extends ContextDefinitionCore implements ContextDefiniti 'assignment_restriction' => 'assignmentRestriction', ]; + /** + * The Typed Data widget ID to be used. + * + * @var string + */ + protected $widgetId; + /** * Whether the context value is allowed to be NULL or not. * @@ -117,4 +136,59 @@ public function setAssignmentRestriction($restriction) { return $this; } + /** + * {@inheritdoc} + */ + public function getWidgetId() { + $data_definition = $this->getDataDefinition(); + + if ($this->widgetId) { + $widget = $this->getFormWidgetManager()->createInstance($this->widgetId); + + if ($widget->isApplicable($data_definition)) { + return $this->widgetId; + } + } + + $widgets = [ + 'text_input' => [ + Email::class, + DateTimeInterface::class, + DurationInterface::class, + FloatInterface::class, + IntegerInterface::class, + UriInterface::class, + StringInterface::class, + ], + 'select' => [ + OptionsProviderInterface::class, + ], + ]; + + foreach ($widgets as $widget_id => $data_types) { + foreach ($data_types as $data_type) { + if (is_subclass_of($data_definition->getClass(), $data_type)) { + return $widget_id; + } + } + } + + return 'broken'; + } + + /** + * {@inheritdoc} + */ + public function setWidgetId($widget_id) { + $this->widgetId = $widget_id; + return $this; + } + + /** + * {@inheritdoc} + */ + public function getWidgetSettings() { + return []; + } + } diff --git a/src/Context/ContextDefinitionInterface.php b/src/Context/ContextDefinitionInterface.php index 118627ca..65ee7c79 100644 --- a/src/Context/ContextDefinitionInterface.php +++ b/src/Context/ContextDefinitionInterface.php @@ -68,4 +68,30 @@ public function setAssignmentRestriction($restriction); */ public function toArray(); + /** + * Gets a typed data widget id. + * + * @return string + * The typed data widget id. By default returns the "broken" widget. + */ + public function getWidgetId(); + + /** + * Sets the typed data widget id. + * + * @param string $widget_id + * The typed data widget id. + * + * @return $this + */ + public function setWidgetId($widget_id); + + /** + * Gets default configuration of the widget. + * + * @return array + * An associative array with the default configuration. + */ + public function getWidgetSettings(); + }