You are here

public static function AddressDefaultFormatter::replacePlaceholders in Address 8

Replaces placeholders in the given string.

Parameters

string $string: The string containing the placeholders.

array $replacements: An array of replacements keyed by their placeholders.

Return value

string The processed string.

1 call to AddressDefaultFormatter::replacePlaceholders()
AddressDefaultFormatter::postRender in src/Plugin/Field/FieldFormatter/AddressDefaultFormatter.php
Inserts the rendered elements into the format string.

File

src/Plugin/Field/FieldFormatter/AddressDefaultFormatter.php, line 225

Class

AddressDefaultFormatter
Plugin implementation of the 'address_default' formatter.

Namespace

Drupal\address\Plugin\Field\FieldFormatter

Code

public static function replacePlaceholders($string, array $replacements) {

  // Make sure the replacements don't have any unneeded newlines.
  $replacements = array_map('trim', $replacements);
  $string = strtr($string, $replacements);

  // Remove noise caused by empty placeholders.
  $lines = explode("\n", $string);
  foreach ($lines as $index => $line) {

    // Remove leading punctuation, excess whitespace.
    $line = trim(preg_replace('/^[-,]+/', '', $line, 1));
    $line = preg_replace('/\\s\\s+/', ' ', $line);
    $lines[$index] = $line;
  }

  // Remove empty lines.
  $lines = array_filter($lines);
  return implode("\n", $lines);
}