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

Commit 85cc402

Browse files
committed
sort out block introduction and custom block tutorial
1 parent 89f6f12 commit 85cc402

File tree

2 files changed

+134
-134
lines changed

2 files changed

+134
-134
lines changed

bundles/block/create_your_own_blocks.rst

Lines changed: 128 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,24 @@ for instance ``acme_main.block.rss``::
7676
Create a Block Service
7777
----------------------
7878

79-
If the configuration and logic already satisfies your needs, you should use an
80-
already existing block service. For your RSS block, you need a custom service
79+
The block service is responsible for rendering the blocks of a specific type,
80+
as returned by the ``getType`` method.
81+
82+
A block service contains:
83+
84+
* The ``load`` method to initialize a block before rendering;
85+
* The ``execute`` method to render the block;
86+
* The ``setDefaultSettings`` to define default settings;
87+
* Cache configuration;
88+
* Form configuration;
89+
* JavaScript and Stylesheet files to be loaded.
90+
91+
If the code of an existing service class satisfies your needs, you can simply
92+
create a new service (see next section) with that existing class. The block
93+
services provided by the CmfBlockBundle are in the namespace
94+
``Symfony\Cmf\Bundle\BlockBundle\Block``.
95+
96+
For your RSS block, you need a custom service
8197
that knows how to fetch the feed data of an ``RssBlock``::
8298

8399
// src/Acme/MainBundle/Block/RssBlockService.php
@@ -159,8 +175,8 @@ that knows how to fetch the feed data of an ``RssBlock``::
159175
}
160176

161177
// These methods are required by the sonata block service interface.
162-
// They are not used in the CMF. To edit, create a sonata admin or
163-
// something else that builds a form and collects the data.
178+
// They are not used in the CMF. To edit, create a symfony form or
179+
// a sonata admin.
164180

165181
public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
166182
{
@@ -173,17 +189,117 @@ that knows how to fetch the feed data of an ``RssBlock``::
173189
}
174190
}
175191

