View source
<?php
namespace Drupal\social_post\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxy;
use Drupal\user\EntityOwnerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PostBlock extends BlockBase implements ContainerFactoryPluginInterface {
public $entityType;
public $bundle;
public $formDisplay;
protected $entityTypeManager;
protected $currentUser;
protected $formBuilder;
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, AccountProxy $currentUser, FormBuilderInterface $formBuilder, ModuleHandler $moduleHandler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityType = 'post';
$this->bundle = 'post';
$this->formDisplay = 'default';
$this->entityTypeManager = $entityTypeManager;
$this->currentUser = $currentUser;
$this->formBuilder = $formBuilder;
$this->moduleHandler = $moduleHandler;
if ($moduleHandler
->moduleExists('social_post_photo')) {
$this->bundle = 'photo';
}
}
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_user'), $container
->get('form_builder'), $container
->get('module_handler'));
}
protected function blockAccess(AccountInterface $account) {
return $this->entityTypeManager
->getAccessControlHandler($this->entityType)
->createAccess($this->bundle, $account, [], TRUE);
}
public function build() {
$values = [];
if ($this->entityTypeManager
->getDefinition($this->entityType)
->hasKey('bundle')) {
$bundle_key = $this->entityTypeManager
->getDefinition($this->entityType)
->getKey('bundle');
$values = [
$bundle_key => $this->bundle,
];
}
$entity = $this->entityTypeManager
->getStorage($this->entityType)
->create($values);
if ($entity instanceof EntityOwnerInterface) {
$entity
->setOwnerId($this->currentUser
->id());
}
$display = $this->entityTypeManager
->getStorage('entity_form_display')
->load($this->entityType . '.' . $this->bundle . '.' . $this->formDisplay);
$form_object = $this->entityTypeManager
->getFormObject($entity
->getEntityTypeId(), 'default');
$form_object
->setEntity($entity);
$form_state = (new FormState())
->setFormState([]);
$form_state
->set('form_display', $display);
return $this->formBuilder
->buildForm($form_object, $form_state);
}
}