2015-08-17 17:00:26 -07:00
< ? php
namespace Drupal\tour ;
use Drupal\Core\Entity\EntityViewBuilder ;
use Drupal\Component\Utility\Html ;
/**
* Provides a Tour view builder .
*/
class TourViewBuilder extends EntityViewBuilder {
/**
* { @ inheritdoc }
*/
2017-04-13 15:53:35 +01:00
public function viewMultiple ( array $entities = [], $view_mode = 'full' , $langcode = NULL ) {
2015-08-17 17:00:26 -07:00
/** @var \Drupal\tour\TourInterface[] $entities */
2017-04-13 15:53:35 +01:00
$build = [];
2015-08-17 17:00:26 -07:00
foreach ( $entities as $entity_id => $entity ) {
$tips = $entity -> getTips ();
$count = count ( $tips );
2017-04-13 15:53:35 +01:00
$list_items = [];
2015-08-17 17:00:26 -07:00
foreach ( $tips as $index => $tip ) {
if ( $output = $tip -> getOutput ()) {
2017-04-13 15:53:35 +01:00
$attributes = [
'class' => [
2015-08-17 17:00:26 -07:00
'tip-module-' . Html :: cleanCssIdentifier ( $entity -> getModule ()),
'tip-type-' . Html :: cleanCssIdentifier ( $tip -> getPluginId ()),
'tip-' . Html :: cleanCssIdentifier ( $tip -> id ()),
2017-04-13 15:53:35 +01:00
],
];
$list_items [] = [
2015-08-17 17:00:26 -07:00
'output' => $output ,
2017-04-13 15:53:35 +01:00
'counter' => [
2015-08-17 17:00:26 -07:00
'#type' => 'container' ,
2017-04-13 15:53:35 +01:00
'#attributes' => [
'class' => [
2015-08-17 17:00:26 -07:00
'tour-progress' ,
2017-04-13 15:53:35 +01:00
],
],
'#children' => t ( '@tour_item of @total' , [ '@tour_item' => $index + 1 , '@total' => $count ]),
],
2015-08-17 17:00:26 -07:00
'#wrapper_attributes' => $tip -> getAttributes () + $attributes ,
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
}
}
// 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' );
2017-04-13 15:53:35 +01:00
$build [ $entity_id ] = [
2015-08-17 17:00:26 -07:00
'#theme' => 'item_list' ,
'#items' => $list_items ,
'#list_type' => 'ol' ,
2017-04-13 15:53:35 +01:00
'#attributes' => [
2015-08-17 17:00:26 -07:00
'id' => 'tour' ,
2017-04-13 15:53:35 +01:00
'class' => [
2015-08-17 17:00:26 -07:00
'hidden' ,
2017-04-13 15:53:35 +01:00
],
],
2015-08-17 17:00:26 -07:00
'#cache' => [
'tags' => $entity -> getCacheTags (),
],
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
}
}
// If at least one tour was built, attach the tour library.
if ( $build ) {
$build [ '#attached' ][ 'library' ][] = 'tour/tour' ;
}
return $build ;
}
}