EmbedTypeBase   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A calculateDependencies() 0 3 1
A defaultConfiguration() 0 3 1
A getConfiguration() 0 3 1
A getConfigurationValue() 0 9 2
A setConfiguration() 0 3 1
A setConfigurationValue() 0 3 1
A buildConfigurationForm() 0 3 1
A validateConfigurationForm() 0 3 1
A submitConfigurationForm() 0 10 2
1
<?php
2
3
namespace Drupal\embed\EmbedType;
4
5
use Drupal\Component\Utility\NestedArray;
6
use Drupal\Core\Form\FormStateInterface;
7
use Drupal\Core\Plugin\PluginBase;
8
9
/**
10
 * Defines a base implementation that most embed type plugins will extend.
11
 *
12
 * @ingroup embed_api
13
 */
14
abstract class EmbedTypeBase extends PluginBase implements EmbedTypeInterface {
15
16
  /**
17
   * {@inheritdoc}
18
   */
19
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
20
    parent::__construct($configuration, $plugin_id, $plugin_definition);
21
    $this->configuration = NestedArray::mergeDeep($this->defaultConfiguration(), $this->configuration);
22
  }
23
24
  /**
25
   * {@inheritdoc}
26
   */
27
  public function calculateDependencies() {
28
    return [];
29
  }
30
31
  /**
32
   * {@inheritdoc}
33
   */
34
  public function defaultConfiguration() {
35
    return [];
36
  }
37
38
  /**
39
   * {@inheritdoc}
40
   */
41
  public function getConfiguration() {
42
    return $this->configuration;
43
  }
44
45
  /**
46
   * {@inheritdoc}
47
   */
48
  public function getConfigurationValue($name, $default = NULL) {
49
    $configuration = $this->getConfiguration();
50
    if (array_key_exists($name, $configuration)) {
51
      return $configuration[$name];
52
    }
53
    else {
54
      return $default;
55
    }
56
  }
57
58
  /**
59
   * {@inheritdoc}
60
   */
61
  public function setConfiguration(array $configuration) {
62
    $this->configuration = $configuration;
63
  }
64
65
  /**
66
   * {@inheritdoc}
67
   */
68
  public function setConfigurationValue($name, $value) {
69
    $this->configuration[$name] = $value;
70
  }
71
72
  /**
73
   * {@inheritdoc}
74
   */
75
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
76
    return $form;
77
  }
78
79
  /**
80
   * {@inheritdoc}
81
   */
82
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
83
    // Do nothing.
84
  }
85
86
  /**
87
   * {@inheritdoc}
88
   */
89
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
90
    if (!$form_state->hasAnyErrors()) {
91
      $this->setConfiguration(
92
        array_intersect_key(
93
          $form_state->getValues(),
94
          $this->defaultConfiguration()
95
        )
96
      );
97
    }
98
  }
99
100
}
101