176-
The execute method simply reads the RSS feed and forwards the items to
177-
a template. See :ref:`bundle-block-execute` for more information on the
178-
``execute`` method of the block service. To not make your page very slow
179-
by fetching the feed on each request, have a look at the
180-
:doc:`cache documentation <cache>`.
192+
.. _bundle-block-execute:
193+
194+
The Execute Method
195+
~~~~~~~~~~~~~~~~~~
196+
197+
This method of the block service contains *controller* logic. It is called
198+
by the block framework to render a block. In this example, it checks if the
199+
block is enabled and if so renders RSS items with a template.
200+
201+
.. note::
202+
203+
If you need complex logic to handle a block, it is recommended to move that
204+
logic into a dedicated service and inject that service into the block
205+
service and defer execution in the ``execute`` method, passing along
206+
arguments determined from the block.
207+
208+
.. tip::
209+
210+
When you do a block that will be slow to render, like this example where
211+
we read an RSS feed, you should activate :doc:`block caching <cache>`.
212+
213+
Default Settings
214+
~~~~~~~~~~~~~~~~
215+
216+
The method ``setDefaultSettings`` allows your service to provide default
217+
configuration options for a block. Settings can be altered in multiple
218+
places afterwards, cascading as follows:
219+
220+
* Default settings from the block service;
221+
* If you use a 3rd party bundle you might want to change them in the bundle
222+
configuration for your application see :ref:`bundle-block-configuration`;
223+
* Settings can be altered through template helpers (see example below);
224+
* And settings can also be altered in a block document. Do this only for
225+
settings that are individual to the specific block instance rather than
226+
all blocks of a type. These settings will be stored in the database.
227+
228+
Example of how settings can be overwritten through a template helper:
229+
230+
.. configuration-block::
231+
232+
.. code-block:: jinja
233+
234+
{{ sonata_block_render({'name': 'rssBlock'}, {
235+
'title': 'Symfony2 CMF news',
236+
'url': 'http://cmf.symfony.com/news.rss'
237+
}) }}
238+
239+
.. code-block:: html+php
240+
241+
<?php $view['blocks']->render(array('name' => 'rssBlock'), array(
242+
'title' => 'Symfony2 CMF news',
243+
'url' => 'http://cmf.symfony.com/news.rss',
244+
)) ?>
245+
246+
The Load Method
247+
~~~~~~~~~~~~~~~
248+
249+
The method ``load`` can be used to load additional data into a block. It is
250+
called each time a block is rendered before the ``execute`` method is called.
251+
252+
Form Configuration
253+
~~~~~~~~~~~~~~~~~~
254+
255+
The methods ``buildEditForm`` and ``buildCreateForm`` specify how to build the
256+
the forms for editing using a frontend or backend UI. The method
257+
``validateBlock`` contains the validation configuration. This is not used in
258+
the CMF and it is recommended to instead build forms or Sonata admin classes
259+
that can handle the block documents.
260+
261+
Cache Configuration
262+
~~~~~~~~~~~~~~~~~~~
263+
264+
The method ``getCacheKeys`` contains cache keys to be used for caching the
265+
block. See the section :doc:`block cache <cache>` for more on caching.
266+
267+
JavaScripts and Stylesheets
268+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
269+
270+
The methods ``getJavaScripts`` and ``getStylesheets`` of the service class
271+
define the JavaScript and Stylesheet files needed by a block. There is a
272+
twig function and a templating helper to render all links for all blocks used
273+
on the current page:
274+
275+
.. configuration-block::
276+
277+
.. code-block:: jinja
278+
279+
{{ sonata_block_include_javascripts("all") }}
280+
{{ sonata_block_include_stylesheets("all") }}
281+
282+
.. code-block:: html+php
283+
284+
<?php $view['blocks']->includeJavaScripts('all') ?>
285+
<?php $view['blocks']->includeStylesheets('all') ?>
286+
287+
.. note::
288+
289+
This mechanism is not recommended. For optimal load times, it is better
290+
to have a central assets definition for your project and aggregate them
291+
into a single Stylesheet and a single JavaScript file, e.g. with assetic_,
292+
rather than having individual ``<link>`` and ``<script>`` tags for each
293+
single file.
294+
295+
Register the Block Service
296+
--------------------------
181297

182298
To make the block work, the last step is to define the service. Do not forget
183299
to tag your service with ``sonata.block`` to make it known to the
184300
SonataBlockBundle. The first argument is the name of the block this service
185301
handles, as per the ``getType`` method of the block. The second argument is the
186-
templating, in order to be able to render this block.
302+
``templating`` service, in order to be able to render this block.
187303

188304
.. configuration-block::
189305

@@ -221,3 +337,5 @@ templating, in order to be able to render this block.
221337
))
222338
->addTag('sonata.block')
223339
;
340+
341+
.. _assetic: http://symfony.com/doc/current/cookbook/assetic/asset_management.html

bundles/block/introduction.rst

Lines changed: 6 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -199,131 +199,13 @@ helper. Therefore, use the BlockContext to get or alter a setting if needed.
199199
Block Service
200200
-------------
201201

202-
If you look inside the ``SimpleBlock`` class, you will notice the method
203-
``getType``. This defines the name of the block service that processes the
204-
block when it is rendered.
202+
Internally, the block bundle uses a block service to work with each type
203+
of block. The service is configured to handle a *type* of block. The
204+
blocks themselves identify their type in the ``getType`` method.
205205

