You are here

public static function AddressDefaultFormatter::postRender in Address 8

Inserts the rendered elements into the format string.

Parameters

string $content: The rendered element.

array $element: An associative array containing the properties and children of the element.

Return value

string The new rendered element.

File

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

Class

AddressDefaultFormatter
Plugin implementation of the 'address_default' formatter.

Namespace

Drupal\address\Plugin\Field\FieldFormatter

Code

public static function postRender($content, array $element) {

  /** @var \CommerceGuys\Addressing\AddressFormat\AddressFormat $address_format */
  $address_format = $element['#address_format'];
  $locale = $element['#locale'];

  // Add the country to the bottom or the top of the format string,
  // depending on whether the format is minor-to-major or major-to-minor.
  if (Locale::matchCandidates($address_format
    ->getLocale(), $locale)) {
    $format_string = '%country' . "\n" . $address_format
      ->getLocalFormat();
  }
  else {
    $format_string = $address_format
      ->getFormat() . "\n" . '%country';
  }
  $replacements = [];
  foreach (Element::getVisibleChildren($element) as $key) {
    $child = $element[$key];
    if (isset($child['#placeholder'])) {
      $replacements[$child['#placeholder']] = $child['#value'] ? $child['#markup'] : '';
    }
  }
  $content = self::replacePlaceholders($format_string, $replacements);
  $content = nl2br($content, FALSE);
  return $content;
}