You are here

public function ContactFieldFormatter::viewElements in Contact Formatter 8

Same name and namespace in other branches
  1. 2.x src/Plugin/Field/FieldFormatter/ContactFieldFormatter.php \Drupal\contact_formatter\Plugin\Field\FieldFormatter\ContactFieldFormatter::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

src/Plugin/Field/FieldFormatter/ContactFieldFormatter.php, line 108

Class

ContactFieldFormatter
Plugin implementation of the 'contact_field_formatter' formatter.

Namespace

Drupal\contact_formatter\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($items as $delta => $item) {
    $message = \Drupal::entityTypeManager()
      ->getStorage('contact_message')
      ->create([
      'contact_form' => $item
        ->getValue()['target_id'],
    ]);

    // Can't add Personal form.
    if (!$message
      ->isPersonal()) {
      $form = \Drupal::service('entity.form_builder')
        ->getForm($message);
      $elements[$delta] = [
        '#markup' => $this->renderer
          ->render($form),
      ];
    }
  }
  return $elements;
}