Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/Context/ContextDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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',
Expand All @@ -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.
*
Expand Down Expand Up @@ -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 [];
}

}
26 changes: 26 additions & 0 deletions src/Context/ContextDefinitionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}