Completed
Pull Request — master (#157)
by
unknown
01:57
created

DriverFieldPluginBase::isFinal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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