You are here

class ConfigNormalizerSort in Configuration Normalizer 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/ConfigNormalizer/ConfigNormalizerSort.php \Drupal\config_normalizer\Plugin\ConfigNormalizer\ConfigNormalizerSort

Recursively sorts a configuration array.

Plugin annotation


@ConfigNormalizer(
  id = "sort",
  label = @Translation("sort"),
  weight = 20,
  description = @Translation("Recursively sorts a configuration array."),
)

Hierarchy

Expanded class hierarchy of ConfigNormalizerSort

Deprecated

in config_normalizer:2.0.0-alpha1 and is removed from config_normalizer:2.0.0. No replacement.

See also

https://www.drupal.org/project/config_normalizer/issues/3230398

File

src/Plugin/ConfigNormalizer/ConfigNormalizerSort.php, line 22

Namespace

Drupal\config_normalizer\Plugin\ConfigNormalizer
View source
class ConfigNormalizerSort extends ConfigNormalizerBase implements ContainerFactoryPluginInterface {

  /**
   * {@inheritdoc}
   */
  public function normalize($name, array &$data, array $context) {

    // Only sort if the normalization mode is default.
    if ($this
      ->isDefaultModeContext($context)) {

      // Recursively normalize and return.
      $data = $this
        ->normalizeArray($data);
    }
  }

  /**
   * Recursively sorts an array by key.
   *
   * @param array $array
   *   An array to normalize.
   *
   * @return array
   *   An array that is sorted by key, at each level of the array, with empty
   *   arrays removed.
   */
  protected function normalizeArray(array $array) {
    foreach ($array as $key => $value) {
      if (is_array($value)) {
        $new = $this
          ->normalizeArray($value);
        if (count($new)) {
          $array[$key] = $new;
        }
      }
    }

    // If the array is associative, sort by key.
    if (Inline::isHash($array)) {
      ksort($array);
    }
    else {
      sort($array);
    }
    return $array;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigNormalizerBase::$entityTypeManager protected property The entity type manager.
ConfigNormalizerBase::create public static function
ConfigNormalizerBase::isActiveStorageContext protected function Determines whether the context reference storage is the active storage.
ConfigNormalizerBase::isDefaultModeContext protected function Determines whether the context has a default normalization mode.
ConfigNormalizerBase::__construct public function Creates a new config normalizer plugin. Overrides PluginBase::__construct
ConfigNormalizerSort::normalize public function Normalizes config for comparison. Overrides ConfigNormalizerInterface::normalize
ConfigNormalizerSort::normalizeArray protected function Recursively sorts an array by key.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.