ConfigFactoryOverrideBase.php 3.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

namespace Drupal\Core\Config;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Defines a base event listener implementation configuration overrides.
 */
abstract class ConfigFactoryOverrideBase implements EventSubscriberInterface {

  /**
   * Reacts to the ConfigEvents::COLLECTION_INFO event.
   *
   * @param \Drupal\Core\Config\ConfigCollectionInfo $collection_info
16
   *   The configuration collection info event.
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
   */
  abstract public function addCollections(ConfigCollectionInfo $collection_info);

  /**
   * Actions to be performed to configuration override on configuration save.
   *
   * @param \Drupal\Core\Config\ConfigCrudEvent $event
   *   The config CRUD event.
   */
  abstract public function onConfigSave(ConfigCrudEvent $event);

  /**
   * Actions to be performed to configuration override on configuration delete.
   *
   * @param \Drupal\Core\Config\ConfigCrudEvent $event
   *   The config CRUD event.
   */
  abstract public function onConfigDelete(ConfigCrudEvent $event);

  /**
   * Actions to be performed to configuration override on configuration rename.
   *
   * @param \Drupal\Core\Config\ConfigRenameEvent $event
   *   The config rename event.
   */
  abstract public function onConfigRename(ConfigRenameEvent $event);

  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    $events[ConfigEvents::COLLECTION_INFO][] = array('addCollections');
    $events[ConfigEvents::SAVE][] = array('onConfigSave', 20);
    $events[ConfigEvents::DELETE][] = array('onConfigDelete', 20);
    $events[ConfigEvents::RENAME][] = array('onConfigRename', 20);
    return $events;
  }

  /**
   * Filters data in the override based on what is currently in configuration.
   *
   * @param \Drupal\Core\Config\Config $config
   *   Current configuration object.
   * @param \Drupal\Core\Config\StorableConfigBase $override
   *   Override object corresponding to the configuration to filter data in.
   */
  protected function filterOverride(Config $config, StorableConfigBase $override) {
    $override_data = $override->get();
65
    $changed = $this->filterNestedArray($config->get(), $override_data);
66 67 68 69
    if (empty($override_data)) {
      // If no override values are left that would apply, remove the override.
      $override->delete();
    }
70
    elseif ($changed) {
71
      // Otherwise set the filtered override values back.
72
      $override->setData($override_data)->save(TRUE);
73 74 75 76 77 78 79 80 81 82
    }
  }

  /**
   * Filters data in nested arrays.
   *
   * @param array $original_data
   *   Original data array to filter against.
   * @param array $override_data
   *   Override data to filter.
83 84 85
   *
   * @return bool
   *   TRUE if $override_data was changed, FALSE otherwise.
86 87
   */
  protected function filterNestedArray(array $original_data, array &$override_data) {
88
    $changed = FALSE;
89 90 91 92
    foreach ($override_data as $key => $value) {
      if (!isset($original_data[$key])) {
        // The original data is not there anymore, remove the override.
        unset($override_data[$key]);
93
        $changed = TRUE;
94 95 96 97
      }
      elseif (is_array($override_data[$key])) {
        if (is_array($original_data[$key])) {
          // Do the filtering one level deeper.
98 99 100 101
          // Ensure that we track $changed along the way.
          if ($this->filterNestedArray($original_data[$key], $override_data[$key])) {
            $changed = TRUE;
          }
102 103 104
          // If no overrides are left under this level, remove the level.
          if (empty($override_data[$key])) {
            unset($override_data[$key]);
105
            $changed = TRUE;
106 107 108 109 110 111
          }
        }
        else {
          // The override is an array but the value is not, this will not go
          // well, remove the override.
          unset($override_data[$key]);
112
          $changed = TRUE;
113 114 115
        }
      }
    }
116
    return $changed;
117 118 119
  }

}