Message-ID: <1146622059.2718.1485850502468.JavaMail.confluence@ip-10-127-227-164> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_2717_2108331928.1485850502468" ------=_Part_2717_2108331928.1485850502468 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html Creating Landing Page blocks (Enterprise)

Creating Landing Page blocks (Enterprise)

=20
=20
=20
=20

Description

V1.2=

A Landing Page has a customizable layout with multiple zones where you can place pr= edefined blocks with content.

By default eZ Enterprise comes with a number of preset Landing Page bloc= ks. You can, however, add custom blocks to your configuration.

Solution

Block c= onfiguration

In the Demo installation the layout configuration is stored in ezs= tudio-demo-bundle/Resources/config/default_layouts.yml:

Example default_layouts.yml
=20
blocks:
    gallery:
        views:
            gallery:
                template: eZStudioDemoBundle:blocks:gallery.html.twig
                name: Default Gallery Block template
    keyword:
        views:
            keyword:
                template: eZStudioDemoBundle:blocks:keyword.html.twig
                name: Default Keyword Block template
    rss:
        views:
            rss:
                template: eZStudioDemoBundle:blocks:rss.html.twig
                name: Default RSS Block template
    tag:
        views:
            tag:
                template: eZStudioDemoBundle:blocks:tag.html.twig
                name: Default Tag Block template
=20

 

Creating= a new block

= Creating a class for the block

The class for the block must implement the BlockType interf= ace:

=20
EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\Model\Bl=
ockType
=20

Most methods are implemented in a universal way by using the AbstractBlockType abstract class:

=20
EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\Model\Ab=
stractBlockType 
=20

If your block does not have specific attributes or a structure, you can = extend the AbstractBlockType class, which = contains simple generic converters designated for the block attributes.

For example:

=20
<?php
namespace AcmeDemoBundle\Block;

use EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\Model\Abstra=
ctBlockType;

/**
* RSS block
* Renders feed from a given URL.
*/
class RSSBlock extends AbstractBlockType
{
   // Class body
}
=20


Describing a class definition

A block must have a definition set using two classes:

B= lockAttributeDefinition

The BlockAttributeDefinition class defines the attributes= of a block:

Attribute Type Definition
$id string block attribute ID
$name string block attribute name
$type string block attribute type, available options are:
  • integer
  • string
  • url
  • text
  • embed
  • select
  • multiple
$regex string block attribute regex used for validation
$regexErrorMessage string message displayed when regex does not match
$required bool TRUE if attribute is required<= /td>
$inline bool indicates whether block attribute input sho= uld be rendered inline in a form
$values array array of chosen values
$options array array of available options


BlockDefin= ition

The BlockDefinition class describes a block:

Attribute Type Definition Note

$type

string block type  
$name string block name  
$category string block category  
$thumbnail string path to block thumbnail image  
$templates array array of available paths of templates

Retrieved from the config file = (default_layouts.yml)

$attributes array array of block attributes (objects of  BlockAttributeDefinition  class)  


 

When extending AbstractBlockType you must = implement at least 3 methods:

createBlockDefinition()

This method must return an  EzSystems\LandingPageFiel= dTypeBundle\FieldType\LandingPage\Definition\BlockDefinition  object.

Example of a Gallery block:

=20
/**
 * Creates BlockDefinition object for block type.
 *
 * @return \EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\Defi=
nition\BlockDefinition
 */
public function createBlockDefinition()
{
    return new BlockDefinition(
        'gallery',
        'Gallery Block',
        'default',
        'bundles/ezsystemslandingpagefieldtype/images/thumbnails/gallery.sv=
g',
        [],=20
        [
            new BlockAttributeDefinition(
                'contentId',
                'Folder',
                'embed',
                '/^([a-zA-Z]:)?(\/[a-zA-Z0-9_\/-]+)+\/?/',
                'Choose an image folder'
            ),
        ]
    );
}
=20
getTemplateParameters(BlockValue $block= Value)

This method returns an array of parameters to be displayed in rendered v= iew of block. You can access them directly in a block template (e. g.= via twig {{ title }} ).

 

When parameters are used in the template you call them directly without = the parameters array name:

Correct Not Correct
&= lt;h1>{{ title }}</h1> = <h1>{{ parameters.title }}</h1>

 

Example of the getTemplateParameters() method implementatio= n:

=20
/**
* @param \EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\Model\=
BlockValue $blockValue
*
* @return array
*/
public function getTemplateParameters(BlockValue $blockValue)
{
=09$attributes =3D $blockValue->getAttributes();
=09$limit =3D (isset($attributes['limit'])) ? $attributes['limit'] : 10;
=09$offset =3D (isset($attributes['offset'])) ? $attributes['offset'] : 0;
=09$parameters =3D [
=09=09'title' =3D> $attributes['title'],
=09=09'limit' =3D> $limit,
=09=09'offset' =3D> $offset,
=09=09'feeds' =3D> $this->RssProvider->getFeeds($attributes['url']=
),
=09];
=09
=09return $parameters;
}
=20

 

checkAttributesStructure(array $attribu= tes)

This method validates the input fields for a block. You can specify your= own conditions to throw the InvalidBlockAttributeException ex= ception.

This InvalidBlockAttributeException exception has the follo= wing parameters:

Name Description
<= strong>blockType na= me of a block
<= span class=3D"pl-s1"> attribute <= span class=3D"pl-s1"> name of the block's attribute = which failed validation
<= span class=3D"pl-s1"> message = <= span class=3D"pl-s1"> a short information about an error =
<= span class=3D"pl-s1"> pr= evious <= span class=3D"pl-s1"> previous ex= ception, null by default

 

For example:

=20
/**
 * Checks if block's attributes are valid.
 *
 * @param array $attributes
 *
 * @throws \EzSystems\LandingPageFieldTypeBundle\Exception\InvalidBlockAttr=
ibuteException
 */
