View source
<?php
namespace Drupal\formblock\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\node\Entity\NodeType;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFormBuilderInterface;
class NodeFormBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $entityFormBuilder;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, EntityFormBuilderInterface $entityFormBuilder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this
->setConfiguration($configuration);
$this->entityTypeManager = $entityTypeManager;
$this->entityFormBuilder = $entityFormBuilder;
}
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('entity.form_builder'));
}
public function defaultConfiguration() {
return [
'type' => NULL,
'show_help' => FALSE,
];
}
public function blockForm($form, FormStateInterface $form_state) {
$form['formblock_node_type'] = [
'#title' => $this
->t('Node type'),
'#description' => $this
->t('Select the node type whose form will be shown in the block.'),
'#type' => 'select',
'#required' => TRUE,
'#options' => $this
->getNodeTypes(),
'#default_value' => $this->configuration['type'],
];
$form['formblock_show_help'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show submission guidelines'),
'#default_value' => $this->configuration['show_help'],
'#description' => $this
->t('Enable this option to show the submission guidelines in the block above the form.'),
];
return $form;
}
protected function getNodeTypes() {
$options = [];
$types = $this->entityTypeManager
->getStorage('node_type')
->loadMultiple();
foreach ($types as $type) {
$options[$type
->id()] = $type
->label();
}
return $options;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['type'] = $form_state
->getValue('formblock_node_type');
$this->configuration['show_help'] = $form_state
->getValue('formblock_show_help');
}
public function build() {
$build = [];
$node_type = NodeType::load($this->configuration['type']);
if ($this->configuration['show_help']) {
$build['help'] = [
'#markup' => !empty($node_type
->getHelp()) ? '<p>' . Xss::filterAdmin($node_type
->getHelp()) . '</p>' : '',
];
}
$node = $this->entityTypeManager
->getStorage('node')
->create([
'type' => $node_type
->id(),
]);
$build['form'] = $this->entityFormBuilder
->getForm($node);
return $build;
}
public function blockAccess(AccountInterface $account) {
$access_control_handler = $this->entityTypeManager
->getAccessControlHandler('node');
$result = $access_control_handler
->createAccess($this->configuration['type'], $account, [], TRUE);
$node_type = $node_type = NodeType::load($this->configuration['type']);
$result
->addCacheTags($node_type
->getCacheTags());
return $result;
}
}