View source
<?php
namespace Drupal\contact_block\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\contact\Access\ContactPageAccess;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContactBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $configFactory;
protected $entityFormBuilder;
protected $renderer;
protected $contactForm;
protected $routeMatch;
protected $checkContactPageAccess;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, EntityFormBuilderInterface $entity_form_builder, RendererInterface $renderer, CurrentRouteMatch $route_match, ContactPageAccess $check_contact_page_access) {
$this->entityTypeManager = $entity_type_manager;
$this->configFactory = $config_factory;
$this->entityFormBuilder = $entity_form_builder;
$this->renderer = $renderer;
$this->routeMatch = $route_match;
$this->checkContactPageAccess = $check_contact_page_access;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('config.factory'), $container
->get('entity.form_builder'), $container
->get('renderer'), $container
->get('current_route_match'), $container
->get('access_check.contact_personal'));
}
protected function blockAccess(AccountInterface $account) {
$contact_form = $this
->getContactForm();
$contact_message = $this
->createContactMessage();
if (empty($contact_form)) {
return AccessResult::forbidden();
}
if ($contact_message
->isPersonal()) {
$user = $this->routeMatch
->getParameter('user');
if (empty($user)) {
return AccessResult::forbidden();
}
return $this->checkContactPageAccess
->access($user, $account);
}
return $contact_form
->access('view', $account, TRUE);
}
public function defaultConfiguration() {
$default_form = $this->configFactory
->get('contact.settings')
->get('default_form');
return [
'label' => $this
->t('Contact block'),
'contact_form' => $default_form,
];
}
public function blockForm($form, FormStateInterface $form_state) {
$options = $this->entityTypeManager
->getStorage('contact_form')
->loadMultiple();
foreach ($options as $key => $option) {
$options[$key] = $option
->label();
}
$form['contact_form'] = [
'#type' => 'select',
'#title' => $this
->t('Contact form'),
'#options' => $options,
'#default_value' => $this->configuration['contact_form'],
'#required' => TRUE,
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['contact_form'] = $form_state
->getValue('contact_form');
}
public function build() {
$form = [];
$contact_form = $this
->getContactForm();
if ($contact_form) {
$contact_message = $this
->createContactMessage();
if ($contact_message
->isPersonal()) {
$user = $this->routeMatch
->getParameter('user');
$contact_message
->set('recipient', $user);
}
$form = $this->entityFormBuilder
->getForm($contact_message);
$form['#cache']['contexts'][] = 'user.permissions';
$this->renderer
->addCacheableDependency($form, $contact_form);
$form['#contextual_links']['contact_block'] = [
'route_parameters' => [
'contact_form' => $contact_form
->id(),
],
];
}
return $form;
}
public function calculateDependencies() {
$dependencies = array_merge_recursive(parent::calculateDependencies(), [
'config' => [],
]);
if ($contact_form = $this
->getContactForm()) {
$dependencies['config'][] = $contact_form
->getConfigDependencyName();
}
return $dependencies;
}
protected function getContactForm() {
if (!isset($this->contactForm)) {
if (isset($this->configuration['contact_form'])) {
$this->contactForm = $this->entityTypeManager
->getStorage('contact_form')
->load($this->configuration['contact_form']);
}
}
return $this->contactForm;
}
protected function createContactMessage() {
$contact_message = NULL;
$contact_form = $this
->getContactForm();
if ($contact_form) {
$contact_message = $this->entityTypeManager
->getStorage('contact_message')
->create([
'contact_form' => $contact_form
->id(),
]);
}
return $contact_message;
}
}