public function checkAttributesStructure(array $attributes)
{
    if (!isset($attributes['url'])) {
        throw new InvalidBlockAttributeException('RSS', 'url', 'URL must be=
 set.');
    }

    if (isset($attributes['limit']) && (($attributes['limit'] < =
1) || (!is_numeric($attributes['limit'])))) {
        throw new InvalidBlockAttributeException('RSS', 'limit', 'Limit mus=
t be a number greater than 0.');
    }

    if (isset($attributes['offset']) && (($attributes['offset'] <=
; 0) || (!is_numeric($attributes['limit'])))) {
        throw new InvalidBlockAttributeException('RSS', 'offset', 'Offset m=
ust be a number no less than 0.');
    }
}
=20

When the class is created make sure it is added to a container.

 

Adding the class to the container

 The services.yml file must contain info about you= r block class.

The description of your class must contain a tag which provides:

  • tag name: landing_page_field_type.block_type
  • tag alias: <name of a block>

 

For example:

=20
acme.landing_page.block.rss: =09=09=09=09=09=09=09=09=09=09=09# ser=
vice id
       class: AcmeDemoBundle\FieldType\LandingPage\Model\Block\RSSBlock # b=
lock's class with namespace
       tags: =09=09=09=09=09=09=09=09=09=09=09=09=09=09=09# service definit=
ion must contain tag with=20
           - { name: landing_page_field_type.block_type, alias: rss}=09# "l=
anding_page_field_type.block_type" name and block name as an alias
=20

Custom edi= ting UI

If you want to add a custom editing UI to your new block, you need to pr= ovide the code for the custom popup UI in Javascript (see the code for eZS.ScheduleBlockView or= eZS.TagBlockView for exa= mples).

Once it is ready, create a plugin for eZS.LandingPageCreatorView that makes a use of the addBlock public method from eZS.LandingPageCreatorView, see the example below:

=20
YUI.add('ezs-addcustomblockplugin', function (Y) {
    'use strict';

    var namespace =3D 'Any.Namespace.Of.Your.Choice',

    Y.namespace(namespace);
    NS =3D Y[namespace];

    NS.Plugin.AddCustomBlock =3D Y.Base.create('addCustomBlockPlugin', Y.Pl=
ugin.Base, [], {
        initializer: function () {
            this.get('host').addBlock('custom', NS.CustomBlockView);
        },
    }, {
        NS: 'dashboardPlugin'
    });

    Y.eZ.PluginRegistry.registerPlugin(
        NS.Plugin.AddCustomBlock, ['landingPageCreatorView']
    );
});
=20

Upcoming feature - multiple block templates

The ability to configure different templates (views) for one Landing Pag= e block is upcoming. See EZS-1008 to follow its progress.<= /p>

 

Example

Block Class
TagBlock.php
=20
<?php
/**
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
 * @license For full copyright and license information view LICENSE file di=
stributed with this source code.
 */
namespace EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\Model\=
Block;

use EzSystems\LandingPageFieldTypeBundle\Exception\InvalidBlockAttributeExc=
eption;
use EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\Definition\B=
lockDefinition;
use EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\Definition\B=
lockAttributeDefinition;
use EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\Model\Abstra=
ctBlockType;
use EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\Model\BlockT=
ype;
use EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\Model\BlockV=
alue;

/**
 * Tag block
 * Renders simple HTML.
 */
class TagBlock extends AbstractBlockType implements BlockType
{
    /**
     * Returns array of parameters required to render block template.
     *
     * @param array $blockValue Block value attributes
     *
     * @return array Template parameters
     */
    public function getTemplateParameters(BlockValue $blockValue)
    {
        return ['block' =3D> $blockValue];
    }

=09/**
 =09 * Creates BlockDefinition object for block type.
 =09 *
 =09 * @return \EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\=
Definition\BlockDefinition
 =09 */
    public function createBlockDefinition()
    {
        return new BlockDefinition(
            'tag',
            'Tag Block',
            'default',
            'bundles/ezsystemslandingpagefieldtype/images/thumbnails/tag.sv=
g',
            [],
            [
                new BlockAttributeDefinition(
                    'content',
                    'Content',
                    'text',
                    '/[^\\s]/',
                    'Provide html code'
                ),
            ]
        );
    }

=09/**
 =09 * Checks if block's attributes are valid.
=09 *
=09 * @param array $attributes
=09 *
=09 * @throws \EzSystems\LandingPageFieldTypeBundle\Exception\InvalidBlockA=
ttributeException
=09 */
    public function checkAttributesStructure(array $attributes)
    {
        if (!isset($attributes['content'])) {
            throw new InvalidBlockAttributeException('Tag', 'content', 'Con=
tent must be set.');
        }
    }
}

=20

V1.7=

If you want to make sure that your block is only available in the Elemen= t menu in a specific situation, you can override the isAvailable= method, which makes the block accessible by default:

=20
public function isAvailable()
    {
        return true;
    }
=20

 

s= ervice.yml configuration

services.yml
=20
ezpublish.landing_page.block.tag:
    class: EzSystems\LandingPageFieldTypeBundle\FieldType\LandingPage\Model=
\Block\TagBlock
    tags:
        - { name: landing_page_field_type.block_type, alias: tag }
=20

Block = template

{{ block.attributes.content|raw }}

 

=20
=20
=20
=20

In this topic= :

 

Related top= ics:

Creating Landing Pa= ge layouts (Enterprise)

Landing Page Field = Type (Enterprise)

=20
=20
=20
------=_Part_2717_2108331928.1485850502468 Content-Type: image/png Content-Transfer-Encoding: base64 Content-Location: file:///C:/af918576c0365dadd34b26056af46801 iVBORw0KGgoAAAANSUhEUgAAABAAAAAQBAMAAADt3eJSAAAAA3NCSVQICAjb4U/gAAAAFVBMVEX/ //9wcHBwcHBwcHBwcHBwcHBwcHA3RenHAAAAB3RSTlMAZoiZzN3/SzZomQAAAAlwSFlzAAALEgAA CxIB0t1+/AAAABx0RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzIENTNui8sowAAAAUdEVYdENy ZWF0aW9uIFRpbWUANi8xLzEzOKlF0AAAACFJREFUCJljYCATsCgwqIAZTMnMyRAhsTABCIMxkVxT GQCLcwHyUKXpLgAAAABJRU5ErkJggg== ------=_Part_2717_2108331928.1485850502468--