Content Moderation latest versions do not work with Display Suite view modes

Created on 1 June 2018, about 6 years ago
Updated 27 December 2023, 6 months ago

Posting this here although I will admit I don't know whether this is a Display Suite fix or a fix in the core Content_moderation module.

The issue occurs when using both Display Suite and Content_Moderation + Workflows together.
1. Create two different display view modes, "full" and "alternate"
2. Enable Content_moderation for that node type
3. Create a new node and set it as "alternate" view mode
4. Edit the node and save as "draft" (content moderation)
5. View the "latest version" tab, which should show a preview if the node draft
6. The node draft will appear in the "full" view mode even if the published node is set to display as "alternate." This is particularly a problem if you are using different view modes to display different fields altogether.

This seems to be an issue with the way the route is built for the "latest draft" version, in EntityModerationRouteProvider.php

protected function getLatestVersionRoute(EntityTypeInterface $entity_type) {
    if ($entity_type->hasLinkTemplate('latest-version') && $entity_type->hasViewBuilderClass()) {
      $entity_type_id = $entity_type->id();
      $route = new Route($entity_type->getLinkTemplate('latest-version'));
      $route
        ->addDefaults([
          '_entity_view' => "{$entity_type_id}.full",
          '_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::title',
        ])
        // If the entity type is a node, unpublished content will be visible
        // if the user has the "view all unpublished content" permission.
        ->setRequirement('_entity_access', "{$entity_type_id}.view")
        ->setRequirement('_content_moderation_latest_version', 'TRUE')
        ->setOption('_content_moderation_entity_type', $entity_type_id)
        ->setOption('parameters', [
          $entity_type_id => [
            'type' => 'entity:' . $entity_type_id,
            'load_latest_revision' => TRUE,
          ],
        ]);

      // Entity types with serial IDs can specify this in their route
      // requirements, improving the matching process.
      if ($this->getEntityTypeIdKeyType($entity_type) === 'integer') {
        $route->setRequirement($entity_type_id, '\d+');
      }
      return $route;
    }
  }

The route has the "full" view mode hard coded into it. Unfortunately I haven't found a way to pass the view mode for a particular node into an AlterRoute to fix this, since the route subscriber content is all cached.

Any help, even just pointing in the right direction, would be great!

✨ Feature request
Status

Fixed

Version

3.0

Component

Code

Created by

🇺🇸United States katyrose

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024