Path

7x / documentation / ez publish / technical manual / 4.x / reference / template operators / miscellaneous / attribute / how to create a custom outp...


Caution: This documentation is for eZ Publish legacy, from version 3.x to 6.x.
For 5.x documentation covering Platform see eZ Documentation Center, for difference between legacy and Platform see 5.x Architecture overview.

How to create a custom output formatter for the template operator attribute

eZ Publish 4.6 allows to create a custom output formatter for the attribute() template operator. This document explains process how to create a custom output formatter.

Create a custom extension folder

First we need to create a custom extension folder where we will place our code and configuration settings. Create a "ezattributeformatter" folder under extension folder inside eZ Publish root folder. You should have a following directory structure:
extension
├── ezattributeformatter
│   │
│   ├── classes
     └── settings

Create php file

Next inside settings folder create a "template.ini.append.php" file with the following settings:

<?php /*
 
[AttributeOperator]
OutputFormatter[custom]=ezpAttributeOperatorCustomFormatter
 
*/ ?>

Key 'custom' for "OutputFormatter" is our custom attribute() template operator identifier. Value is a PHP class which has to implement "ezpAttributeOperatorFormatterInterface".

Implement PHP class

Once we have prepared our configuration we need to implement our custom formatter PHP class. In this example we will extend the base "ezpAttributeOperatorFormatter" class which provides for your convenient two useful methods which returns type and value of object, array, etc. Note that your class has to implement at least "ezpAttributeOperatorFormatterInterface".

class ezpAttributeOperatorBKMLFormatter extends ezpAttributeOperatorFormatter implements ezpAttributeOperatorFormatterInterface
{
 
   /**
    * Formats header for the 'attribute' template operator output
    *
    * @param string $value
    * @param bool $showValues
    * @return string
    */
   public function header( $value, $showValues )
   {
       $headers = "Attribute [Type]";
       if ( $showValues )
           ;$headers  .= ": Value";
 
       return "<h1>{$headers}</h1>{$value}";
   }
 
   /**
    * Formats single line for the 'attribute' template operator output
    *
    * @param string $key
    * @param mixed $item
    * @param bool $showValues
    * @param int $level
    * @return string
    */
   public function line( $key, $item, $showValues, $level )
   {
       $type = $this->getType( $item );
       $value = $this->getValue( $item );
       $spacing = str_repeat( "&gt;", $level );
 
       if ( $showValues )
           $output  = "<div><strong>{$spacing}{$key}</strong> [ {$type} ]  : {$value}</div>";
       else
           $output  = "<div><strong>{$spacing}{$key}</strong> [ {$type} ]  : {$value}</div>";
 
       return $output;
   }
}

The "ezpAttributeOperatorFormatterInterface::header()" method allows you to customize a header style and format while the "ezpAttributeOperatorFormatterInterface::line()" method allows you to customize a single line/row of the attribute() template operator output.

Activate extension   

Next we need to activate our extension. Add following line in to the settings/override/site.ini.append.php:

[ExtensionSettings]
ActiveExtensions[]
[...]
ActiveExtensions[]=ezattributeformatter

Update autoload array

Once our extension is activated we need to update the PHP autoload array. In order to do so, execute following command from eZ Publish root folder:

php bin/php/ezpgenerateautoloads.php -e -p

Use the new output formatter

Now you we can use a new output formatter for attribute() template operator as follows:

{$node|attribute('show', 2, 'custom')}

Geir Arne Waaler (15/08/2011 1:22 pm)

Geir Arne Waaler (03/10/2011 11:05 am)


Comments

There are no comments.