ConfigModuleOverridesEvent.php 2.3 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\Core\Config;

use Drupal\Component\Utility\NestedArray;
6
use Drupal\Core\Language\LanguageInterface;
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
use Symfony\Component\EventDispatcher\Event;

/**
 * Event object to allow configuration to be overridden by modules.
 */
class ConfigModuleOverridesEvent extends Event {

  /**
   * Configuration names.
   *
   * @var array
   */
  protected $names;

  /**
   * Configuration overrides.
   *
   * @var array
   */
  protected $overrides;

  /**
   * The Language object used to override configuration data.
   *
31
   * @var \Drupal\Core\Language\LanguageInterface
32 33 34 35 36 37 38 39
   */
  protected $language;

  /**
   * Constructs a configuration overrides event object.
   *
   * @param array $names
   *   A list of configuration names.
40
   * @param \Drupal\Core\Language\LanguageInterface
41 42
   *   (optional) The language for this configuration.
   */
43
  public function __construct(array $names, LanguageInterface $language = NULL) {
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    $this->names = $names;
    $this->language = $language;
    $this->overrides = array();
  }

  /**
   * Gets configuration names.
   *
   * @return array
   *   The list of configuration names that can be overridden.
   */
  public function getNames() {
    return $this->names;
  }

  /**
   * Gets configuration language.
   *
62
   * @return \Drupal\Core\Language\LanguageInterface
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
   *   The configuration language object.
   */
  public function getLanguage() {
    return $this->language;
  }

  /**
   * Get configuration overrides.
   *
   * @return array.
   *   The array of configuration overrides.
   */
  public function getOverrides() {
    return $this->overrides;
  }

  /**
   * Sets a configuration override for the given name.
   *
   * @param string $name
   *   The configuration object name to override.
   * @param array $values
   *   The values in the configuration object to override.
   *
87
   * @return $this
88 89 90 91 92 93 94 95 96 97 98 99 100 101
   */
  public function setOverride($name, array $values) {
    if (in_array($name, $this->names)) {
      if (isset($this->overrides[$name])) {
        // Existing overrides take precedence since these will have been added
        // by events with a higher priority.
        $this->overrides[$name] = NestedArray::mergeDeepArray(array($values, $this->overrides[$name]), TRUE);
      }
      else {
        $this->overrides[$name] = $values;
      }
    }
    return $this;
  }
102

103
}