sonata-project / SonataPageBundle

This bundle provides a Site and Page management through container and block services
https://docs.sonata-project.org/projects/SonataPageBundle
MIT License
219 stars 210 forks source link

path() twig helper func call with ($myPage, 'true') arguments raises an exception: CmsPageRouter::generateFromPage() must be of the type array, boolean given #305

Closed ronanguilloux closed 10 years ago

ronanguilloux commented 10 years ago

Summary : Use of the path()twig helper with a opt boolean argument (as documented in the Sonata Page Twig Helpers documentation page) raises an error in the routing service

Scenario details : In SonataAdmin,

and the tricky part: call the path() Twig function using the optional boolean argument:

 {{ path(myPage, true) }}

Important note: adding the ', true' optional argument to the path()func call raises the error below (see detailled trace). While removing this opt argument, error is no more raised, & returned link URL is ok.

Custom Block Twig template :

{% extends sonata_block.templates.block_base %}
{% block block %}
    <div class="sonata-page-block-myPage">
        {% if myPage %}
        <h3><a href="{{ path(myPage, true) }}">{{ myPage.getTitle }}</a></h3>
        {% endif %}

        {% sonata_template_box 'This is the myPage block.' %}
    </div>
{% endblock %}

Custom Block Service PHP code :


namespace Application\Sonata\PageBundle\Block\Service;

use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\CoreBundle\Model\ManagerInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Validator\ErrorElement;

use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Block\BaseBlockService;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

use Sonata\PageBundle\Exception\PageNotFoundException;
use Sonata\PageBundle\Site\SiteSelectorInterface;
use Sonata\PageBundle\CmsManager\CmsManagerSelectorInterface;

class MyBlockService extends BaseBlockService
{
    protected $manager;
    protected $siteSelector;
    protected $cmsManagerSelector;

    /**
     * @param string                        $name
     * @param EngineInterface               $templating
     * @param SiteSelectorInterface         $siteSelector
     * @param CmsManagerSelectorInterface   $cmsManagerSelector
     * @param ManagerInterface              $manager
     */
    //public function __construct($name, EngineInterface $templating, ManagerInterface $manager)
    public function __construct($name, EngineInterface $templating, SiteSelectorInterface $siteSelector, CmsManagerSelectorInterface $cmsManagerSelector)
    {
        parent::__construct($name, $templating);

        $this->siteSelector       = $siteSelector;
        $this->cmsManagerSelector = $cmsManagerSelector;
    }

    /**
     * {@inheritdoc}
     */
    public function execute(BlockContextInterface $blockContext, Response $response = null)
    {
        $settings = $blockContext->getSettings();
        $cmsManager = $this->cmsManagerSelector->retrieve();
        $site = $this->siteSelector->retrieve();

        try {
            $myPage = $cmsManager->getPage($site, 85);
        } catch (PageNotFoundException $e) {
            $myPage = false;
        }

        $criteria = array(
            'mode' => $settings['mode']
        );

        $parameters = array(
            'context'   => $blockContext,
            'settings'  => $settings,
            'block'     => $blockContext->getBlock(),
            'myPage'  => $myPage,
        );

        if ($settings['mode'] === 'admin') {
            return $this->renderPrivateResponse($blockContext->getTemplate(), $parameters, $response);
        }

        return $this->renderResponse($blockContext->getTemplate(), $parameters, $response);
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'My Page';
    }

    /**
     * {@inheritdoc}
     */
    public function setDefaultSettings(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'title'      => 'my Page title',
            'mode'       => 'public',
            'template'   => 'ApplicationSonataPageBundle:Block:myBlock.html.twig'
        ));
    }

Error trace :

Catchable fatal error: Argument 2 passed to Sonata\PageBundle\Route\CmsPageRouter::generateFromPage() must be of the type array, boolean given, called in vendor/sonata-project/page-bundle/Route/CmsPageRouter.php on line 115 and defined in vendor/sonata-project/page-bundle/Route/CmsPageRouter.php on line 197
Call Stack
#TimeMemoryFunctionLocation
10.0050240448{main}(  )../app_dev.php:0
20.08581947896Symfony\Component\HttpKernel\Kernel->handle(  )../app_dev.php:26
317.420550841544Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(  )../bootstrap.php.cache:2245
417.420550843784Symfony\Component\HttpKernel\HttpKernel->handle(  )../bootstrap.php.cache:2944
517.420550843848Symfony\Component\HttpKernel\HttpKernel->handleRaw(  )../bootstrap.php.cache:2815
618.549653793040call_user_func_array
(  )../bootstrap.php.cache:2841
718.549653793656Sonata\PageBundle\Page\PageServiceManager->execute(  )../bootstrap.php.cache:2841
818.557853816176Sonata\PageBundle\Page\Service\DefaultPageService->execute(  )../PageServiceManager.php:117
918.557853817800Sonata\PageBundle\Page\TemplateManager->renderResponse(  )../DefaultPageService.php:63
1018.557953818568Symfony\Bundle\TwigBundle\TwigEngine->renderResponse(  )../TemplateManager.php:155
1118.557953818632Symfony\Bundle\TwigBundle\Debug\TimedTwigEngine->render(  )../TwigEngine.php:112
1218.557953819624Symfony\Bundle\TwigBundle\TwigEngine->render(  )../TimedTwigEngine.php:52
1318.557953819784Symfony\Bridge\Twig\TwigEngine->render(  )../TwigEngine.php:83
1419.131556892256Twig_Template->render(  )../TwigEngine.php:53
1519.131656908960Twig_Template->display(  )../Template.php:263
1619.142856929896Twig_Template->displayWithErrorHandling(  )../Template.php:252
1719.142856930088__TwigTemplate_1599b356247ca8d8920d54d30f5f669fec04ca8fccc9eab288c51a0b0d88da9b->doDisplay(  )../Template.php:278
1819.142856930704Twig_Template->display(  )../b356247ca8d8920d54d30f5f669fec04ca8fccc9eab288c51a0b0d88da9b.php:26
1919.142856930768Twig_Template->displayWithErrorHandling(  )../Template.php:252
2019.142856930960__TwigTemplate_86e6d16f1a3e774b8207e3cc696f64d55018cfc3e9979b8ffaba3f86bd6b521c->doDisplay(  )../Template.php:278
2119.144156951352Twig_Template->displayBlock(  )../d16f1a3e774b8207e3cc696f64d55018cfc3e9979b8ffaba3f86bd6b521c.php:50
2219.144156951872__TwigTemplate_1599b356247ca8d8920d54d30f5f669fec04ca8fccc9eab288c51a0b0d88da9b->block_sonata_page_container(  )../Template.php:144
2320.007358956312Twig_Template->displayBlock(  )../b356247ca8d8920d54d30f5f669fec04ca8fccc9eab288c51a0b0d88da9b.php:73
2420.007358956720__TwigTemplate_1599b356247ca8d8920d54d30f5f669fec04ca8fccc9eab288c51a0b0d88da9b->block_page_content(  )../Template.php:144
2520.007458958712Sonata\PageBundle\Twig\Extension\PageExtension->renderContainer(  )../b356247ca8d8920d54d30f5f669fec04ca8fccc9eab288c51a0b0d88da9b.php:161
2620.007458958848Sonata\PageBundle\Twig\Extension\PageExtension->renderBlock(  )../PageExtension.php:234
2720.007558960416Sonata\BlockBundle\Templating\Helper\BlockHelper->render(  )../PageExtension.php:264
2820.007958968496Sonata\BlockBundle\Block\BlockRenderer->render(  )../BlockHelper.php:366
2920.008858975240Sonata\BlockBundle\Block\Service\ContainerBlockService->execute(  )../BlockRenderer.php:87
3020.008858976328Sonata\BlockBundle\Block\BaseBlockService->renderResponse(  )../ContainerBlockService.php:68
3120.008858976328Symfony\Bundle\TwigBundle\TwigEngine->renderResponse(  )../BaseBlockService.php:53
3220.008858976328Symfony\Bundle\TwigBundle\Debug\TimedTwigEngine->render(  )../TwigEngine.php:112
3320.008858976464Symfony\Bundle\TwigBundle\TwigEngine->render(  )../TimedTwigEngine.php:52
3420.008858976464Symfony\Bridge\Twig\TwigEngine->render(  )../TwigEngine.php:83
3520.008858976464Twig_Template->render(  )../TwigEngine.php:53
3620.008958993088Twig_Template->display(  )../Template.php:263
3720.008958994248Twig_Template->displayWithErrorHandling(  )../Template.php:252
3820.008958994248__TwigTemplate_3613d44973a86a57d6488a82288773b9afcc1158a090cefa0861db6305a2e15e->doDisplay(  )../Template.php:278
3920.008958994576Twig_Template->display(  )../d44973a86a57d6488a82288773b9afcc1158a090cefa0861db6305a2e15e.php:24
4020.008958994576Twig_Template->displayWithErrorHandling(  )../Template.php:252
4120.008958994576__TwigTemplate_7b4f598022b3e326289cccc3df4953fb010bf351022555374e5e985b538e488e->doDisplay(  )../Template.php:278
4220.008958995208Twig_Template->display(  )../598022b3e326289cccc3df4953fb010bf351022555374e5e985b538e488e.php:25
4320.008958995208Twig_Template->displayWithErrorHandling(  )../Template.php:252
4420.008958995208__TwigTemplate_708dcb0f663842ac24341a7c898126ae7660cc9834ba19119c20624b87b285bf->doDisplay(  )../Template.php:278
4520.009158996728Twig_Template->displayBlock(  )../cb0f663842ac24341a7c898126ae7660cc9834ba19119c20624b87b285bf.php:80
4620.009158997344__TwigTemplate_7b4f598022b3e326289cccc3df4953fb010bf351022555374e5e985b538e488e->block_block(  )../Template.php:144
4720.009159001664Twig_Template->displayBlock(  )../598022b3e326289cccc3df4953fb010bf351022555374e5e985b538e488e.php:75
4820.009159002376__TwigTemplate_3613d44973a86a57d6488a82288773b9afcc1158a090cefa0861db6305a2e15e->block_block_child_render(  )../Template.php:144
4920.009159002456Sonata\PageBundle\Twig\Extension\PageExtension->renderBlock(  )../d44973a86a57d6488a82288773b9afcc1158a090cefa0861db6305a2e15e.php:32
5020.009159004024Sonata\BlockBundle\Templating\Helper\BlockHelper->render(  )../PageExtension.php:264
5120.009559012600Sonata\BlockBundle\Block\BlockRenderer->render(  )../BlockHelper.php:366
5220.010459019376Application\Sonata\PageBundle\Block\Service\MyBlockService->execute(  )../BlockRenderer.php:87
5320.015159050440Sonata\BlockBundle\Block\BaseBlockService->renderResponse(  )../MyBlockService.php:100
5420.015159050520Symfony\Bundle\TwigBundle\TwigEngine->renderResponse(  )../BaseBlockService.php:53
5520.015159050520Symfony\Bundle\TwigBundle\Debug\TimedTwigEngine->render(  )../TwigEngine.php:112
5620.015159051440Symfony\Bundle\TwigBundle\TwigEngine->render(  )../TimedTwigEngine.php:52
5720.015159051440Symfony\Bridge\Twig\TwigEngine->render(  )../TwigEngine.php:83
5820.068858864280Twig_Template->render(  )../TwigEngine.php:53
5920.068858880984Twig_Template->display(  )../Template.php:263
6020.068858882208Twig_Template->displayWithErrorHandling(  )../Template.php:252
6120.068858882400__TwigTemplate_347e40cdf769ea6ddce58af96c5918a7018b70f47c20455123b7ec47a077c915->doDisplay(  )../Template.php:278
6220.069058884264Twig_Template->display(  )../40cdf769ea6ddce58af96c5918a7018b70f47c20455123b7ec47a077c915.php:22
6320.069058884264Twig_Template->displayWithErrorHandling(  )../Template.php:252
6420.069058884264__TwigTemplate_708dcb0f663842ac24341a7c898126ae7660cc9834ba19119c20624b87b285bf->doDisplay(  )../Template.php:278
6520.069358885784Twig_Template->displayBlock(  )../cb0f663842ac24341a7c898126ae7660cc9834ba19119c20624b87b285bf.php:80
6620.069358886080__TwigTemplate_347e40cdf769ea6ddce58af96c5918a7018b70f47c20455123b7ec47a077c915->block_block(  )../Template.php:144
6720.069458886464Symfony\Bridge\Twig\Extension\RoutingExtension->getPath(  )../40cdf769ea6ddce58af96c5918a7018b70f47c20455123b7ec47a077c915.php:37
6820.069458886640Symfony\Cmf\Component\Routing\ChainRouter->generate(  )../RoutingExtension.php:45
6920.069458887112Sonata\PageBundle\Route\CmsPageRouter->generate(  )../ChainRouter.php:229
  7020.069458887336Sonata\PageBundle\Route\CmsPageRouter->generateFromPage(  )../CmsPageRouter.php:115

