You are here

public function MessageViewBuilder::view in Message 8

Builds the render array for the provided entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to render.

string $view_mode: (optional) The view mode that should be used to render the entity.

string $langcode: (optional) For which language the entity should be rendered, defaults to the current content language.

Return value

array A render array for the entity.

Throws

\InvalidArgumentException Can be thrown when the set of parameters is inconsistent, like when trying to view a Comment and passing a Node which is not the one the comment belongs to, or not passing one, and having the comment node not be available for loading.

Overrides EntityViewBuilder::view

File

src/MessageViewBuilder.php, line 18

Class

MessageViewBuilder
Render controller for Messages.

Namespace

Drupal\message

Code

public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
  $build = parent::view($entity, $view_mode, $langcode);

  /* @var \Drupal\message\Entity\Message $entity  */
  $partials = $entity
    ->getText($langcode);

  // Get the partials the user selected for the current view mode.
  $extra_fields = EntityViewDisplay::load('message.' . $entity
    ->bundle() . '.' . $view_mode);
  if (!$extra_fields instanceof EntityViewDisplayInterface) {
    $extra_fields = EntityViewDisplay::create([
      'targetEntityType' => 'message',
      'bundle' => $entity
        ->bundle(),
      'mode' => $view_mode,
      'status' => TRUE,
    ]);
  }
  foreach ($extra_fields
    ->getComponents() as $field_name => $settings) {

    // The partials are keyed with `partial_X`, check if that is set.
    if (strpos($field_name, 'partial_') === 0) {
      list(, $delta) = explode('_', $field_name);
      if (isset($partials[$delta])) {
        $build[$field_name]['#markup'] = $partials[$delta];
      }
    }
    else {

      // This is another field.
      $display = $this
        ->getSingleFieldDisplay($entity, $field_name, $settings);
      $build += $display
        ->build($entity);
    }
  }
  return $build;
}