Search code examples
phpsymfonycontent-management-systemezpublishezplatform

Common content in all views from a shared symfony base controller


I'm using ezplatform and trying to figure out how to automatically share common cms content to all pages without writing a separate controller for each view.

This is a simple extract from the yml file:

root_folder:
    controller: "AppBundle:Homepage:homepage"
    match:
        Id\Location: 58

article_container:
  controller: "AppBundle:ArticleContainer:articleContainerChildren"
  match:
      Identifier\ContentType: "article_container"

article_one_column:
    template: "full/article_one_column.html.twig"
    match:
        Identifier\ContentType: "article_one_column"

These are three simple matches to there own custom controllers, with the exception of the last.

The first two matches hit a controller which extends a base controller. Within the base controller we have a standard render function which is called like this:

return $this->render(
    'full/article_container.html.twig',
    [
        'location'         => $location,
        'content'          => $currentContent,
        'articles'         => $articles,
        'articleLocations' => $locations,
    ]
);

The said render function looks like this:

$parameters = array_merge($parameters, [
    'main_navi'         => $navigation,
    'mega_navi_data'    => $navigation,
    'quotes'            => $contentRenderer->getQuotesData(),
    'featured_articles' => $contentRenderer->getFeaturedArticles(),
    'contact_form'      => $this->getContactForm(),
]);

return parent::render($view, $parameters, $response);

As you can see we simply merge the original params with some common data that all pages need. This data is used for the "maga navi" and the footer content.

The problem is now that when we try to load a article_one_column page, as this is not using a custom controller it therefore doesn't load the common data required for the header and footer which results in an twig error.

QUESTION: How can we deliver common content to all routes without writing a custom controller for each data type?


Solution

  • You don't have to create a controller for each Content Type. What you want, if I may, is to inject data into the view.

    Using a custom controller is one of the options, for generic stuff that you want to inject you can also use https://doc.ez.no/display/DEVELOPER/Injecting+parameters+in+content+views

    We have bundled this concept (among other here: https://github.com/Novactive/NovaeZExtraBundle) with a concept of ChildrenProvider.

    Also, you mentioned you wanted to inject information related to the header and the footer in the view to get them in the layout. It sounds weird. With eZ but also just with Symfony you can render a controller from a view, them you can render the header and render the footer independently from the view. (that is probably what you should do)

    For the children though, I like to use the listener instead of the custom controller. It is managed in the Novactive bundle but be careful if you do it on your own, the pre_content_view listener will trigger on each view (line, full, etc..) And usually, you want to inject stuff in the view full only.