View source
<?php
namespace Drupal\forward\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\forward\ForwardAccessCheckerInterface;
use Drupal\forward\ForwardFormBuilderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ForwardFormBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $accessChecker;
protected $formBuilder;
protected $routeMatch;
protected $entity;
protected $settings;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ForwardAccessCheckerInterface $access_checker, ForwardFormBuilderInterface $form_builder, ConfigFactoryInterface $config_factory, RouteMatchInterface $route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->accessChecker = $access_checker;
$this->formBuilder = $form_builder;
$this->routeMatch = $route_match;
$settings = $config_factory
->get('forward.settings')
->get();
$settings['forward_interface_type'] = 'form';
$this->settings = $settings;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('forward.access_checker'), $container
->get('forward.form_builder'), $container
->get('config.factory'), $container
->get('current_route_match'));
}
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$config = $this->configuration;
$form['body'] = [
'#type' => 'text_format',
'#title' => $this
->t('Body'),
'#description' => $this
->t('If set, this is placed in the block before the form.'),
];
if (isset($config['body']['value'])) {
$form['body']['#default_value'] = $config['body']['value'];
$form['body']['#format'] = $config['body']['format'];
}
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
parent::blockSubmit($form, $form_state);
$this->configuration['body'] = $form_state
->getValue('body');
}
public function blockAccess(AccountInterface $account) {
return $this
->isAllowed() ? AccessResult::allowed() : AccessResult::forbidden();
}
public function getCacheMaxAge() {
return 0;
}
public function isAllowed() {
$allowed = FALSE;
$entity = NULL;
$parameters = $this->routeMatch
->getParameters();
if ($parameters
->has('node')) {
$entity = $parameters
->get('node');
$bundle = $entity
->bundle();
}
if ($parameters
->has('commerce_product')) {
$entity = $parameters
->get('commerce_product');
$bundle = $entity
->bundle();
}
if ($parameters
->has('taxonomy_term')) {
$entity = $parameters
->get('taxonomy_term');
$bundle = $entity
->bundle();
}
if ($parameters
->has('user')) {
$entity = $parameters
->get('user');
$bundle = '';
}
if ($entity) {
$view_mode = 'full';
$this->entity = $entity;
$allowed = $this->accessChecker
->isAllowed($this->settings, $entity, $view_mode, $entity
->getEntityTypeId(), $bundle);
}
return $allowed;
}
public function build() {
$render_array = [];
$config = $this
->getConfiguration();
if (empty($config['build']['#forward_build']) && $this
->isAllowed()) {
$render_array = [];
if (!empty($config['body']['value'])) {
$render_array[] = [
'#type' => 'processed_text',
'#text' => $config['body']['value'],
'#format' => $config['body']['format'],
];
}
$render_array[] = $this->formBuilder
->buildForwardEntityForm($this->entity, $this->settings);
}
return $render_array;
}
}