You are here

private function USPSBase::preparePluginDefinition in Commerce USPS 8

Prepares the service array keys to support integer values.

Parameters

array $plugin_definition: The plugin definition provided to the class.

Return value

array The prepared plugin definition.

1 call to USPSBase::preparePluginDefinition()
USPSBase::__construct in src/Plugin/Commerce/ShippingMethod/USPSBase.php
Constructs a new ShippingMethodBase object.

File

src/Plugin/Commerce/ShippingMethod/USPSBase.php, line 202

Class

USPSBase

Namespace

Drupal\commerce_usps\Plugin\Commerce\ShippingMethod

Code

private function preparePluginDefinition(array $plugin_definition) {

  // Cache and unset the parsed plugin definitions for services.
  $services = $plugin_definition['services'];
  unset($plugin_definition['services']);

  // Loop over each service definition and redefine them with
  // integer keys that match the UPS API.
  // TODO: Remove once core issue has been addressed.
  // See: https://www.drupal.org/node/2904467 for more information.
  foreach ($services as $key => $service) {

    // Remove the "_" from the service key.
    $key_trimmed = str_replace('_', '', $key);
    $plugin_definition['services'][$key_trimmed] = $service;
  }

  // Sort the options alphabetically.
  uasort($plugin_definition['services'], function (TranslatableMarkup $a, TranslatableMarkup $b) {
    return $a
      ->getUntranslatedString() < $b
      ->getUntranslatedString() ? -1 : 1;
  });
  return $plugin_definition;
}