Templateoperator mit 2 Parametern

21.03.2012 | eZ Publish

Ziel ist, in einer vorhandenen Operatordatei (hier: productsOperator.php) einen neuen Templateoperator zu definieren, dem 2 Parameter übergeben werden können:getProductDetailsFromXML($xmlFile, 'Dimens

Ziel ist, in einer vorhandenen Operatordatei (hier: productsOperator.php) einen neuen Templateoperator zu definieren, dem 2 Parameter übergeben werden können:

getProductDetailsFromXML($xmlFile, 'Dimensions')

1. Änderungen in <extensions>/autoloads/productsOperator.php

Hier neuen Operator (getProductDetailsFromXML) in die Liste eintragen: 

function operatorList() {
return array( 'getArrayFromXML', 'getProductDetailsFromXML' );
}

in der ParameterList werden die Parameter definiert:

function namedParameterList() {
     return array( 'getArrayFromXML' => array( 'xmlData' => array( 'type' => 'string ',
                                                                 'required' => false,
                                                                 'default' => 'null' ) ),
'getProductDetailsFromXML' => array( 'xmlFile' => array( 'type' => 'string ',
                                                                                   'required' => false,
                                                                                    'default' => 'null' ),
                                                       'xmlNode' => array( 'type' => 'string ',
'required' => false,
'default' => 'null' ) ) 
);

function modify:

function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters, $placement ){
 switch ( $operatorName ) {
 case 'getArrayFromXML':
 {
 $xmlObject = simplexml_load_string($operatorValue);
 $operatorValue = $this->getArrayFromXML($xmlObject);
 } break;
 case 'getProductDetailsFromXML':
 {
 $operatorValue = $this->getProductDetailsFromXML($namedParameters['xmlFile'],$namedParameters['xmlNode']);
 } break;
 }
 }

Operatorfunktion definieren:

function getProductDetailsFromXML($xmlFile,$xmlNode,
 $flattenValues=false,
 $flattenAttributes = false,
 $flattenChildren=false,
 $valueKey='@value',
 $attributesKey='@attributes',
 $childrenKey='@children'){

$xml = '';
 $return = array();
 $xmlFile = $_SERVER['DOCUMENT_ROOT']. $xmlFile;
 if (file_exists($xmlFile)) {
 if ($xml = simplexml_load_file($xmlFile ))
 {
 $return = array_merge($return, array('default_dimensions' => $xml->Dimensions->StandardDimensions->asXML()));
 $return = array_merge($return, array('made_to_measure' => $xml->Dimensions->MadeToMeasure->asXML()));
 $return = array_merge($return, array('colours' => $xml->Colours->asXML()));
 $return = array_merge($return, array('basic_colours' => $xml->BasicColours->asXML()));
 $return = array_merge($return, array('inlay_colours' => $xml->Inlays->asXML()));
 $return = array_merge($return, array('panel_options' => $xml->PanelOptions->asXML()));
 $return = array_merge($return, array('extras' => $xml->Extras->asXML()));
 $return = array_merge($return, array('resources' => $xml->EquipmentAndResources->asXML()));
 $return = array_merge($return, array('product_image' => strtolower($xml->Gallery->Picture[0]['value'])));
 
 } else {
 $return = "simplexml-Error with " . $xmlFile;
 }
 }
 else {
 $return = "File doesnt exist: " . $xmlFile;
 }

return $return;
 } 

2. in der <extensions>/autoloads/eztemplateautoload.php den neuen Operator anmelden:

$eZTemplateOperatorArray[] = array( 'script' => 'extension/hueppe/autoloads/productsOperator.php',
 'class' => 'productsOperator',
 'operator_names' => array( 'getArrayFromXML','getProductDetailsFromXML' ) );

Analyse

Entwurf

Development

Launch