app/config YAML file:

sonata_block:
    context_manager: sonata.page.block.context_manager
    default_contexts: [cms]
    blocks:
        (...)
        sonata.page.block.service.ateliers:

composer.json:

(...)
"require": {
        "php":                                      ">=5.3.3",

        "twig/twig":                                "~1.12",
        "twig/extensions":                          "~1.0",

        "symfony/symfony":                          "~2.3.7",
        "symfony/assetic-bundle":                   "~2.3",
        "symfony/swiftmailer-bundle":               "~2.3",
        "symfony/monolog-bundle":                   "~2.3",

        "sensio/distribution-bundle":               "~2.3",
        "sensio/framework-extra-bundle":            "~2.3",
        "sensio/generator-bundle":                  "~2.3",

        "jms/security-extra-bundle":                "~1.5",
        "jms/di-extra-bundle":                      "~1.4",
        "jms/serializer-bundle":                    "~0.11",

        "doctrine/orm":                             "~2.4",
        "doctrine/doctrine-bundle":                 "~1.2",
        "doctrine/doctrine-migrations-bundle":      "~2.0dev",
        "doctrine/migrations":                      "~1.0@dev",
        "doctrine/doctrine-fixtures-bundle":        "~2.2",
        "doctrine/data-fixtures":                   "~1.0@dev",

        "symfony-cmf/routing-bundle":               "~1.1",

        "knplabs/gaufrette":                        "0.1.4",
        "knplabs/knp-menu-bundle":                  "1.1.x-dev",
        "knplabs/knp-markdown-bundle":              "1.2.*@dev",

        "sonata-project/easy-extends-bundle":       "~2.1",
        "sonata-project/seo-bundle":                "~1",
        "sonata-project/doctrine-extensions":       "~1",
        "sonata-project/intl-bundle":               "~2.1",
        "sonata-project/admin-bundle":              "~2.2",
        "sonata-project/doctrine-orm-admin-bundle": "~2.2",
        "sonata-project/notification-bundle":       "~2.2",
        "sonata-project/block-bundle":              "~2.2",
        "sonata-project/media-bundle":              "~2.2",
        "sonata-project/user-bundle":               "~2.2",
        "sonata-project/cache-bundle":              "~2.1",
        "sonata-project/page-bundle":               "~2.3",
        "sonata-project/formatter-bundle":          "~2.3",
        "sonata-project/exporter":                  "~1.3",
        "sonata-project/classification-bundle":     "~2.2",

        "dflydev/markdown":                         "~1.0",
        "simplethings/entity-audit-bundle":         "~0.5",
        "willdurand/faker-bundle":                  "~1.0",

        "mopa/bootstrap-bundle":                    "v3.0.0-beta3",
        "twbs/bootstrap":                           "v3.0.0",

        "stof/doctrine-extensions-bundle":          "~1.1",

        "incenteev/composer-parameter-handler":     "~2.0",

        "liip/monitor-bundle":                      "1.0.*@dev",
        "liip/monitor":                             "1.0.*@dev"

    },
(...)
ronanguilloux commented 10 years ago

Sorry, my mistake: a BC BREAK from 2012-08-31 changed prototype of "PageExtension::url" method and so "sonata_page_url" Twig helper.

Before: sonata_page_url(page, absolute) After: sonata_page_url(page, {'param1': 'value1', ...}, absolute)

ronanguilloux commented 10 years ago

TODO : documentation page about SonataPage Twig helpers to be updated (WIP)