Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit bf9aeae

Browse files
committed
continue block bundle doc cleanup
1 parent 2523a22 commit bf9aeae

File tree

5 files changed

+213
-184
lines changed

5 files changed

+213
-184
lines changed

bundles/block/create_your_own_blocks.rst

Lines changed: 72 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
Create your own Blocks
22
======================
33

4-
.. include:: ../_outdate-caution.rst.inc
5-
64
Follow these steps to create a block:
75

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
119
:ref:`bundle-block-document`;
1210
* render the block, see :ref:`bundle-block-rendering`;
1311

1412
Lets say you are working on a project where you have to integrate data
1513
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
1715
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.
1923

2024
Create a block document
2125
-----------------------
2226

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``.
2833

2934
.. code-block:: php
3035
@@ -51,33 +56,56 @@ Create a Block Service
5156
----------------------
5257

5358
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``:
5661

5762
* The method ``setDefaultSettings`` configures a template, title, url and the
5863
maximum amount of items::
5964

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+
// ...
7183

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+
}
74102

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).
78106

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.
81109

82110
.. configuration-block::
83111

@@ -116,13 +144,15 @@ with ``sonata.block``, otherwise it will not be known by the Bundle.
116144
117145
$container->register('acme_main.rss_reader', 'Acme\MainBundle\Feed\SimpleReader');
118146
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

Comments
 (0)