View source
<?php
namespace Drupal\contact_storage;
use Drupal\Core\Config\Config;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityViewBuilderInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContactFormViewBuilder implements EntityViewBuilderInterface, EntityHandlerInterface {
protected $entityFormBuilder;
protected $renderer;
protected $config;
protected $contactMessageStorage;
public function __construct(EntityFormBuilderInterface $entity_form_builder, RendererInterface $renderer, Config $config, EntityStorageInterface $contact_message_storage) {
$this->entityFormBuilder = $entity_form_builder;
$this->renderer = $renderer;
$this->config = $config;
$this->contactMessageStorage = $contact_message_storage;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($container
->get('entity.form_builder'), $container
->get('renderer'), $container
->get('config.factory')
->get('contact.settings'), $container
->get('entity_type.manager')
->getStorage('contact_message'));
}
public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
if ($entity
->status()) {
$message = $this->contactMessageStorage
->create([
'contact_form' => $entity
->id(),
]);
$form = $this->entityFormBuilder
->getForm($message);
$form['#title'] = $entity
->label();
$form['#cache']['contexts'][] = 'user.permissions';
$this->renderer
->addCacheableDependency($form, $this->config);
}
else {
$form['disabled_form_error'] = [
'#theme' => 'contact_storage_disabled_form',
'#contact_form' => $entity,
'#redirect_uri' => $entity
->getThirdPartySetting('contact_storage', 'redirect_uri', ''),
'#disabled_form_message' => $entity
->getThirdPartySetting('contact_storage', 'disabled_form_message', t('This contact form has been disabled.')),
];
}
$this->renderer
->addCacheableDependency($form, $entity);
return $form;
}
public function viewMultiple(array $entities = [], $view_mode = 'full', $langcode = NULL) {
$build = [];
foreach ($entities as $key => $entity) {
$build[$key] = $this
->view($entity, $view_mode, $langcode);
}
return $build;
}
public function resetCache(array $entities = NULL) {
}
public function getCacheTags() {
}
public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
throw new \LogicException();
}
public function viewField(FieldItemListInterface $items, $display_options = []) {
throw new \LogicException();
}
public function viewFieldItem(FieldItemInterface $item, $display_options = []) {
throw new \LogicException();
}
}