@@ -19,32 +19,35 @@ class PantheonContentPublisherViewController extends EntityViewController {
1919
2020 public function pantheonView (Request $ request , $ pantheon_id ): array {
2121 $ query = $ request ->query ;
22- $ is_preview = $ query ->get ('publishingLevel ' ) === 'REALTIME ' ;
2322 $ collection = $ query ->get ('siteId ' ) ?: array_key_first (PantheonDocumentCollection::loadMultiple ());
23+ if ($ query ->get ('publishingLevel ' ) === 'REALTIME ' ) {
24+ return $ this ->pantheonPreview ($ collection , $ pantheon_id );
25+ }
26+ $ document = $ this ->loadPantheonDocument ($ collection , $ pantheon_id );
27+ $ page = $ this ->view ($ document );
28+ $ page ['#cache ' ]['contexts ' ][] = 'url.query_args:publishingLevel ' ;
29+ return $ page ;
30+ }
31+
32+ public function pantheonPreview ($ collection , $ pantheon_id ) {
2433 try {
25- $ document = PantheonDocument:: load (PantheonDocumentStorage:: getEntityId ( $ collection , $ pantheon_id) );
34+ $ document = $ this -> loadPantheonDocument ( $ collection , $ pantheon_id );
2635 }
2736 catch (GraphQLException $ e ) {
28- if ($ is_preview ) {
29- $ document = PantheonDocument::create (['collection ' => $ collection ]);
30- }
31- else {
32- throw $ e ;
33- }
34- }
35- if ($ is_preview ) {
36- // PantheonTagsFormatter turns this into
37- // <div id="pantheon-content-publisher-preview"></div>
38- $ document ->get ('content ' )->value = '{"attrs":{"id":"pantheon-content-publisher-preview"}} ' ;
37+ $ document = PantheonDocument::create (['collection ' => $ collection ]);
3938 }
39+ // PantheonTagsFormatter turns this into
40+ // <div id="pantheon-content-publisher-preview"></div>
41+ $ document ->get ('content ' )->value = '{"attrs":{"id":"pantheon-content-publisher-preview"}} ' ;
4042 $ page = $ this ->view ($ document );
41- if ($ is_preview ) {
42- $ page ['#attached ' ]['library ' ][] = 'pantheon_content_publisher/preview ' ;
43- $ page ['#attached ' ]['drupalSettings ' ]['pantheon_content_publisher ' ]['site_id ' ] = $ collection ;
44- $ page ['#attached ' ]['http_header ' ][] = [PantheonContentPublisherXFrameSubscriber::HEADER_NAME , '' ];
45- }
43+ $ page ['#attached ' ]['library ' ][] = 'pantheon_content_publisher/preview ' ;
44+ $ page ['#attached ' ]['drupalSettings ' ]['pantheon_content_publisher ' ]['site_id ' ] = $ collection ;
45+ $ page ['#attached ' ]['http_header ' ][] = [PantheonContentPublisherXFrameSubscriber::HEADER_NAME , '' ];
4646 $ page ['#cache ' ]['contexts ' ][] = 'url.query_args:publishingLevel ' ;
4747 return $ page ;
4848 }
4949
50+ protected function loadPantheonDocument (float |bool |int |string |null $ collection , $ pantheon_id ): \Drupal \Core \Entity \EntityBase |\Drupal \Core \Entity \EntityInterface |null |PantheonDocument {
51+ return PantheonDocument::load (PantheonDocumentStorage::getEntityId ($ collection , $ pantheon_id ));
52+ }
5053}
0 commit comments