View source
<?php
namespace Drupal\opigno_ilt;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Link;
class ILTViewBuilder extends EntityViewBuilder {
protected function buildNavigation(ILTInterface $entity) {
$gid = $entity
->getTrainingId();
if (empty($gid)) {
return [];
}
$actions = [];
$actions['form-actions'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'form-actions',
],
'id' => 'edit-actions',
],
'#title' => 'test',
];
$title = $this
->t('Back to training homepage');
$route = 'entity.group.canonical';
$route_params = [
'group' => $gid,
];
$options = [
'attributes' => [
'class' => [
'btn',
'btn-rounded',
],
'id' => 'edit-submit',
],
];
$actions['form-actions'][] = Link::createFromRoute($title, $route, $route_params, $options)
->toRenderable();
return $actions;
}
protected function alterBuild(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
$build[] = [
'#type' => 'html_tag',
'#tag' => 'h3',
'#value' => $entity
->getTitle(),
];
$start_date = $entity
->getStartDate();
$end_date = $entity
->getEndDate();
if (isset($start_date) && isset($end_date)) {
$start_date = DrupalDateTime::createFromFormat(DrupalDateTime::FORMAT, $start_date);
$end_date = DrupalDateTime::createFromFormat(DrupalDateTime::FORMAT, $end_date);
$end_date_format = $end_date
->format('g:i A');
if ($start_date
->format('jS F Y') != $end_date
->format('jS F Y')) {
$end_date_format = $end_date
->format('jS F Y - g:i A');
}
$build[] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this
->t('Date: @start to @end', [
'@start' => $start_date
->format('jS F Y - g:i A'),
'@end' => $end_date_format,
]),
];
}
$build[] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this
->t('Place: @place', [
'@place' => $entity
->getPlace(),
]),
];
$trainer_id = $entity
->getTrainerId();
if ($trainer_id) {
$trainer = \Drupal::entityTypeManager()
->getStorage('user')
->load($trainer_id);
if ($trainer) {
$build[] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this
->t('Trainer: @trainer', [
'@trainer' => $trainer
->getAccountName(),
]),
];
}
}
$build[] = $this
->buildNavigation($entity);
}
}