|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\Driver\Plugin; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Component\Plugin\PluginBase; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
7
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Base class for Driver field plugins. |
|
11
|
|
|
*/ |
|
12
|
|
|
class DriverFieldPluginBase extends PluginBase implements DriverFieldPluginInterface, ContainerFactoryPluginInterface |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The field object this plugin is processing values for. |
|
17
|
|
|
* |
|
18
|
|
|
* @var \Drupal\Driver\Wrapper\Field\DriverFieldInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $field; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritdoc} |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct( |
|
26
|
|
|
array $configuration, |
|
27
|
|
|
$plugin_id, |
|
28
|
|
|
$plugin_definition |
|
29
|
|
|
) { |
|
30
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition); |
|
31
|
|
|
|
|
32
|
|
|
$this->field = $configuration['field']; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function create( |
|
39
|
|
|
ContainerInterface $container, |
|
40
|
|
|
array $configuration, |
|
41
|
|
|
$plugin_id, |
|
42
|
|
|
$plugin_definition |
|
43
|
|
|
) { |
|
44
|
|
|
return new static( |
|
45
|
|
|
$configuration, |
|
46
|
|
|
$plugin_id, |
|
47
|
|
|
$plugin_definition |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function isFinal($field) |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->pluginDefinition['final']; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
public function processValues($values) |
|
63
|
|
|
{ |
|
64
|
|
|
if (!is_array($values)) { |
|
65
|
|
|
throw new \Exception("Values must be an array"); |
|
66
|
|
|
} |
|
67
|
|
|
$processed = []; |
|
68
|
|
|
foreach ($values as $value) { |
|
69
|
|
|
$value = $this->assignPropertyNames($value); |
|
70
|
|
|
$processed[] = $this->processValue($value); |
|
71
|
|
|
} |
|
72
|
|
|
return $processed; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Converts a single instruction into an array of field properties for |
|
77
|
|
|
* content fields. We want to allow plugins to be called with field properties |
|
78
|
|
|
* already explicitly specified, but also need to allow for more cryptic |
|
79
|
|
|
* inputs that the plugin has to decipher. |
|
80
|
|
|
* |
|
81
|
|
|
* @return string|array |
|
82
|
|
|
* returns the array of field properties for one field value. |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function assignPropertyNames($value) |
|
85
|
|
|
{ |
|
86
|
|
|
// Keep config properties simple. |
|
87
|
|
|
if ($this->field->isConfigProperty()) { |
|
88
|
|
|
} // Convert simple string |
|
89
|
|
|
elseif (!is_array($value)) { |
|
90
|
|
|
$value = [$this->getMainPropertyName() => $value]; |
|
91
|
|
|
} // Convert single item unkeyed array. |
|
92
|
|
|
elseif (array_keys($value) === [0]) { |
|
93
|
|
|
$value = [$this->getMainPropertyName() => $value[0]]; |
|
94
|
|
|
} |
|
95
|
|
|
return $value; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Gets the default column name to use for field values. |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
* The default column name for this field type. |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function getMainPropertyName() |
|
105
|
|
|
{ |
|
106
|
|
|
if ($this->field->isConfigProperty()) { |
|
107
|
|
|
throw new \Exception("Main property name not used when processing config properties."); |
|
108
|
|
|
} |
|
109
|
|
|
return $this->pluginDefinition['mainPropertyName']; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Processes the properties for a single field value. |
|
114
|
|
|
* |
|
115
|
|
|
* @param array $value |
|
116
|
|
|
* An array of field properties. |
|
117
|
|
|
* |
|
118
|
|
|
* @return array |
|
119
|
|
|
* returns the array of column values for one field value. |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function processValue($value) |
|
122
|
|
|
{ |
|
123
|
|
|
return $value; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|