openquestions-1.0.x-dev/src/Display/OqApplicationViewBuilder.php
src/Display/OqApplicationViewBuilder.php
<?php
namespace Drupal\openquestions\Display;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\openquestions\Service\OQUtilsInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Theme\Registry;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* View builder handler for oq applications.
*/
class OqApplicationViewBuilder extends EntityViewBuilder implements TrustedCallbackInterface {
protected const DESCRIPTION_MAX_LEN = 1000;
/**
* The date formatter.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Our utils.
*
* @var \Drupal\openquestions\Service\OQUtilsInterface
*/
protected $oqUtils;
/**
* Constructs a new EntityViewBuilder.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Theme\Registry $theme_registry
* The theme registry.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository.
* @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
* Format dates.
* @param \Drupal\openquestions\Service\OQUtilsInterface $oqUtils
* Our utils.
*/
public function __construct(EntityTypeInterface $entity_type,
EntityRepositoryInterface $entity_repository,
LanguageManagerInterface $language_manager,
Registry $theme_registry,
EntityDisplayRepositoryInterface $entity_display_repository,
DateFormatterInterface $dateFormatter,
OQUtilsInterface $oqUtils) {
parent::__construct($entity_type, $entity_repository, $language_manager, $theme_registry, $entity_display_repository);
$this->dateFormatter = $dateFormatter;
$this->oqUtils = $oqUtils;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('entity.repository'),
$container->get('language_manager'),
$container->get('theme.registry'),
$container->get('entity_display.repository'),
$container->get('date.formatter'),
$container->get('openquestions.oq_utils')
);
}
/**
* {@inheritdoc}
*/
public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
/** @var \Drupal\node\NodeInterface[] $entities */
if (empty($entities)) {
return;
}
parent::buildComponents($build, $entities, $displays, $view_mode);
foreach ($entities as $id => $entity) {
$bundle = $entity->bundle();
$display = $displays[$bundle];
$account = $this->oqUtils->getFirstReference($entity, 'uid');
$term = $this->oqUtils->getFirstReference($entity, 'disposition');
$build[$id]['attribution'] = [
'#type' => 'markup',
'#markup' => $this->t('This application was created by <a class="oq-user-link" href=":user_link">@user_name</a> on @date and is in the disposition <span class="oq-disposition-name">@disposition</span>.', [
':user_link' => $account->toUrl()->toString(),
'@user_name' => $account->get('name')->value,
'@date' => $this->dateFormatter->format($entity->get('created')->value),
'@disposition' => $term->getName(),
]),
'#prefix' => '<div id="field-attribution-display">',
'#suffix' => '</div>',
];
$build[$id]['application_text_intro'] = [
'#type' => 'markup',
'#markup' => $this->t('This is the text of the application:'),
'#prefix' => '<div id="field-application-text-intro-display">',
'#suffix' => '</div>',
];
$text = $entity->get('description')->value;
$text = strip_tags($text);
$text = Unicode::truncate($text, self::DESCRIPTION_MAX_LEN, TRUE, TRUE, 100);
$text = Html::escape($text);
$text = str_rot13($text);
$build[$id]['application_text'] = [
'#type' => 'markup',
'#markup' => $text,
'#prefix' => '<div id="field-application-text-display" class="application-description"><span>',
'#suffix' => '</span></div>',
'#attached' => ['library' => 'openquestions/openquestions_voting'],
];
// Add Language field text element to render array.
if ($display->getComponent('langcode')) {
$build[$id]['langcode'] = [
'#type' => 'item',
'#title' => t('Language'),
'#markup' => $entity->language()->getName(),
'#prefix' => '<div id="field-language-display">',
'#suffix' => '</div>',
];
}
}
}
/**
* {@inheritdoc}
*/
protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
$defaults = parent::getBuildDefaults($entity, $view_mode);
// Don't cache entities that are in 'preview' mode.
if (isset($defaults['#cache']) && isset($entity->in_preview)) {
unset($defaults['#cache']);
}
return $defaults;
}
}
