Skip to content
Snippets Groups Projects
Commit a1b43fe0 authored by Bas Vredeling's avatar Bas Vredeling Committed by Bas Vredeling
Browse files

Issue #3106704 by basvredeling, Arccoss: Allow paragraphs added to a node...

Issue #3106704 by basvredeling, Arccoss: Allow paragraphs added to a node draft to be visible in layout builder
parent e2a55d9a
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@
namespace Drupal\paragraph_blocks;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
......@@ -60,9 +61,43 @@ class ParagraphBlocksEntityManager implements ContainerInjectionInterface {
// If section storage is for a single entity override there will be a entity to return.
if ('overrides' === $section_storage->getPluginId()) {
return $section_storage->getContext('entity')->getContextData()->getEntity();
$entity = $section_storage->getContext('entity')->getContextData()->getEntity();
if (\Drupal::moduleHandler()->moduleExists('content_moderation')) {
return $this->getLatestEntityRevision($entity);
}
return $entity;
}
return null;
}
/**
* Load the latest entity revision when using content moderation.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
*
* @return \Drupal\Core\Entity\ContentEntityInterface
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function getLatestEntityRevision(ContentEntityInterface &$entity) {
/** @var \Drupal\content_moderation\ModerationInformation $moderation_info */
$moderation_info = \Drupal::service('content_moderation.moderation_information');
$pending = $moderation_info->hasPendingRevision($entity);
if ($pending) {
/** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
$entity_type_id = $entity->getEntityTypeId();
$storage = \Drupal::entityTypeManager()
->getStorage($entity_type_id);
$latest_revision_id = $storage->getLatestTranslationAffectedRevisionId($entity->id(), $entity->language()
->getId());
$entity = \Drupal::entityTypeManager()
->getStorage($entity_type_id)
->loadRevision($latest_revision_id);
}
return $entity;
}
}
......@@ -166,6 +166,13 @@ class ParagraphBlocksLabeller {
return FALSE;
}
// Check if we have a pending version of the current entity. If so, load
// that revision of the entity instead so we can build layouts in draft.
if (\Drupal::moduleHandler()->moduleExists('content_moderation')) {
$this->entity = \Drupal::service('paragraph_blocks.entity_manager')
->getLatestEntityRevision($this->entity);
}
// Get the referenced paragraph.
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $this->entity->get($plugin_field_name)->referencedEntities()[$plugin_field_delta];
......
......@@ -3,11 +3,12 @@
namespace Drupal\paragraph_blocks\Plugin\Block;
use Drupal\Component\Utility\Html;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\CacheableMetadata;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -89,16 +90,20 @@ class ParagraphBlock extends BlockBase implements ContextAwarePluginInterface, C
* {@inheritdoc}
*/
public function build() {
// Get the referencing and referenced entity.
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$paragraph = NULL;
// Get the referencing and referenced entity.
$entity = $this->getContextEntity();
if ($entity) {
$referenced_entities = $entity
->get($this->fieldName)
->referencedEntities();
if (isset($referenced_entities[$this->fieldDelta])) {
$paragraph = $referenced_entities[$this->fieldDelta];
$paragraph = $this->getReferencedParagraph($entity, $this->fieldDelta);
// Check if we have a pending version of the entity we want to preview.
if (is_null($paragraph) && \Drupal::moduleHandler()
->moduleExists('content_moderation')) {
/** @var \Drupal\paragraph_blocks\ParagraphBlocksEntityManager $entity_manager */
$entity_manager = \Drupal::service('paragraph_blocks.entity_manager');
$entity = $entity_manager->getLatestEntityRevision($entity);
$paragraph = $this->getReferencedParagraph($entity, $this->fieldDelta);
}
}
if (!$paragraph) {
......@@ -147,4 +152,21 @@ class ParagraphBlock extends BlockBase implements ContextAwarePluginInterface, C
return $this->getContextValue('entity');
}
/**
* @param ContentEntityInterface $entity
* @param mixed $delta
* Integer or string.
*
* @return ?\Drupal\paragraphs\Entity\Paragraph
*/
private function getReferencedParagraph(ContentEntityInterface $entity, mixed $delta) {
$referenced_entities = $entity
->get($this->fieldName)
->referencedEntities();
if (isset($referenced_entities[$delta])) {
$paragraph = $referenced_entities[$delta];
}
return $paragraph ?? NULL;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment