summaryrefslogtreecommitdiffstatshomepage
path: root/core/lib/Drupal/Core/Plugin/ConfigurableTrait.php
blob: bfe4a6615127b49904606487ed8990c631574d6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php

declare(strict_types=1);

namespace Drupal\Core\Plugin;

use Drupal\Component\Utility\NestedArray;

/**
 * Implementation class for \Drupal\Component\Plugin\ConfigurableInterface.
 *
 * In order for configurable plugins to maintain their configuration, the
 * default configuration must be merged into any explicitly defined
 * configuration. This trait provides the appropriate getters and setters to
 * handle this logic, removing the need for excess boilerplate.
 *
 * To use this trait implement ConfigurableInterface and add a constructor. In
 * the constructor call the parent constructor and then call setConfiguration().
 * That will merge the explicitly defined plugin configuration and the default
 * plugin configuration.
 *
 * @ingroup Plugin
 */
trait ConfigurableTrait {

  /**
   * Configuration information passed into the plugin.
   *
   * This property is declared in \Drupal\Component\Plugin\PluginBase as well,
   * which most classes using this trait will ultimately be extending. It is
   * re-declared here to make the trait self-contained and to permit use of the
   * trait in classes that do not extend PluginBase.
   *
   * @var array
   */
  protected $configuration;

  /**
   * Gets this plugin's configuration.
   *
   * @return array
   *   An associative array containing the plugin's configuration.
   *
   * @see \Drupal\Component\Plugin\ConfigurableInterface::getConfiguration()
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * Sets the configuration for this plugin instance.
   *
   * The provided configuration is merged with the plugin's default
   * configuration. If the same configuration key exists in both configurations,
   * then the value in the provided configuration will override the default.
   *
   * @param array $configuration
   *   An associative array containing the plugin's configuration.
   *
   * @return $this
   *
   * @see \Drupal\Component\Plugin\ConfigurableInterface::setConfiguration()
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = NestedArray::mergeDeepArray([$this->defaultConfiguration(), $configuration], TRUE);
    return $this;
  }

  /**
   * Gets default configuration for this plugin.
   *
   * @return array
   *   An associative array containing the default configuration.
   *
   * @see \Drupal\Component\Plugin\ConfigurableInterface::defaultConfiguration()
   */
  public function defaultConfiguration() {
    return [];
  }

}