ForwardFormBlock.php in Forward 8.3
File
src/Plugin/Block/ForwardFormBlock.php
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\Entity\EntityInterface;
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;
use Symfony\Component\HttpFoundation\ParameterBag;
class ForwardFormBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $accessChecker;
protected $formBuilder;
protected $routeMatch;
protected $entity;
protected $settings;
public function __construct($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 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('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 = $this->formBuilder
->buildForwardEntityForm($this->entity, $this->settings);
}
return $render_array;
}
}