View source
<?php
namespace Drupal\contact_poup\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
class ContactPopupBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $routeMatch;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->routeMatch = $route_match;
}
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('current_route_match'));
}
protected function blockAccess(AccountInterface $account) {
$contact_form = $this
->getContactForm();
if (empty($contact_form)) {
return AccessResult::forbidden();
}
if ($contact_form
->id() === 'personal') {
$user = $this->routeMatch
->getParameter('user');
if (empty($user)) {
return AccessResult::forbidden();
}
if ($user
->id() == $account
->id()) {
return AccessResult::forbidden();
}
return AccessResult::allowedIfHasPermission($account, 'access user contact forms');
}
return $contact_form
->access('view', $account, TRUE);
}
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$config = $this
->getConfiguration();
$form['contact_form'] = array(
'#type' => 'select',
'#title' => $this
->t('Contact form'),
'#description' => $this
->t('Select the contact form to use.'),
'#options' => $this
->listContactForms(),
'#default_value' => isset($config['contact_form']) ? $config['contact_form'] : '',
);
$form['link_title'] = array(
'#type' => 'textfield',
'#title' => $this
->t('The link title'),
'#description' => $this
->t('The title to use for the link. Leave empty for using the contact form label.'),
'#default_value' => isset($config['link_title']) ? $config['link_title'] : '',
);
return $form;
}
public function build() {
$build = [];
$config = $this
->getConfiguration();
if (empty($config['contact_form'])) {
return $build;
}
$storage_contact_form = $this->entityTypeManager
->getStorage('contact_form');
$contact_form = $storage_contact_form
->load($config['contact_form']);
if (empty($contact_form)) {
return $build;
}
$id = $contact_form
->id();
$options = array(
'attributes' => array(
'class' => array(
'use-ajax',
'contact-form',
),
'data-dialog-type' => 'modal',
),
);
if ($id === 'personal') {
if ($user = $this->routeMatch
->getParameter('user')) {
$contact_form_url = Url::fromRoute('entity.user.contact_form', [
'user' => $user
->id(),
], $options);
$build['#cache']['contexts'][] = 'url';
}
else {
return $build;
}
}
else {
$contact_form_url = $contact_form
->toUrl('canonical', $options);
}
$contact_form_title = $contact_form
->label();
$title = empty($config['link_title']) ? $contact_form_title : $config['link_title'];
$link = Link::fromTextAndUrl($title, $contact_form_url);
$build['contact_form'] = [
'#theme' => 'contact_popup_block',
'#link' => $link,
'#contact_form' => $contact_form,
];
$build['contact_form']['#attached']['library'][] = 'core/drupal.dialog.ajax';
$build['#cache']['contexts'][] = 'user.permissions';
return $build;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['contact_form'] = !$form_state
->isValueEmpty('contact_form') ? $form_state
->getValue('contact_form') : '';
$this->configuration['link_title'] = !$form_state
->isValueEmpty('link_title') ? $form_state
->getValue('link_title') : '';
}
private function listContactForms() {
$options = [];
$storage_contact_form = $this->entityTypeManager
->getStorage('contact_form');
$list_contact_form = $storage_contact_form
->getQuery()
->execute();
foreach ($list_contact_form as $contact_form) {
$contact = $storage_contact_form
->load($contact_form);
$options[$contact
->id()] = $contact
->label();
}
return $options;
}
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;
}
}