diff --git a/bundles/block/create_your_own_blocks.rst b/bundles/block/create_your_own_blocks.rst index 5a8b5747..786b701e 100644 --- a/bundles/block/create_your_own_blocks.rst +++ b/bundles/block/create_your_own_blocks.rst @@ -76,8 +76,24 @@ for instance ``acme_main.block.rss``:: Create a Block Service ---------------------- -If the configuration and logic already satisfies your needs, you should use an -already existing block service. For your RSS block, you need a custom service +The block service is responsible for rendering the blocks of a specific type, +as returned by the ``getType`` method. + +A block service contains: + +* The ``load`` method to initialize a block before rendering; +* The ``execute`` method to render the block; +* The ``setDefaultSettings`` to define default settings; +* Cache configuration; +* Form configuration; +* JavaScript and Stylesheet files to be loaded. + +If the code of an existing service class satisfies your needs, you can simply +create a new service (see next section) with that existing class. The block +services provided by the CmfBlockBundle are in the namespace +``Symfony\Cmf\Bundle\BlockBundle\Block``. + +For your RSS block, you need a custom service that knows how to fetch the feed data of an ``RssBlock``:: // src/Acme/MainBundle/Block/RssBlockService.php @@ -159,8 +175,8 @@ that knows how to fetch the feed data of an ``RssBlock``:: } // These methods are required by the sonata block service interface. - // They are not used in the CMF. To edit, create a sonata admin or - // something else that builds a form and collects the data. + // They are not used in the CMF. To edit, create a symfony form or + // a sonata admin. public function buildEditForm(FormMapper $formMapper, BlockInterface $block) { @@ -173,17 +189,117 @@ that knows how to fetch the feed data of an ``RssBlock``:: } } -The execute method simply reads the RSS feed and forwards the items to -a template. See :ref:`bundle-block-execute` for more information on the -``execute`` method of the block service. To not make your page very slow -by fetching the feed on each request, have a look at the -:doc:`cache documentation `. +.. _bundle-block-execute: + +The Execute Method +~~~~~~~~~~~~~~~~~~ + +This method of the block service contains *controller* logic. It is called +by the block framework to render a block. In this example, it checks if the +block is enabled and if so renders RSS items with a template. + +.. note:: + + If you need complex logic to handle a block, it is recommended to move that + logic into a dedicated service and inject that service into the block + service and defer execution in the ``execute`` method, passing along + arguments determined from the block. + +.. tip:: + + When you do a block that will be slow to render, like this example where + we read an RSS feed, you should activate :doc:`block caching `. + +Default Settings +~~~~~~~~~~~~~~~~ + +The method ``setDefaultSettings`` allows your service to provide default +configuration options for a block. Settings can be altered in multiple +places afterwards, cascading as follows: + +* Default settings from the block service; +* If you use a 3rd party bundle you might want to change them in the bundle + configuration for your application see :ref:`bundle-block-configuration`; +* Settings can be altered through template helpers (see example below); +* And settings can also be altered in a block document. Do this only for + settings that are individual to the specific block instance rather than + all blocks of a type. These settings will be stored in the database. + +Example of how settings can be overwritten through a template helper: + +.. configuration-block:: + + .. code-block:: jinja + + {{ sonata_block_render({'name': 'rssBlock'}, { + 'title': 'Symfony2 CMF news', + 'url': 'http://cmf.symfony.com/news.rss' + }) }} + + .. code-block:: html+php + + render(array('name' => 'rssBlock'), array( + 'title' => 'Symfony2 CMF news', + 'url' => 'http://cmf.symfony.com/news.rss', + )) ?> + +The Load Method +~~~~~~~~~~~~~~~ + +The method ``load`` can be used to load additional data into a block. It is +called each time a block is rendered before the ``execute`` method is called. + +Form Configuration +~~~~~~~~~~~~~~~~~~ + +The methods ``buildEditForm`` and ``buildCreateForm`` specify how to build the +the forms for editing using a frontend or backend UI. The method +``validateBlock`` contains the validation configuration. This is not used in +the CMF and it is recommended to instead build forms or Sonata admin classes +that can handle the block documents. + +Cache Configuration +~~~~~~~~~~~~~~~~~~~ + +The method ``getCacheKeys`` contains cache keys to be used for caching the +block. See the section :doc:`block cache ` for more on caching. + +JavaScripts and Stylesheets +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The methods ``getJavaScripts`` and ``getStylesheets`` of the service class +define the JavaScript and Stylesheet files needed by a block. There is a +twig function and a templating helper to render all links for all blocks used +on the current page: + +.. configuration-block:: + + .. code-block:: jinja + + {{ sonata_block_include_javascripts("all") }} + {{ sonata_block_include_stylesheets("all") }} + + .. code-block:: html+php + + includeJavaScripts('all') ?> + includeStylesheets('all') ?> + +.. note:: + + This mechanism is not recommended. For optimal load times, it is better + to have a central assets definition for your project and aggregate them + into a single Stylesheet and a single JavaScript file, e.g. with assetic_, + rather than having individual ```` and ``