Mercurial > hg > isophonics-drupal-site
comparison core/modules/tour/src/TourViewBuilder.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\tour; | |
4 | |
5 use Drupal\Core\Entity\EntityViewBuilder; | |
6 use Drupal\Component\Utility\Html; | |
7 | |
8 /** | |
9 * Provides a Tour view builder. | |
10 */ | |
11 class TourViewBuilder extends EntityViewBuilder { | |
12 | |
13 /** | |
14 * {@inheritdoc} | |
15 */ | |
16 public function viewMultiple(array $entities = [], $view_mode = 'full', $langcode = NULL) { | |
17 /** @var \Drupal\tour\TourInterface[] $entities */ | |
18 $build = []; | |
19 foreach ($entities as $entity_id => $entity) { | |
20 $tips = $entity->getTips(); | |
21 $count = count($tips); | |
22 $list_items = []; | |
23 foreach ($tips as $index => $tip) { | |
24 if ($output = $tip->getOutput()) { | |
25 $attributes = [ | |
26 'class' => [ | |
27 'tip-module-' . Html::cleanCssIdentifier($entity->getModule()), | |
28 'tip-type-' . Html::cleanCssIdentifier($tip->getPluginId()), | |
29 'tip-' . Html::cleanCssIdentifier($tip->id()), | |
30 ], | |
31 ]; | |
32 $list_items[] = [ | |
33 'output' => $output, | |
34 'counter' => [ | |
35 '#type' => 'container', | |
36 '#attributes' => [ | |
37 'class' => [ | |
38 'tour-progress', | |
39 ], | |
40 ], | |
41 '#children' => t('@tour_item of @total', ['@tour_item' => $index + 1, '@total' => $count]), | |
42 ], | |
43 '#wrapper_attributes' => $tip->getAttributes() + $attributes, | |
44 ]; | |
45 } | |
46 } | |
47 // If there is at least one tour item, build the tour. | |
48 if ($list_items) { | |
49 end($list_items); | |
50 $key = key($list_items); | |
51 $list_items[$key]['#wrapper_attributes']['data-text'] = t('End tour'); | |
52 $build[$entity_id] = [ | |
53 '#theme' => 'item_list', | |
54 '#items' => $list_items, | |
55 '#list_type' => 'ol', | |
56 '#attributes' => [ | |
57 'id' => 'tour', | |
58 'class' => [ | |
59 'hidden', | |
60 ], | |
61 ], | |
62 '#cache' => [ | |
63 'tags' => $entity->getCacheTags(), | |
64 ], | |
65 ]; | |
66 } | |
67 } | |
68 // If at least one tour was built, attach the tour library. | |
69 if ($build) { | |
70 $build['#attached']['library'][] = 'tour/tour'; | |
71 } | |
72 return $build; | |
73 } | |
74 | |
75 } |