MessageViewBuilder.php 1.75 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains Drupal\contact\MessageViewBuilder.
6 7 8 9 10
 */

namespace Drupal\contact;

use Drupal\Core\Entity\EntityInterface;
11
use Drupal\Core\Entity\EntityViewBuilder;
12
use Drupal\Component\Utility\String;
13
use Drupal\Core\Render\Element;
14 15 16 17

/**
 * Render controller for contact messages.
 */
18
class MessageViewBuilder extends EntityViewBuilder {
19 20

  /**
21
   * {@inheritdoc}
22
   */
23 24
  public function buildComponents(array &$build, array $entities, array $displays, $view_mode, $langcode = NULL) {
    parent::buildComponents($build, $entities, $displays, $view_mode, $langcode);
25

26
    foreach ($entities as $id => $entity) {
27
      // Add the message extra field, if enabled.
28
      $display = $displays[$entity->bundle()];
29
      if ($entity->getMessage() && $display->getComponent('message')) {
30
        $build[$id]['message'] = array(
31 32
          '#type' => 'item',
          '#title' => t('Message'),
33
          '#markup' => String::checkPlain($entity->getMessage()),
34 35 36 37 38 39
        );
      }
    }
  }

  /**
40
   * {@inheritdoc}
41 42 43 44 45 46 47
   */
  public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
    $build = parent::view($entity, $view_mode, $langcode);

    if ($view_mode == 'mail') {
      // Convert field labels into headings.
      // @todo Improve drupal_html_to_text() to convert DIVs correctly.
48
      foreach (Element::children($build) as $key) {
49 50 51 52 53 54 55 56 57 58 59 60 61 62
        if (isset($build[$key]['#label_display']) && $build[$key]['#label_display'] == 'above') {
          $build[$key] += array('#prefix' => '');
          $build[$key]['#prefix'] = $build[$key]['#title'] . ":\n";
          $build[$key]['#label_display'] = 'hidden';
        }
      }
      $build = array(
        '#markup' => drupal_html_to_text(drupal_render($build)),
      );
    }
    return $build;
  }

}