Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| PluginItem | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 4 |
20 | |
0.00% |
0 / 1 |
| defaultFieldSettings | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| propertyDefinitions | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| schema | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getInstance | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\display_builder\Plugin\Field\FieldType; |
| 6 | |
| 7 | use Drupal\Core\Field\Attribute\FieldType; |
| 8 | use Drupal\Core\Field\FieldItemBase; |
| 9 | use Drupal\Core\Field\FieldItemList; |
| 10 | use Drupal\Core\Field\FieldStorageDefinitionInterface; |
| 11 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
| 12 | use Drupal\Core\TypedData\DataDefinition; |
| 13 | use Drupal\Core\TypedData\MapDataDefinition; |
| 14 | |
| 15 | /** |
| 16 | * Store a plugin ID and its configuration to easily create instances. |
| 17 | */ |
| 18 | #[FieldType( |
| 19 | id: 'plugin', |
| 20 | label: new TranslatableMarkup('Plugin'), |
| 21 | list_class: FieldItemList::class, |
| 22 | no_ui: TRUE, |
| 23 | )] |
| 24 | class PluginItem extends FieldItemBase { |
| 25 | |
| 26 | /** |
| 27 | * {@inheritdoc} |
| 28 | */ |
| 29 | public static function defaultFieldSettings() { |
| 30 | return [ |
| 31 | 'plugin_manager_id' => '', |
| 32 | ] + parent::defaultFieldSettings(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * {@inheritdoc} |
| 37 | */ |
| 38 | public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition): array { |
| 39 | $definitions = []; |
| 40 | // @see FactoryInterface::createInstance() |
| 41 | // The ID of the plugin being instantiated. |
| 42 | $definitions['plugin_id'] = DataDefinition::create('string')->setRequired(TRUE); |
| 43 | // An array of configuration relevant to the plugin instance. |
| 44 | $definitions['configuration'] = MapDataDefinition::create(); |
| 45 | |
| 46 | return $definitions; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * {@inheritdoc} |
| 51 | */ |
| 52 | public static function schema(FieldStorageDefinitionInterface $field_definition): array { |
| 53 | return [ |
| 54 | 'columns' => [ |
| 55 | 'plugin_id' => [ |
| 56 | 'type' => 'varchar_ascii', |
| 57 | 'length' => 255, |
| 58 | ], |
| 59 | 'configuration' => [ |
| 60 | 'type' => 'blob', |
| 61 | 'size' => 'big', |
| 62 | 'serialize' => TRUE, |
| 63 | ], |
| 64 | ], |
| 65 | ]; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get plugin instance. |
| 70 | * |
| 71 | * @return object |
| 72 | * An instantiated plugin instance. |
| 73 | */ |
| 74 | public function getInstance(): object { |
| 75 | $service_id = $this->getSetting('plugin_manager_id'); |
| 76 | /** @var \Drupal\Component\Plugin\PluginManagerInterface $service */ |
| 77 | $service = \Drupal::service($service_id); |
| 78 | |
| 79 | return $service->createInstance( |
| 80 | $this->get('plugin_id')->getString(), |
| 81 | $this->get('configuration')->getValue() ?? [], |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | } |