marketo_suite-1.0.x-dev/src/Entity/MarketoViewBuilder.php
src/Entity/MarketoViewBuilder.php
<?php
namespace Drupal\e3_marketo\Entity;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Theme\Registry;
use Drupal\e3_marketo\Plugin\MarketoHandlerManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* View Builder for Marketo Forms.
*/
class MarketoViewBuilder extends EntityViewBuilder {
/**
* Constructs a new MarketoViewBuilder.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entityType
* The entity type definition.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
* The entity repository service.
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* The language manager.
* @param \Drupal\Core\Theme\Registry $themeRegistry
* The theme registry.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entityDisplayRepository
* The entity display repository.
* @param \Drupal\e3_marketo\Plugin\MarketoHandlerManager $marketoHandlerManager
* Marketo Handler Manager.
* @param \Drupal\Core\Render\RendererInterface $renderer
* Renderer service.
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* Current Route Match.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager service.
*/
public function __construct(
EntityTypeInterface $entityType,
EntityRepositoryInterface $entityRepository,
LanguageManagerInterface $languageManager,
Registry $themeRegistry,
EntityDisplayRepositoryInterface $entityDisplayRepository,
protected MarketoHandlerManager $marketoHandlerManager,
protected RendererInterface $renderer,
protected RouteMatchInterface $routeMatch,
protected EntityTypeManagerInterface $entityTypeManager
) {
parent::__construct($entityType, $entityRepository, $languageManager, $themeRegistry, $entityDisplayRepository);
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) : static {
return new static(
$entity_type,
$container->get('entity.repository'),
$container->get('language_manager'),
$container->get('theme.registry'),
$container->get('entity_display.repository'),
$container->get('plugin.manager.marketo_handler_manager'),
$container->get('renderer'),
$container->get('current_route_match'),
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
parent::buildComponents($build, $entities, $displays, $view_mode);
// Embed Marketo forms using applicable plugins.
/** @var \Drupal\e3_marketo\Entity\MarketoFormEntityInterface[] $entities */
foreach ($entities as $id => $entity) {
// If the form is referenced, add corresponding cacheable dependency.
if (!empty($entity->_referringItem)) {
$reference_paragraph = $entity->_referringItem->getEntity();
if ($reference_paragraph instanceof CacheableDependencyInterface) {
$this->renderer->addCacheableDependency($build[$id], $reference_paragraph);
}
}
// Do not inject the form on entity add/edit/etc. forms.
$current_route = $this->routeMatch->getRouteName();
if (!preg_match('/^entity\.\w+\.\w*form$/', $current_route) && $displays[$entity->bundle()]->getComponent('marketo_form_embed')) {
// Invoke all handlers to inject the form.
$this->marketoHandlerManager->invokeHandlers($entity, 'embedMarketoForm', $build[$id]);
}
}
}
}
