TourRenderController.php 2.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
<?php

/**
 * @file
 * Contains \Drupal\tour\TourRenderController.
 */

namespace Drupal\tour;

use Drupal\Core\Entity\EntityRenderController;
use Drupal\Core\Entity\EntityInterface;

/**
 * Provides a Tour render controller.
 */
class TourRenderController extends EntityRenderController {

  /**
   * Overrides \Drupal\Core\Entity\EntityRenderController::viewMultiple().
   */
  public function viewMultiple(array $entities = array(), $view_mode = 'full', $langcode = NULL) {
    $build = array();
    foreach ($entities as $entity_id => $entity) {
      $tips = $entity->getTips();
      $count = count($tips);
      $list_items = array();
      foreach ($tips as $index => $tip) {
        if ($output = $tip->getOutput()) {
29 30 31 32 33 34 35
          $attributes = array(
            'class' => array(
              'tip-module-' . drupal_clean_css_identifier($entity->get('module')),
              'tip-type-' . drupal_clean_css_identifier($tip->get('plugin')),
              'tip-' . drupal_clean_css_identifier($tip->get('id')),
            ),
          );
36 37 38 39 40 41 42 43 44 45 46
          $list_items[] = array(
            'output' => $output,
            'counter' => array(
              '#type' => 'container',
              '#attributes' => array(
                'class' => array(
                  'tour-progress',
                ),
              ),
              '#children' => t('!tour_item of !total', array('!tour_item' => $index + 1, '!total' => $count)),
            ),
47
            '#wrapper_attributes' => $tip->getAttributes() + $attributes,
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
          );
        }
      }
      // If there is at least one tour item, build the tour.
      if ($list_items) {
        end($list_items);
        $key = key($list_items);
        $list_items[$key]['#wrapper_attributes']['data-text'] = t('End tour');
        $build[$entity_id] = array(
          '#theme' => 'item_list',
          '#items' => $list_items,
          '#type' => 'ol',
          '#attributes' => array(
            'id' => 'tour',
            'class' => array(
63
              'hidden',
64 65 66 67 68 69 70 71 72 73 74 75 76
            ),
          ),
        );
      }
    }
    // If at least one tour was built, attach the tour library.
    if ($build) {
      $build['#attached']['library'][] = array('tour', 'tour');
    }
    return $build;
  }

}