Skip to content

Block Class (Block.php)

The Block class is available to extend the functionality of the block. By default when a block is created using the og-cli command (og make:block (optional)Category BlockName) it will extend the Block class and have an init method.

The core class structure must be present, but it does not need to be modified for the block to work.

By default all blocks will have access to the global context and the built in block context that's provided from the Block class.

Below is an example of a class structure and context that's being added to the template file.

<?php
namespace Origins\Blocks\Category\ExampleBlock;

class Block extends \Og\Block
{
    protected function init()
    {
        $context = 'Put this into context!'
        $this->add('custom_context': $context);
    }
}

Now you can access that context in your index.twig file with:

<pre>
    dump(block.data.custom_context)
</pre>

The most important thing to note about the Block class is that the namespace at the top of the file must match the path of the block Origins\Blocks\{{ Category if the block is in a category (folder) }}\{{BlockName}}. If you move a block into a different category(folder) be mindful of updating the namespace.