@@ -76,8 +76,24 @@ for instance ``acme_main.block.rss``::
76
76
Create a Block Service
77
77
----------------------
78
78
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
81
97
that knows how to fetch the feed data of an ``RssBlock ``::
82
98
83
99
// src/Acme/MainBundle/Block/RssBlockService.php
@@ -159,8 +175,8 @@ that knows how to fetch the feed data of an ``RssBlock``::
159
175
}
160
176
161
177
// 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 .
164
180
165
181
public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
166
182
{
@@ -173,17 +189,117 @@ that knows how to fetch the feed data of an ``RssBlock``::
173
189
}
174
190
}
175
191
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
+ --------------------------
181
297
182
298
To make the block work, the last step is to define the service. Do not forget
183
299
to tag your service with ``sonata.block `` to make it known to the
184
300
SonataBlockBundle. The first argument is the name of the block this service
185
301
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.
187
303
188
304
.. configuration-block ::
189
305
@@ -221,3 +337,5 @@ templating, in order to be able to render this block.
221
337
))
222
338
->addTag('sonata.block')
223
339
;
340
+
341
+ .. _assetic : http://symfony.com/doc/current/cookbook/assetic/asset_management.html
0 commit comments