Druplash tutorial

Dynamic blocks with Druplash

If you went through the tutorial, you should know how to add content in the main section of the Drupal's theme. Of course, there might be times where you will need to display some dynamic content in the top section, in the left or right column, or in the footer. To do this in a pure Drupal style, you would use blocks, with some PHP code in the blocks. Druplash does not remove the notion of blocks that is at the core of Drupal. Instead, Druplash binds Mouf instances with Drupal block. Let's see how this works.

The DrupalDynamicBlock class

When you want to create a block in Drupal that will contain dynamic content generated by your controller, you can create a "DrupalDynamicBlock" instance in Mouf.

Go to Menu > Instances > Create a new instance. Select the "DrupalDynamicBlock" and name it 'myDynamicBlock'. As you can see, a dynamic block contains a number of parameters that are all explained in the page. The parameters you should care about are: - name: the name of the Druplash block, as displayed in the Drupal block administration screen - subject: the title of the block (it will be displayed each time the block is displayed) - content: you can put here any instance extending the HtmlElementInterface class (an element extending the HtmlElementInterface can be displayed in HTML). However, there are easier ways to use the block as we will see.

Once your block is created, you can see it in Drupal's block administration interface. Go in this interface, and place the block where you want it to be (left column, header, footer, etc...)

Now, we need to write into the block. We will do this from the controller:

namespace Test;

use Mouf\Html\Template\TemplateInterface;
use Mouf\Mvc\Splash\Controllers\Controller;
use Mouf\Integration\Drupal\Druplash\DrupalDynamicBlockInterface;

class MyController extends Controller {

     * @var TemplateInterface
    public $template;

     * @Property
     * @var DrupalDynamicBlockInterface
    public $myDynamicBlock;

     * @Action
     * @URL hello/world
    public function index() {
        $this->myDynamicBlock->addContentText('Hello world!');

In this example, please notice the we add a property to the controller named "myDynamicBlock". This property will have to be bound to the DrupalDynamicBlock we previously created (go to your controller's instance, and select your block in the dropdown for the "myDynamicBlock" property).

As you can see, writing in a block as easy, you just need to use the appropriate function. As usual in SplashMVC, there are 4 different functions to write in the block:

  • addText: to directly write a text into the block
  • addFunction: will call a function. The output of the function will be written into the block
  • addFile: will include a file. The content of the file will be outputed in the block
  • addHtmlElement: will add an object implementing the HtmlElementInterface. The "toHtml()" method of the object will be called and displayed in the block
Druplash tutorial

Found a typo? Something is wrong in this documentation? Just fork and edit it!