View source
<?php
namespace Drupal\layout_builder\Plugin\Block;
use Drupal\block_content\Access\RefinableDependentAccessInterface;
use Drupal\block_content\Access\RefinableDependentAccessTrait;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class InlineBlock extends BlockBase implements ContainerFactoryPluginInterface, RefinableDependentAccessInterface {
use RefinableDependentAccessTrait;
protected $entityTypeManager;
protected $blockContent;
protected $entityDisplayRepository;
protected $isNew = TRUE;
protected $currentUser;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository, AccountInterface $current_user = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->entityDisplayRepository = $entity_display_repository;
if (!empty($this->configuration['block_revision_id']) || !empty($this->configuration['block_serialized'])) {
$this->isNew = FALSE;
}
if (!$current_user) {
@trigger_error('The current_user service must be passed to InlineBlock::__construct(), it is required before Drupal 9.0.0.', E_USER_DEPRECATED);
$current_user = \Drupal::currentUser();
}
$this->currentUser = $current_user;
}
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_display.repository'), $container
->get('current_user'));
}
public function defaultConfiguration() {
return [
'view_mode' => 'full',
'block_revision_id' => NULL,
'block_serialized' => NULL,
];
}
public function blockForm($form, FormStateInterface $form_state) {
$block = $this
->getEntity();
$form['block_form'] = [
'#type' => 'container',
'#process' => [
[
static::class,
'processBlockForm',
],
],
'#block' => $block,
'#access' => $this->currentUser
->hasPermission('create and edit custom blocks'),
];
$options = $this->entityDisplayRepository
->getViewModeOptionsByBundle('block_content', $block
->bundle());
$form['view_mode'] = [
'#type' => 'select',
'#options' => $options,
'#title' => $this
->t('View mode'),
'#description' => $this
->t('The view mode in which to render the block.'),
'#default_value' => $this->configuration['view_mode'],
'#access' => count($options) > 1,
];
return $form;
}
public static function processBlockForm(array $element, FormStateInterface $form_state) {
$block = $element['#block'];
EntityFormDisplay::collectRenderDisplay($block, 'edit')
->buildForm($block, $element, $form_state);
$element['revision_log']['#access'] = FALSE;
$element['info']['#access'] = FALSE;
return $element;
}
public function blockValidate($form, FormStateInterface $form_state) {
$block_form = $form['block_form'];
$block = $block_form['#block'];
$form_display = EntityFormDisplay::collectRenderDisplay($block, 'edit');
$complete_form_state = $form_state instanceof SubformStateInterface ? $form_state
->getCompleteFormState() : $form_state;
$form_display
->extractFormValues($block, $block_form, $complete_form_state);
$form_display
->validateFormValues($block, $block_form, $complete_form_state);
$form_state
->setTemporaryValue('block_form_parents', $block_form['#parents']);
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['view_mode'] = $form_state
->getValue('view_mode');
$block_form = NestedArray::getValue($form, $form_state
->getTemporaryValue('block_form_parents'));
$block = $block_form['#block'];
$form_display = EntityFormDisplay::collectRenderDisplay($block, 'edit');
$complete_form_state = $form_state instanceof SubformStateInterface ? $form_state
->getCompleteFormState() : $form_state;
$form_display
->extractFormValues($block, $block_form, $complete_form_state);
$block
->setInfo($this->configuration['label']);
$this->configuration['block_serialized'] = serialize($block);
}
protected function blockAccess(AccountInterface $account) {
if ($entity = $this
->getEntity()) {
return $entity
->access('view', $account, TRUE);
}
return AccessResult::forbidden();
}
public function build() {
$block = $this
->getEntity();
return $this->entityTypeManager
->getViewBuilder($block
->getEntityTypeId())
->view($block, $this->configuration['view_mode']);
}
protected function getEntity() {
if (!isset($this->blockContent)) {
if (!empty($this->configuration['block_serialized'])) {
$this->blockContent = unserialize($this->configuration['block_serialized']);
}
elseif (!empty($this->configuration['block_revision_id'])) {
$entity = $this->entityTypeManager
->getStorage('block_content')
->loadRevision($this->configuration['block_revision_id']);
$this->blockContent = $entity;
}
else {
$this->blockContent = $this->entityTypeManager
->getStorage('block_content')
->create([
'type' => $this
->getDerivativeId(),
'reusable' => FALSE,
]);
}
if ($this->blockContent instanceof RefinableDependentAccessInterface && ($dependee = $this
->getAccessDependency())) {
$this->blockContent
->setAccessDependency($dependee);
}
}
return $this->blockContent;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
if ($this->isNew) {
unset($form['label']['#default_value']);
}
$form['label']['#description'] = $this
->t('The title of the block as shown to the user.');
return $form;
}
public function saveBlockContent($new_revision = FALSE, $duplicate_block = FALSE) {
$block = NULL;
if (!empty($this->configuration['block_serialized'])) {
$block = unserialize($this->configuration['block_serialized']);
}
if ($duplicate_block) {
if (empty($block) && !empty($this->configuration['block_revision_id'])) {
$block = $this->entityTypeManager
->getStorage('block_content')
->loadRevision($this->configuration['block_revision_id']);
}
if ($block) {
$block = $block
->createDuplicate();
}
}
if ($block) {
if ($new_revision) {
$block
->setNewRevision();
}
$block
->save();
$this->configuration['block_revision_id'] = $block
->getRevisionId();
$this->configuration['block_serialized'] = NULL;
}
}
}