Verified Commit 695846b5 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3044292 by johnwebdev, Sam152, Berdir: Inline content blocks created by...

Issue #3044292 by johnwebdev, Sam152, Berdir: Inline content blocks created by layout builder should not directly be impacted by content moderation
parent 828f9d16
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\content_moderation\Entity\Handler;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Form\FormStateInterface;

/**
@@ -29,4 +30,14 @@ public function enforceRevisionsBundleFormAlter(array &$form, FormStateInterface
    $form['revision']['#description'] = $this->t('Revisions must be required when moderation is enabled.');
  }

  /**
   * {@inheritdoc}
   */
  public function isModeratedEntity(ContentEntityInterface $entity) {
    // Only reusable blocks can be moderated individually. Non-reusable or
    // inline blocks are moderated as part of the entity they are a composite
    // of.
    return $entity->isReusable();
  }

}
+8 −0
Original line number Diff line number Diff line
@@ -28,6 +28,14 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
    return new static();
  }

  /**
   * {@inheritdoc}
   */
  public function isModeratedEntity(ContentEntityInterface $entity) {
    // Moderate all entities included in the moderation workflow by default.
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
+23 −0
Original line number Diff line number Diff line
@@ -16,6 +16,29 @@
 */
interface ModerationHandlerInterface {

  /**
   * Determines if an entity should be moderated.
   *
   * At the workflow level, moderation is enabled or disabled for entire entity
   * types or bundles. After a bundle has been enabled, there maybe be further
   * decisions each entity type may make to evaluate if a given entity is
   * appropriate to be included in a moderation workflow. The handler is only
   * consulted after the user has configured the associated entity type and
   * bundle to be included in a moderation workflow.
   *
   * Returning FALSE will remove the moderation state field widget from the
   * associated entity form and opt out of all moderation related entity
   * semantics, such as creating new revisions and changing the publishing
   * status of a revision.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity we may be moderating.
   *
   * @return bool
   *   TRUE if this entity should be moderated, FALSE otherwise.
   */
  public function isModeratedEntity(ContentEntityInterface $entity);

  /**
   * Operates on moderated content entities preSave().
   *
+4 −2
Original line number Diff line number Diff line
@@ -52,8 +52,10 @@ public function isModeratedEntity(EntityInterface $entity) {
    if (!$entity instanceof ContentEntityInterface) {
      return FALSE;
    }

    return $this->shouldModerateEntitiesOfBundle($entity->getEntityType(), $entity->bundle());
    if (!$this->shouldModerateEntitiesOfBundle($entity->getEntityType(), $entity->bundle())) {
      return FALSE;
    }
    return $this->entityTypeManager->getHandler($entity->getEntityTypeId(), 'moderation')->isModeratedEntity($entity);
  }

  /**
+55 −1
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\Tests\content_moderation\Functional;

use Drupal\block_content\Entity\BlockContentType;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
@@ -24,6 +25,7 @@ class LayoutBuilderContentModerationIntegrationTest extends BrowserTestBase {
    'node',
    'content_moderation',
    'menu_ui',
    'block_content',
  ];

  /**
@@ -41,10 +43,21 @@ protected function setUp(): void {
    //   https://www.drupal.org/project/drupal/issues/2917777.
    $this->drupalPlaceBlock('local_tasks_block');

    $workflow = $this->createEditorialWorkflow();

    // Add a new bundle and add an editorial workflow.
    $this->createContentType(['type' => 'bundle_with_section_field']);
    $workflow = $this->createEditorialWorkflow();
    $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'bundle_with_section_field');

    // Add a new block content bundle to the editorial workflow.
    BlockContentType::create([
      'id' => 'basic',
      'label' => 'Basic',
      'revision' => 1,
    ])->save();
    block_content_add_body_field('basic');

    $workflow->getTypePlugin()->addEntityTypeAndBundle('block_content', 'basic');
    $workflow->save();

    // Enable layout overrides.
@@ -62,6 +75,7 @@ protected function setUp(): void {
      'view latest version',
      'use editorial transition create_new_draft',
      'use editorial transition publish',
      'create and edit custom blocks',
    ]));
  }

@@ -138,4 +152,44 @@ public function testLayoutModeration() {
    $assert_session->pageTextNotContains('Powered by Drupal');
  }

  /**
   * Test placing inline blocks that belong to a moderated custom block bundle.
   */
  public function testModeratedInlineBlockBundles() {
    $page = $this->getSession()->getPage();
    $assert_session = $this->assertSession();

    $node = $this->createNode([
      'type' => 'bundle_with_section_field',
      'title' => 'The first node title',
      'moderation_state' => 'published',
    ]);
    $this->drupalGet("node/{$node->id()}/layout");
    $page->clickLink('Add block');
    $this->clickLink('Create custom block');

    $assert_session->fieldNotExists('settings[block_form][moderation_state][0][state]');
    $this->submitForm([
      'settings[label]' => 'Test inline block',
      'settings[block_form][body][0][value]' => 'Example block body',
    ], 'Add block');

    // Save a draft of the page with the inline block and ensure the drafted
    // content appears on the latest version page.
    $this->assertSession()->pageTextContains('Example block body');
    $this->submitForm([
      'moderation_state[0][state]' => 'draft',
    ], 'Save layout');
    $assert_session->pageTextContains('The layout override has been saved.');
    $assert_session->pageTextContains('Example block body');

    // Publish the draft of the page ensure the draft inline block content
    // apears on the published page.
    $this->submitForm([
      'new_state' => 'published',
    ], 'Apply');
    $assert_session->pageTextContains('The moderation state has been updated.');
    $assert_session->pageTextContains('Example block body');
  }

}
Loading