openquestions-1.0.x-dev/src/Display/OqItemViewBuilder.php

src/Display/OqItemViewBuilder.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 OqItemViewBuilder 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) {
    /** @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');
      $build[$id]['attribution'] = [
        '#type' => 'markup',
        '#markup' => $this->t('Posted by <a href=":user_link">@user_name</a> on @date', [
          ':user_link' => $account->toUrl()->toString(),
          '@user_name' => $account->get('name')->value,
          '@date' => $this->dateFormatter->format($entity->get('created')->value),
        ]),
        '#prefix' => '<div id="field-attribution-display">',
        '#suffix' => '</div>',
      ];

      // 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;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc