You are here

public function SmsLinkFormatter::viewElements in SMS Framework 8

Same name and namespace in other branches
  1. 2.x modules/sms_sendtophone/src/Plugin/Field/FieldFormatter/SmsLinkFormatter.php \Drupal\sms_sendtophone\Plugin\Field\FieldFormatter\SmsLinkFormatter::viewElements()
  2. 2.1.x modules/sms_sendtophone/src/Plugin/Field/FieldFormatter/SmsLinkFormatter.php \Drupal\sms_sendtophone\Plugin\Field\FieldFormatter\SmsLinkFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

modules/sms_sendtophone/src/Plugin/Field/FieldFormatter/SmsLinkFormatter.php, line 23

Class

SmsLinkFormatter
Defines a SMS field formatter.

Namespace

Drupal\sms_sendtophone\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = [];
  foreach ($items as $delta => $item) {
    $text = strip_tags($item->value);
    $element[$delta] = [
      '#type' => 'markup',
      'text' => [
        '#type' => 'markup',
        '#markup' => $text,
        '#prefix' => '<span class="sms-sendtophone-inline">',
        '#suffix' => '</span>',
      ],
      'link' => [
        '#type' => 'link',
        '#prefix' => ' (',
        '#suffix' => ')',
        '#title' => $this
          ->t('Send to phone'),
        '#url' => Url::fromRoute('sms_sendtophone.page', [
          'type' => 'field',
        ], [
          'query' => [
            'text' => $text,
            'destination' => \Drupal::destination()
              ->get(),
          ],
        ]),
        '#attributes' => [
          'title' => $this
            ->t('Send this text via SMS.'),
          'class' => 'sms-sendtophone',
        ],
      ],
    ];
  }
  return $element;
}