Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-2597: Removing first Pesonalized block from page builder editor removes both previews #113

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"ramsey/uuid": "^3.9",
"symfony/framework-bundle": "^5.0",
"symfony/twig-bundle": "^5.0",
"symfony/webpack-encore-bundle": "^1.8",
"webmozart/assert": "^1.0"
},
"require-dev": {
Expand Down Expand Up @@ -57,6 +58,7 @@
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": false
}
}
20 changes: 0 additions & 20 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -860,11 +860,6 @@ parameters:
count: 1
path: src/lib/Service/ContentService.php

-
message: "#^Comparison operation \"\\>\" between int\\<1, max\\> and 0 is always true\\.$#"
count: 1
path: src/lib/Service/ContentService.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Service\\\\ContentService\\:\\:fetchContent\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
Expand All @@ -885,21 +880,6 @@ parameters:
count: 1
path: src/lib/Service/ContentService.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Service\\\\ContentService\\:\\:prepareFields\\(\\) has parameter \\$fields with no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Service/ContentService.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Service\\\\ContentService\\:\\:prepareFields\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Service/ContentService.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Service\\\\ContentService\\:\\:prepareFields\\(\\) should return array but returns array\\<int, mixed\\>\\|null\\.$#"
count: 1
path: src/lib/Service/ContentService.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Service\\\\ContentService\\:\\:setContent\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
Expand Down
22 changes: 21 additions & 1 deletion src/bundle/Controller/RecommendationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface;
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;
use Twig\Environment;

class RecommendationController
Expand All @@ -26,16 +28,26 @@ class RecommendationController
/** @var \EzSystems\EzRecommendationClient\Config\CredentialsResolverInterface */
private $credentialsResolver;

/** @var \Symfony\WebpackEncoreBundle\Asset\TagRenderer */
private $encoreTagRenderer;

/** @var \Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface */
private $entrypointLookupCollection;

/** @var \Twig\Environment */
private $twig;

public function __construct(
EventDispatcherInterface $eventDispatcher,
CredentialsResolverInterface $credentialsResolver,
TagRenderer $encoreTagRenderer,
EntrypointLookupCollectionInterface $entrypointLookupCollection,
Environment $twig
) {
$this->eventDispatcher = $eventDispatcher;
$this->credentialsResolver = $credentialsResolver;
$this->encoreTagRenderer = $encoreTagRenderer;
$this->entrypointLookupCollection = $entrypointLookupCollection;
$this->twig = $twig;
}

Expand All @@ -60,12 +72,20 @@ public function showRecommendationsAction(Request $request): Response
$response = new Response();
$response->setPrivate();

return $response->setContent(
$this->encoreTagRenderer->reset();
$this->entrypointLookupCollection->getEntrypointLookup('ezplatform')->reset();

Comment on lines +75 to +77
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming that this is done inside a sub-request of sorts, and for whatever reason the scripts/links are not treated separately by Symfony, you should also reset the internal cache after the response is generated. It is done so in the example provided:

// src/Controller/SomeController.php

use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;

class SomeController
{
    public function index(EntrypointLookupInterface $entrypointLookup)
    {
        $entrypointLookup->reset();
        // render a template

        $entrypointLookup->reset();  // <------ Note the second call
        // render another template

        // ...
    }
}

So what I think you want to do is store the response in a variable instead of returning it immediately, and call resets a second time. And only then return the response.

Without it, any scripts/links generated in the template will not be likewise present in the parent response. There is a practice where JS is included at the end of the HTML document, and that addresses it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added second call: a16f380

$response->setContent(
$this->twig()->render($template, [
'recommendations' => $event->getRecommendationItems(),
'templateId' => Uuid::uuid4()->toString(),
])
);

$this->encoreTagRenderer->reset();
$this->entrypointLookupCollection->getEntrypointLookup('ezplatform')->reset();

return $response;
}

protected function twig(): Environment
Expand Down
2 changes: 2 additions & 0 deletions src/bundle/Resources/config/services/controllers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ services:
public: true
arguments:
$credentialsResolver: '@EzSystems\EzRecommendationClient\Config\EzRecommendationClientCredentialsResolver'
$encoreTagRenderer: '@webpack_encore.tag_renderer'
$entrypointLookupCollection: '@webpack_encore.entrypoint_lookup_collection'

ez_recommendation:
alias: EzSystems\EzRecommendationClientBundle\Controller\RecommendationController
Expand Down
8 changes: 6 additions & 2 deletions src/lib/Service/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,14 @@ private function getAuthor(APIContent $contentValue, APIContentType $contentType

/**
* Checks if fields are given, if not - returns all of them.
*
* @param array<string> $fields
*
* @return array<string>
*/
private function prepareFields(APIContentType $contentType, ?array $fields = null): array
private function prepareFields(APIContentType $contentType, array $fields): array
{
if ($fields && \count($fields) > 0) {
if (empty($fields)) {
return $fields;
}

Expand Down