You are here

trait MergeablePluginDefinitionTrait in Plugin 8.2

Implements the plugin merger parts of \Drupal\plugin\PluginDefinition\PluginDefinitionInterface.

Hierarchy

1 file declares its use of MergeablePluginDefinitionTrait
MergeablePluginDefinitionTraitTest.php in tests/src/Unit/PluginDefinition/MergeablePluginDefinitionTraitTest.php

File

src/PluginDefinition/MergeablePluginDefinitionTrait.php, line 10

Namespace

Drupal\plugin\PluginDefinition
View source
trait MergeablePluginDefinitionTrait {

  /**
   * Implements \Drupal\plugin\PluginDefinition\PluginDefinitionInterface::mergeDefaultDefinition().
   */
  public function mergeDefaultDefinition(PluginDefinitionInterface $other_definition) {
    $this
      ->validateMergeDefinition($other_definition);
    $this
      ->doMergeDefaultDefinition($other_definition);
    return $this;
  }

  /**
   * Merges another definition into this one, using the other for defaults.
   *
   * @param static $other_definition
   *   The other definition to merge into $this. It will not override $this, but
   *   be used to extract default values from instead.
   */
  protected function doMergeDefaultDefinition(PluginDefinitionInterface $other_definition) {

    // Child classes can override this to perform an actual merge.
  }

  /**
   * Implements \Drupal\plugin\PluginDefinition\PluginDefinitionInterface::mergeOverrideDefinition().
   */
  public function mergeOverrideDefinition(PluginDefinitionInterface $other_definition) {
    $this
      ->validateMergeDefinition($other_definition);
    $this
      ->doMergeOverrideDefinition($other_definition);
    return $this;
  }

  /**
   * Merges another definition into this one, using the other for overrides.
   *
   * @param static $other_definition
   *   The other definition to merge into $this. It will override any values
   *   already set in $this.
   */
  protected function doMergeOverrideDefinition(PluginDefinitionInterface $other_definition) {

    // Child classes can override this to perform an actual merge.
  }

  /**
   * Validates whether another definition is compatible with this one.
   *
   * @throws \InvalidArgumentException
   */
  protected function validateMergeDefinition(PluginDefinitionInterface $other_definition) {
    if (!$this
      ->isDefinitionCompatible($other_definition)) {
      throw new \InvalidArgumentException(sprintf('$other_definition must be an instance of %s, but %s was given.', get_class($this), get_class($other_definition)));
    }
  }

  /**
   * Returns whether another definition is compatible with this one.
   *
   * @return bool
   *   Whether or not the definition is compatible with $this.
   */
  protected abstract function isDefinitionCompatible(PluginDefinitionInterface $other_definition);

}

Members

Namesort descending Modifiers Type Description Overrides
MergeablePluginDefinitionTrait::doMergeDefaultDefinition protected function Merges another definition into this one, using the other for defaults. 1
MergeablePluginDefinitionTrait::doMergeOverrideDefinition protected function Merges another definition into this one, using the other for overrides. 1
MergeablePluginDefinitionTrait::isDefinitionCompatible abstract protected function Returns whether another definition is compatible with this one. 1
MergeablePluginDefinitionTrait::mergeDefaultDefinition public function Implements \Drupal\plugin\PluginDefinition\PluginDefinitionInterface::mergeDefaultDefinition().
MergeablePluginDefinitionTrait::mergeOverrideDefinition public function Implements \Drupal\plugin\PluginDefinition\PluginDefinitionInterface::mergeOverrideDefinition().
MergeablePluginDefinitionTrait::validateMergeDefinition protected function Validates whether another definition is compatible with this one.