eZ Platform Discussions

Discuss about platform-http-cache and how to use it properly


#1

Good morning, everyone.

While working on the landing page and the “Content List” block (ee) I discovered the EzSystemsPlatformHttpCacheBundle which seems to be an important stone for a modern web infrastructure.

ContentListBlockListener.php

    public function onBlockResponse(BlockResponseEvent $event): void
    {
        $response = $event->getResponse();
        //..
        $searchHits = $this->findContentItems( // ....
        //..
        $tags = [];
        foreach ($searchHits as $searchHit) {
            $location = $searchHit->valueObject;
            $tags[] = 'relation-'.$location->contentId;
        }

        $this->tagHandler->addTags(array_unique($tags));
        $this->tagHandler->tagResponse($response);
    }

EzPlatformPageFieldTypeBundle:blocks:contentlist.html.twig

            {% for content in contentArray %}
                {{ fos_httpcache_tag('relation-' ~ content.content.id) }}
            {% endfor %}

We notice that the tags are added twice.

  1. In the php
  2. in the twig

Why? What does that mean?


In my project I have an object of type UNE.
This object is inserted in the rich text of an article. It is therefore displayed in content/embed view.

It happens to have a function similar to the “Content List” block: automatic content retrieval.

UneController.php

    /** content/embed UNE */
    public function embedAction(ContentView $view)
    {
        /** @var \EzSystems\PlatformHttpCacheBundle\Handler\TagHandler $tagHandler */
        $tagHandler = $this->container->get('fos_http_cache.handler.tag_handler');

        /** @var Content $list */
        $list = [];

        /** @var string[] $tags 'relation-[CONTENT_ID]' */
        $tags = [];

            $query = new Query();
            // ...
            $searchResult = $this->getRepository()->getSearchService()->findContent($query);

            foreach ($searchResult->searchHits as $hit) {
                /** @var Content $content */
                $content = $hit->valueObject;
                $list[] = $content;
                $tags[] = 'relation-'.$content->id;
            }

        $tagHandler->addTags(array_unique($tags));
        // $tagHandler->tagResponse($view->getResponse()); // Type error: Argument 1 passed to TagHandler::tagResponse() must be an instance of Symfony\Component\HttpFoundation\Response, null given,

        $view->addParameters([
            'list' => $list,
        ]);
        return $view;
    }

Is it useful in my case to add the tagHandler?