openquestions-1.0.x-dev/src/Display/OqVoteViewBuilder.php
src/Display/OqVoteViewBuilder.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 Symfony\Component\DependencyInjection\ContainerInterface;
/**
* View builder handler for oq items.
*/
class OqVoteViewBuilder extends EntityViewBuilder implements TrustedCallbackInterface {
/**
* 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) {
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');
$item = $this->oqUtils->getFirstReference($entity, 'item');
$question = $this->oqUtils->getFirstReference($entity, 'question');
$vote_value = $entity->get('vote_value')->value;
$build[$id]['summary'] = [
'#type' => 'item',
'#markup' => $this->t('For the question <span class="oq-question-quote">@question</span> on the item <a class="oq-item-quote" href=":item_url">"@item"</a>, the user <a href=":user_link">@user_name</a> voted <span class="oq-vote-value">@vote_value</span> on <span class="oq-vote-date">@date</span>.', [
'@question' => $question->label(),
':item_url' => $item->toUrl()->toString(),
'@item' => $item->label(),
':user_link' => $account->toUrl()->toString(),
'@user_name' => $account->get('name')->value,
'@vote_value' => $question->getOptionName($vote_value),
'@date' => $this->dateFormatter->format($entity->get('created')->value),
]),
'#prefix' => '<div id="field-attribution-display">',
'#suffix' => '</div>',
'#attached' => ['library' => 'openquestions/openquestions_voting'],
];
$rangeInfo = $question->getOptionsRangeInfo();
$build[$id]['question_info'] = [
'#type' => 'item',
];
if ($rangeInfo['type'] == 'binary') {
$build[$id]['question_info']['#markup'] = $this->t('The choices on that question are @a or @b.', ['@a' => $rangeInfo['first'], '@b' => $rangeInfo['last']]);
}
else {
$build[$id]['question_info']['#markup'] = $this->t('The choices on that question range between @a and @b.', ['@a' => $rangeInfo['first'], '@b' => $rangeInfo['last']]);
}
$build[$id]['explanation_info'] = [
'#type' => 'item',
'#markup' => t('This is the explanation given for the vote:'),
];
$build[$id]['comments_info'] = [
'#type' => 'item',
'#markup' => t('If you have any concerns or comments about this vote, please let us know below:'),
];
// 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>',
];
}
}
$build[$id]['#attributes'] = [
'class' => ['oq-vote-page'],
];
}
/**
* {@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;
}
}
