1
1
Create your own Blocks
2
2
======================
3
3
4
- .. include :: ../_outdate-caution.rst.inc
5
-
6
4
Follow these steps to create a block:
7
5
8
- * create a block document;
9
- * create a block service and declare it (optional);
10
- * create a data object representing your block in the repository, see
6
+ * define a block document class ;
7
+ * if needed, create a block service and declare it (optional);
8
+ * instantiate a data object representing your block in the repository, see
11
9
:ref: `bundle-block-document `;
12
10
* render the block, see :ref: `bundle-block-rendering `;
13
11
14
12
Lets say you are working on a project where you have to integrate data
15
13
received from several RSS feeds. Of course you could create an ActionBlock
16
- for each of these feeds, but wouldn't this be silly? In fact all those actions
14
+ for each of these feeds, but wouldn't this be silly? In fact, all those actions
17
15
would look similar: Receive data from a feed, sanitize it and pass the data to
18
- a template. So instead you decide to create your own block, the RSSBlock.
16
+ a template. So instead you decide to create your own block, the ``RssBlock ``.
17
+
18
+ .. tip ::
19
+
20
+ In this example, we create an ``RssBlock ``. This particular block type
21
+ already exists in the Symfony2 CMF BlockBundle, so you can also look at
22
+ the end result if you want.
19
23
20
24
Create a block document
21
25
-----------------------
22
26
23
- The first thing you need is an document that contains the data. It is
24
- recommended to extend ``Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\AbstractBlock ``
25
- contained in this bundle (however you are not forced to do so, as long as you
26
- implement ``Sonata\BlockBundle\Model\BlockInterface ``). In your document, you
27
- need to define the ``getType `` method which just returns ``acme_main.block.rss ``.
27
+ The first thing you need is an document that holds the data. It is
28
+ recommended to extend ``Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\AbstractBlock ``.
29
+ You are of course free to do your own document, but need to at least implement
30
+ ``Sonata\BlockBundle\Model\BlockInterface ``. In your document, you
31
+ need to define the ``getType `` method which returns the type name of your block,
32
+ for instance ``acme_main.block.rss ``.
28
33
29
34
.. code-block :: php
30
35
@@ -51,33 +56,56 @@ Create a Block Service
51
56
----------------------
52
57
53
58
You could choose to use a an already existing block service because the
54
- configuration and logic already satisfy your needs. For our rss block we
55
- create a service that knows how to handle RSSBlocks :
59
+ configuration and logic already satisfy your needs. For our RSS block we
60
+ create a service that knows how to handle `` RssBlocks `` :
56
61
57
62
* The method ``setDefaultSettings `` configures a template, title, url and the
58
63
maximum amount of items::
59
64
60
- // ...
61
- public function setDefaultSettings(OptionsResolverInterface $resolver)
62
- {
63
- $resolver->setDefaults(array(
64
- 'template' => 'AcmeMainBundle::Block::block_rss.html.twig',
65
- 'url' => false,
66
- 'title' => 'Insert the rss title',
67
- 'maxItems' => 10,
68
- ));
69
- }
70
- // ...
65
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
66
+ use Symfony\Component\HttpFoundation\Response;
67
+ use Sonata\BlockBundle\Block\BaseBlockService;
68
+ use Sonata\BlockBundle\Block\BlockContextInterface;
69
+
70
+ class RssBlockService extends extends BaseBlockService
71
+ {
72
+ // ...
73
+ public function setDefaultSettings(OptionsResolverInterface $resolver)
74
+ {
75
+ $resolver->setDefaults(array(
76
+ 'template' => 'AcmeMainBundle::Block::block_rss.html.twig',
77
+ 'url' => false,
78
+ 'title' => 'Feed items',
79
+ 'maxItems' => 10,
80
+ ));
81
+ }
82
+ // ...
71
83
72
- * The execute method passes the settings to an rss reader service and forwards
73
- * The feed items to a template, see :ref: `bundle-block-execute `
84
+ public function execute(BlockContextInterface $blockContext, Response $response = null)
85
+ {
86
+ /** @var $block RssBlock */
87
+ $block = $blockContext->getBlock();
88
+
89
+ if (!$block->getEnabled()) {
90
+ return new Response;
91
+ }
92
+
93
+ $requestParams = $block->resolveRequestParams($this->request, $blockContext);
94
+
95
+ return new Response($this->renderer->render(new ControllerReference(
96
+ 'acme_main.controller.rss:block',
97
+ $requestParams
98
+ )
99
+ ));
100
+ }
101
+ }
74
102
75
- Make sure you implement the interface
76
- `` Sonata\BlockBundle\Block\BlockServiceInterface `` or an existing block
77
- service like `` Sonata\BlockBundle\Block\BaseBlockService `` .
103
+ The execute method passes the settings to an RSS reader service and forwards
104
+ the feed items to a template. (See :ref: ` bundle-block-execute ` for more on the
105
+ block service `` execute `` method) .
78
106
79
- Define the service in a config file. It is important to tag your BlockService
80
- with ``sonata.block ``, otherwise it will not be known by the Bundle .
107
+ Define the service in a configuration file. It is important to tag your BlockService
108
+ with ``sonata.block `` to make it known to the SonataBlockBundle .
81
109
82
110
.. configuration-block ::
83
111
@@ -116,13 +144,15 @@ with ``sonata.block``, otherwise it will not be known by the Bundle.
116
144
117
145
$container->register('acme_main.rss_reader', 'Acme\MainBundle\Feed\SimpleReader');
118
146
119
- $container->addDefinition('sandbox_main.block.rss', new Definition(
120
- 'Acme\MainBundle\Block\RssBlockService',
121
- array(
122
- 'acme_main.block.rss',
123
- new Reference('templating'),
124
- new Reference('sonata.block.renderer'),
125
- new Reference('acme_main.rss_reader'),
126
- )
127
- ))
128
- ->addTag('sonata.block');
147
+ $container
148
+ ->addDefinition('sandbox_main.block.rss', new Definition(
149
+ 'Acme\MainBundle\Block\RssBlockService',
150
+ array(
151
+ 'acme_main.block.rss',
152
+ new Reference('templating'),
153
+ new Reference('sonata.block.renderer'),
154
+ new Reference('acme_main.rss_reader'),
155
+ )
156
+ ))
157
+ ->addTag('sonata.block')
158
+ ;
0 commit comments