Skip to content

Instantly share code, notes, and snippets.

@sunpietro
Last active July 21, 2016 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunpietro/23d765ab75a4b0741b8cf2d8dcdcac4e to your computer and use it in GitHub Desktop.
Save sunpietro/23d765ab75a4b0741b8cf2d8dcdcac4e to your computer and use it in GitHub Desktop.
Extending eZ Studio with new blocks
blocks:
custom:
views:
custom:
template: MyBlockBundle::custom.html.twig
name: My Custom Block view
<h3>My Custom block</h3>
{{ block }}
<?php
namespace My\BlockBundle\Block;
use EzSystems\LandingPageFieldTypeBundle\Exception\InvalidBlockAttributeException;
use EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\Definition\BlockDefinition;
use EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\Definition\BlockAttributeDefinition;
use EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\Model\AbstractBlockType;
use EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\Model\BlockValue;
class CustomBlock extends AbstractBlockType
{
/*
* With this method you can return values that will be used in the template as variables.
*/
public function getTemplateParameters(BlockValue $blockValue)
{
return [
'block' => json_encode($blockValue->getAttributes())
];
}
/*
* Creates block definition with fields
*/
public function createBlockDefinition()
{
return new BlockDefinition(
'custom', // Block type (unique)
'Custom Block', // Name of block
'default', // Block category (currently unsupported)
'bundles/ezsystemslandingpagefieldtype/images/thumbnails/tag.svg', // block thumbnail; will be used in the eZ Studio blocks sidebar
[], // extra views - the hardcoded template
[
new BlockAttributeDefinition(
'content', // Attribute's ID (unique)
'Content', // Attribute' name
'text', // Attribute's type
'/[^\\s]/', // regex for frontend validation
'The content value should be a text', // regex validation fail message
true, // is field required?
false, // should this attribute input be displayed inline to the previous?
[], // default value
[] // available options (only for select and multiple field types)
),
new BlockAttributeDefinition(
'contentStyle',
'Content Style',
'select',
'',
'Please, select a style',
false,
false,
['default'],
[
'default' => 'Default style',
'flat' => 'Flat style',
'rounded' => 'Rounded style'
]
),
]
);
}
/*
* Validates user input from the block configuration form
*/
public function checkAttributesStructure(array $attributes)
{
if (!isset($attributes['content'])) {
throw new InvalidBlockAttributeException(
$this->getBlockDefinition()->getName(),
'content',
'Content must be set.'
);
}
}
}
<?php
namespace My\BlockBundle\DependencyInjection;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Yaml\Yaml;
class CustomBlockExtension extends Extension implements PrependExtensionInterface
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
public function prepend(ContainerBuilder $container)
{
$configFile = __DIR__ . '/../Resources/config/blocks.yml';
$config = Yaml::parse(file_get_contents($configFile));
$container->prependExtensionConfig('ez_systems_landing_page_field_type', $config);
$container->addResource(new FileResource($configFile));
}
}
services:
my.block.custom:
class: My\BlockBundle\Block\CustomBlock
tags:
- { name: landing_page_field_type.block_type, alias: custom }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment