TourViewBuilder.php 2.1 KB

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