You are here

class NameListFormattableMarkup in Name Field 8

Formats a string for HTML display by replacing variable placeholders.

Adds special handling of @names, @last.

Hierarchy

Expanded class hierarchy of NameListFormattableMarkup

1 file declares its use of NameListFormattableMarkup
NameFormatter.php in src/NameFormatter.php

File

src/Render/NameListFormattableMarkup.php, line 13

Namespace

Drupal\name\Render
View source
class NameListFormattableMarkup implements MarkupInterface {

  /**
   * The names.
   *
   * @var array
   */
  protected $names = [];

  /**
   * The name separator.
   *
   * @var string
   */
  protected $separator = ', ';

  /**
   * Constructor for NameListFormattableMarkup.
   */
  public function __construct(array $names = [], $separator = ', ') {
    $this->names = $names;
    $this->separator = $this
      ->escapeValues($separator);
  }

  /**
   * {@inheritdoc}
   */
  public function __toString() {
    return $this
      ->escapeValues($this->names);
  }

  /**
   * {@inheritdoc}
   */
  public function jsonSerialize() {
    return $this
      ->__toString();
  }

  /**
   * Escapes values if needed.
   *
   * @param array|string|\Drupal\Component\Render\MarkupInterface $value
   *   A placeholder replacement value. Will recursively escape array values
   *   using the specified separator.
   *
   * @return string
   *   The properly escaped replacement value.
   */
  protected function escapeValues($value) {
    if (is_array($value)) {
      $escaped = [];
      foreach ($value as $child_value) {
        $escaped[] = $this
          ->escapeValues($child_value);
      }
      return implode($this->separator, $escaped);
    }
    return $value instanceof MarkupInterface ? (string) $value : Html::escape($value);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NameListFormattableMarkup::$names protected property The names.
NameListFormattableMarkup::$separator protected property The name separator.
NameListFormattableMarkup::escapeValues protected function Escapes values if needed.
NameListFormattableMarkup::jsonSerialize public function
NameListFormattableMarkup::__construct public function Constructor for NameListFormattableMarkup.
NameListFormattableMarkup::__toString public function Returns markup. Overrides MarkupInterface::__toString