Symfony Sonata块传递自定义参数

Symfony Sonata块传递自定义参数,symfony,sonata-admin,Symfony,Sonata Admin,我是新的奏鸣曲组合。 我想在我的街区放一张地图。它使用了一些JS库。功能的上下文,我需要通过不同的高度,宽度等。。。例如 但我不知道它是否符合我的需要 起初,我想使用Sonata Block,因为我的地图与某些服务有依赖关系。这很酷,我可以集中他们 但是我可以把一些参数传递给调用我的块的父函数吗 谢谢你的回答 Redfog好的,如果我理解了您的问题,那么您要做的是将模板中的一些自定义参数(精确地说,您在模板中调用了块)传递给执行该块的php类。让我们开始: 允许添加选项以传递高度属性: {% s

我是新的奏鸣曲组合。 我想在我的街区放一张地图。它使用了一些JS库。功能的上下文,我需要通过不同的高度,宽度等。。。例如 但我不知道它是否符合我的需要

起初,我想使用Sonata Block,因为我的地图与某些服务有依赖关系。这很酷,我可以集中他们

但是我可以把一些参数传递给调用我的块的父函数吗

谢谢你的回答


Redfog

好的,如果我理解了您的问题,那么您要做的是将模板中的一些自定义参数(精确地说,您在模板中调用了块)传递给执行该块的php类。让我们开始:

允许添加选项以传递高度属性:

{% sample render of your block %}
{{ sonata_block_render({'type':'your.block.id'}, {'height': 50}) }}
现在,在块服务(php/class)中。您必须在方法中添加此属性作为默认选项:setDefaultSettings,如下所示:

public function setDefaultSettings(OptionsResolverInterface $resolver) {
    $resolver->setDefaults(array(
        // your options goes here, and we add our new option right after them
        'height' => null // or whatever suits your needs
    ));
}
public function execute(BlockContextInterface $blockContext, Response $response = null) {
    $settings = $blockContext->getSettings();
    // now your value can be access from $settings['height'];
}
最后,您只需从execute方法访问您的选项,如下所示:

public function setDefaultSettings(OptionsResolverInterface $resolver) {
    $resolver->setDefaults(array(
        // your options goes here, and we add our new option right after them
        'height' => null // or whatever suits your needs
    ));
}
public function execute(BlockContextInterface $blockContext, Response $response = null) {
    $settings = $blockContext->getSettings();
    // now your value can be access from $settings['height'];
}
让我知道这是否是您想要的。

$resolver->setDefaults()是我所缺少的。谢谢你清晰完整的回答。