Mercurial > hg > isophonics-drupal-site
comparison core/modules/contact/src/MessageViewBuilder.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\contact; | |
4 | |
5 use Drupal\Core\Entity\EntityInterface; | |
6 use Drupal\Core\Entity\EntityViewBuilder; | |
7 use Drupal\Core\Mail\MailFormatHelper; | |
8 use Drupal\Core\Render\Element; | |
9 | |
10 /** | |
11 * Render controller for contact messages. | |
12 */ | |
13 class MessageViewBuilder extends EntityViewBuilder { | |
14 | |
15 /** | |
16 * {@inheritdoc} | |
17 */ | |
18 protected function getBuildDefaults(EntityInterface $entity, $view_mode) { | |
19 $build = parent::getBuildDefaults($entity, $view_mode); | |
20 // The message fields are individually rendered into email templates, so | |
21 // the entity has no template itself. | |
22 unset($build['#theme']); | |
23 return $build; | |
24 } | |
25 | |
26 /** | |
27 * {@inheritdoc} | |
28 */ | |
29 public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) { | |
30 $build = parent::view($entity, $view_mode, $langcode); | |
31 | |
32 if ($view_mode == 'mail') { | |
33 // Convert field labels into headings. | |
34 // @todo Improve \Drupal\Core\Mail\MailFormatHelper::htmlToText() to | |
35 // convert DIVs correctly. | |
36 foreach (Element::children($build) as $key) { | |
37 if (isset($build[$key]['#label_display']) && $build[$key]['#label_display'] == 'above') { | |
38 $build[$key] += ['#prefix' => '']; | |
39 $build[$key]['#prefix'] = $build[$key]['#title'] . ":\n"; | |
40 $build[$key]['#label_display'] = 'hidden'; | |
41 } | |
42 } | |
43 $build['#post_render'][] = function ($html, array $elements) { | |
44 return MailFormatHelper::htmlToText($html); | |
45 }; | |
46 } | |
47 return $build; | |
48 } | |
49 | |
50 } |