entity_legal-4.0.x-dev/src/EntityLegalDocumentVersionViewBuilder.php
src/EntityLegalDocumentVersionViewBuilder.php
<?php
declare(strict_types=1);
namespace Drupal\entity_legal;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Theme\Registry;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Legal document version entity builder class.
*/
class EntityLegalDocumentVersionViewBuilder extends EntityViewBuilder {
/**
* Constructs a new view builder instance.
*
* @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\Session\AccountProxyInterface $currentUser
* The current user.
*/
public function __construct(
EntityTypeInterface $entity_type,
EntityRepositoryInterface $entity_repository,
LanguageManagerInterface $language_manager,
Registry $theme_registry,
EntityDisplayRepositoryInterface $entity_display_repository,
protected AccountProxyInterface $currentUser,
) {
parent::__construct($entity_type, $entity_repository, $language_manager, $theme_registry, $entity_display_repository);
}
/**
* {@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('current_user'),
);
}
/**
* {@inheritdoc}
*/
public function buildComponents(array &$build, array $entities, array $displays, $view_mode): void {
parent::buildComponents($build, $entities, $displays, $view_mode);
/** @var \Drupal\entity_legal\EntityLegalDocumentVersionInterface $entity */
foreach ($entities as $id => $entity) {
// Get the acceptance form or information for the current user.
$document = $entity->getDocument();
if ($document->userMustAgree() && $this->currentUser->isAuthenticated()) {
$build[$id]['acceptance'] = $document->getAcceptanceForm();
$build[$id]['acceptance']['#weight'] = 99;
}
}
}
}