206-
A block service contains:
207-
208-
* An execute method;
209-
* Default settings;
210-
* Dorm configuration;
211-
* Cache configuration;
212-
* JavaScript files and stylesheet assets to be loaded;
213-
* A load method.
214-
215-
The block services provided by the Symfony2 CMF BlockBundle are in the
216-
namespace ``Symfony\Cmf\Bundle\BlockBundle\Block``.
217-
218-
.. note::
219-
220-
Always make sure you implement the interface
221-
``Sonata\BlockBundle\Block\BlockServiceInterface`` or extend a block
222-
service like ``Sonata\BlockBundle\Block\BaseBlockService``.
223-
224-
.. _bundle-block-execute:
225-
226-
The Execute Method
227-
~~~~~~~~~~~~~~~~~~
228-
229-
This method of a block service contains *controller* logic::
230-
231-
// ...
232-
if ($block->getEnabled()) {
233-
$feed = false;
234-
if ($blockContext->getSetting('url', false)) {
235-
$feed = $this->feedReader->import($block);
236-
}
237-
238-
return $this->renderResponse($blockContext->getTemplate(), array(
239-
'feed' => $feed,
240-
'block' => $blockContext->getBlock(),
241-
'settings' => $blockContext->getSettings(),
242-
), $response);
243-
}
244-
// ...
245-
246-
.. note::
247-
248-
If you need complex logic to handle a block, it is recommended to move that
249-
logic into a dedicated service and inject that service into the block
250-
service and call it in the ``execute`` method.
251-
252-
Default Settings
253-
~~~~~~~~~~~~~~~~
254-
255-
The method ``setDefaultSettings`` specifies the default settings for a block.
256-
Settings can be altered in multiple places afterwards, cascading as follows:
257-
258-
* Default settings are stored in the block service;
259-
* If you use a 3rd party bundle you might want to change them in the bundle
260-
configuration for your application see :ref:`bundle-block-configuration`;
261-
* Settings can be altered through template helpers (see example);
262-
* And settings can also be altered in a block document, the advantage is that
263-
settings are stored in the database and are individual to the specific block
264-
instead of all blocks of a type.
265-
266-
Example of how settings can be specified through a template helper:
267-
268-
.. configuration-block::
269-
270-
.. code-block:: jinja
271-
272-
{{ sonata_block_render({'name': 'rssBlock'}, {
273-
'title': 'Symfony2 CMF news',
274-
'url': 'http://cmf.symfony.com/news.rss'
275-
}) }}
276-
277-
.. code-block:: html+php
278-
279-
<?php $view['blocks']->render(array('name' => 'rssBlock'), array(
280-
'title' => 'Symfony2 CMF news',
281-
'url' => 'http://cmf.symfony.com/news.rss',
282-
)) ?>
283-
284-
Form Configuration
285-
~~~~~~~~~~~~~~~~~~
286-
287-
The methods ``buildEditForm`` and ``buildCreateForm`` specify how to build the
288-
the forms for editing using a frontend or backend UI. The method
289-
``validateBlock`` contains the validation configuration.
290-
291-
Cache Configuration
292-
~~~~~~~~~~~~~~~~~~~
293-
294-
The method ``getCacheKeys`` contains cache keys to be used for caching the
295-
block.
296-
297-
JavaScript and Stylesheets
298-
~~~~~~~~~~~~~~~~~~~~~~~~~~
299-
300-
The methods ``getJavaScripts`` and ``getStylesheets`` can be used to define
301-
JavaScript and stylesheet assets needed by a block. Use the twig helpers
302-
``sonata_block_include_javascripts`` and ``sonata_block_include_stylesheets``
303-
to render them:
304-
305-
.. configuration-block::
306-
307-
.. code-block:: jinja
308-
309-
{{ sonata_block_include_javascripts() }}
310-
{{ sonata_block_include_stylesheets() }}
311-
312-
.. code-block:: html+php
313-
314-
<?php $view['blocks']->includeJavaScripts() ?>
315-
<?php $view['blocks']->includeStylesheets() ?>
316-
317-
.. note::
318-
319-
This will output the JavaScript files and stylesheets for all blocks loaded in
320-
the service container of your application.
321-
322-
The Load Method
323-
~~~~~~~~~~~~~~~
324-
325-
The method ``load`` can be used to load additional data. It is called each
326-
time a block is rendered before the ``execute`` method is called.
206+
When using the provided blocks, you do not need to worry about the block
207+
service. It is only relevant when
208+
:doc:`creating your own blocks <create_your_own_blocks>`.
327209

328210
.. _bundle-block-rendering:
329211

0 commit comments

Comments
 (0)