View source
<?php
namespace Drupal\entityform_block\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\EntityOwnerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityEditFormBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $entityTypeBundleInfo;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
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_type.bundle.info'));
}
public function defaultConfiguration() {
return array(
'entity_type' => '',
'bundle' => '',
);
}
protected function blockAccess(AccountInterface $account) {
return $this->entityTypeManager
->getAccessControlHandler($this->configuration['entity_type'])
->createAccess($this->configuration['bundle'], $account, [], TRUE);
}
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$content_entity_types = [];
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $entity_type_definition) {
if ($entity_type_definition
->getGroup() == 'content') {
$content_entity_types[$entity_type_id] = $entity_type_definition;
}
}
$options = array();
foreach ($content_entity_types as $type_key => $entity_type) {
if (!$entity_type
->hasFormClasses() || $type_key == 'comment') {
continue;
}
$entity_type_bundles = $this->entityTypeBundleInfo
->getBundleInfo($type_key);
foreach ($entity_type_bundles as $bundle_key => $bundle_info) {
if ($bundle_key == 'personal' && $type_key == 'contact_message') {
continue;
}
$options[(string) $entity_type
->getLabel()][$type_key . '.' . $bundle_key] = $bundle_info['label'];
}
}
$form['entity_type_bundle'] = array(
'#title' => $this
->t('Entity Type + Bundle'),
'#type' => 'select',
'#options' => $options,
'#required' => TRUE,
'#default_value' => $this->configuration['entity_type'] . '.' . $this->configuration['bundle'],
);
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$selected_entity_type_bundle = $form_state
->getValue('entity_type_bundle');
$values = explode('.', $selected_entity_type_bundle);
$this->configuration['entity_type'] = $values[0];
$this->configuration['bundle'] = $values[1];
}
public function build() {
$values = array();
if ($this->entityTypeManager
->getDefinition($this->configuration['entity_type'])
->hasKey('bundle')) {
$bundle_key = $this->entityTypeManager
->getDefinition($this->configuration['entity_type'])
->getKey('bundle');
$values = array(
$bundle_key => $this->configuration['bundle'],
);
}
$entity = $this->entityTypeManager
->getStorage($this->configuration['entity_type'])
->create($values);
if ($entity instanceof EntityOwnerInterface) {
$entity
->setOwnerId(\Drupal::currentUser()
->id());
}
$form = $this->entityTypeManager
->getFormObject($this->configuration['entity_type'], 'default')
->setEntity($entity);
return \Drupal::formBuilder()
->getForm($form);
}
}