Details
-
Bug
-
Resolution: Fixed
-
High
-
5.4.10
-
None
Description
When using a custom controller alongside a ViewController for displaying a content type, it is not possible to use "Preview" feature for drafts correctly.
When using the custom controller, the developer should forward the request to the original ViewController. He can do that in two ways - using viewLocation or viewContent functions. The example in the documentation uses viewLocation function, however using it with drafts is not supported as they do not have a location, which leads to errors (see https://jira.ez.no/browse/EZP-27123?focusedCommentId=213938&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-213938).
The second option is to use viewContent function. However, this function always loads the last published version of a Content Object instead of the version that is being previewed.
Steps to reproduce (using viewContent)
- Create fresh eZ Publish 5.4 installation.
- In the "src" directory of your project, create new AppBundle directory with new bundle. There, create the following files (or use the attached files):
- AppBundle/Controller/DefaultController with the following code:
<?php namespace AppBundle\Controller; use eZ\Bundle\EzPublishCoreBundle\Controller; use eZ\Publish\API\Repository\Values\Content\Location; class DefaultController extends Controller { public function articleViewEnhancedAction( Location $location, $viewType, $layout = false, array $params = array() ) { if (isset($params['isPreview'])) { $response = $this->get('ez_content')->viewContent( $location->contentInfo->id, $viewType, $layout, $params ); } else { $response = $this->get('ez_content')->viewLocation($location->id, $viewType, $layout, $params); } return $response; } }
- AppBundle/Resources/views/full/article.html.twig with the following code:
<h1>{{ ez_render_field( content, 'title' ) }}</h1>
- AppBundle/Controller/DefaultController with the following code:
- Register AppBundle by adding the following line to the bundles array in ezpublish/EzPublishKernel.php file:
new AppBundle\AppBundle(),
- Paste the following configuration to the ezpublish/config/ezpublish.yml file:
ezpublish: system: ezdemo_site_clean_group: location_view: full: article: controller: AppBundle:Default:articleViewEnhanced template: AppBundle:full:article.html.twig match: Identifier\ContentType: [article] content_view: full: article: template: AppBundle:full:article.html.twig match: Identifier\ContentType: [article]
- In the legacy admin, go to "Content structure". Under "Home", create a new Article Content Object titled "Test" and publish it.
- Edit the "Test" Article. Change its title to "Test2" and save the draft without publishing.
- Under "Current draft" click "Preview" button.
- On the top, the correct Article draft name will be shown ("Test2"). However, in the preview window beneath it there will be the old title shown ("Test").