4. Getting Started

The bundle works on top of 2 simple models :
  • an Article: An article is composed of Fragments and contains information about a content (status, type, publication date, categories, etc…)
  • a Fragment: A fragment contains information about a part of the full content. Each fragment has its own template.

4.1. Rendering an article

Caution

This part is currently in development.

The goal is to provide a simple Controller and Dynamic route which will fetch an article, then if all checks succeed, display it for the user:

public function indexAction(Request $request, int $id): Response
{
    /** @var Page $page */
    $page = $this->getPage($request);
    $article = $this->entityManager->find(Article::class, $id);

    if (!$article) {
        throw new NotFoundHttpException(sprintf('No article found for ID "%s"', $id));
    }

    if ($article->getStatus() != Article::STATUS_PUBLISHED) {
        throw new NotFoundHttpException(sprintf('No article published found for ID "%s"', $id));
    }

    $now = new \DateTime();
    if ($article->getPublicationStartsAt() > $now
        || (!is_null($article->getPublicationEndsAt()) && $article->getPublicationEndsAt() < $now)) {
        throw new NotFoundHttpException(sprintf('No article found between publication dates for ID "%s"', $id));
    }

    $page->setTitle(sprintf('Articles - %s', $article->getTitle()));

    return $this->renderResponse($page, [
        'article' => $article,
        'page'    => $page,
    ]);
}

public function renderResponse(PageInterface $page, array $settings = []): Response
{
    $cms  = $this->cmsSelector->retrieve();
    $code = $page ? $cms->getCurrentPage()->getTemplateCode() : null;

    return new Response($this->templating->render(
        $this->templateManager->get($code)->getPath(),
        $settings
    ));
}

public function getPage(Request $request): PageInterface
{
    $cms = $this->cmsSelector->retrieve();
    $page = $cms->getCurrentPage();
    $slug = $request->get('slug');

    if (!$page && $slug) {
        throw new NotFoundHttpException('Sonata page not found. Add them from the admin.');
    }

    // This is required to avoid Sonata from stripping some params send to the twig
    $page->setDecorate(false);

    return $page;
}

4.2. Render article fragments

Caution

This part is currently in development.

SonataArticleBundle now comes with a twig helper which allows you to render article fragments if they are enabled.

1
{{ sonata_article_render_article_fragments(article) }}

Or a specific fragment whether it is enabled or not.

1
{{ sonata_article_render_fragment(article.fragments[0]) }}

This extension is based on the FragmentHelper class so you can also render fragments directly in the controller:

public function indexAction(Request $request, int $id): Response
{
    $article = $this->entityManager->find(Article::class, $id);

    // ...

    $fragmentsRender = '';
    $fragmentsHelper = $this->get('sonata.article.helper.fragment');

    foreach ($article->getFragments() as $fragment) {
        if ($fragment->getEnabled()) {
            $fragmentsRender .= $this->renderFragment($fragment);
        }
    }

    // ...
}