Skip to content
Snippets Groups Projects

Load latest revision of context entity when content_moderation is enabled and...

Files
3
@@ -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,20 @@ class ParagraphBlock extends BlockBase implements ContextAwarePluginInterface, C
return $this->getContextValue('entity');
}
/**
* @param ContentEntityInterface $entity
* @param int|string $delta
*
* @return ?\Drupal\paragraphs\Entity\Paragraph
*/
private function getReferencedParagraph(ContentEntityInterface $entity, int|string $delta) {
$referenced_entities = $entity
->get($this->fieldName)
->referencedEntities();
if (isset($referenced_entities[$delta])) {
$paragraph = $referenced_entities[$delta];
}
return $paragraph ?? NULL;
}
}
Loading