Skip to content

Instantly share code, notes, and snippets.

@nervetattoo
Last active December 18, 2015 16:39
Show Gist options
  • Save nervetattoo/5813212 to your computer and use it in GitHub Desktop.
Save nervetattoo/5813212 to your computer and use it in GitHub Desktop.
eZ 5 course stuff
<?php
/**
* File containing the EztBlock converter
*
* @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version 1.0
*/
namespace ezt\TrainingBundle\Persistence\Legacy\Content\FieldValue\Converter;
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
use eZ\Publish\SPI\Persistence\Content\FieldValue;
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition;
use eZ\Publish\Core\FieldType\FieldSettings;
class EztBlock implements Converter
{
const STRING_LENGTH_VALIDATOR_IDENTIFIER = "BlockLengthValidator";
/**
* Factory for current class
*
* @note Class should instead be configured as service if it gains dependencies.
*
* @static
* @return TextLine
*/
public static function create()
{
return new self;
}
/**
* Converts data from $value to $storageFieldValue
*
* @param \eZ\Publish\SPI\Persistence\Content\FieldValue $value
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $storageFieldValue
*/
public function toStorageValue( FieldValue $value, StorageFieldValue $storageFieldValue )
{
$storageFieldValue->dataText = $value->data;
$storageFieldValue->sortKeyString = $value->sortKey;
}
/**
* Converts data from $value to $fieldValue
*
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value
* @param \eZ\Publish\SPI\Persistence\Content\FieldValue $fieldValue
*/
public function toFieldValue( StorageFieldValue $value, FieldValue $fieldValue )
{
$fieldValue->data = $value->dataText;
$fieldValue->sortKey = $value->sortKeyString;
}
/**
* Converts field definition data in $fieldDef into $storageFieldDef
*
* @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef
*/
public function toStorageFieldDefinition( FieldDefinition $fieldDef, StorageFieldDefinition $storageDef )
{
if ( isset( $fieldDef->fieldTypeConstraints->validators[self::STRING_LENGTH_VALIDATOR_IDENTIFIER]['maxStringLength'] ) )
{
$storageDef->dataInt1 = $fieldDef->fieldTypeConstraints->validators[self::STRING_LENGTH_VALIDATOR_IDENTIFIER]['maxStringLength'];
}
else
{
$storageDef->dataInt1 = 0;
}
if ( isset( $fieldDef->fieldTypeConstraints->validators[self::STRING_LENGTH_VALIDATOR_IDENTIFIER]['minStringLength'] ) )
{
$storageDef->dataInt2 = $fieldDef->fieldTypeConstraints->validators[self::STRING_LENGTH_VALIDATOR_IDENTIFIER]['minStringLength'];
}
else
{
$storageDef->dataInt2 = 0;
}
$storageDef->dataText1 = $fieldDef->defaultValue->data;
}
/**
* Converts field definition data in $storageDef into $fieldDef
*
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef
* @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef
*/
public function toFieldDefinition( StorageFieldDefinition $storageDef, FieldDefinition $fieldDef )
{
$validatorConstraints = array();
if ( isset( $storageDef->dataInt1 ) )
{
$validatorConstraints[self::STRING_LENGTH_VALIDATOR_IDENTIFIER]["maxStringLength"] = (int)$storageDef->dataInt1;
}
if ( isset( $storageDef->dataInt2 ) )
{
$validatorConstraints[self::STRING_LENGTH_VALIDATOR_IDENTIFIER]["minStringLength"] = (int)$storageDef->dataInt2;
}
$fieldDef->fieldTypeConstraints->validators = $validatorConstraints;
$fieldDef->defaultValue->data = $storageDef->dataText1 ?: null;
$fieldDef->fieldTypeConstraints->fieldSettings = new FieldSettings(
array(
"textRows" => $storageDef->dataInt1
)
);
$fieldDef->defaultValue->data = null;
}
/**
* Returns the name of the index column in the attribute table
*
* Returns the name of the index column the datatype uses, which is either
* "sort_key_int" or "sort_key_string". This column is then used for
* filtering and sorting for this type.
*
* @return string
*/
public function getIndexColumn()
{
return 'sort_key_string';
}
}
parameters:
ezsettings.ez.location_view:
full:
folderOverrideTraining:
template: "eztTrainingBundle:full:folder.html.twig"
match:
Identifier\ContentType: folder
ezpublish.fieldType.eztblock.class: ezt\TrainingBundle\FieldType\EztBlock\Type
ezpublish.fieldType.eztblock.converter.class: ezt\TrainingBundle\Persistence\Legacy\Content\FieldValue\Converter\EztBlock
ezpublish.fieldType.eztblock.storage_gateway.class: ezt\TrainingBundle\FieldType\EztBlockStorage\Gateway\LegacyStorage
ezsettings.ez.field_templates:
- {template: eztTrainingBundle::content_fields.html.twig, priority: 0}
services:
ezpublish.fieldType.eztblock.converter:
class: %ezpublish.fieldType.eztblock.converter.class%
tags:
- {name: ezpublish.storageEngine.legacy.converter, alias: eztblock, lazy: true, callback: '::create'}
ezpublish.fieldType.eztblock.storage_gateway:
class: %ezpublish.fieldType.eztblock.storage_gateway.class%
tags:
- {name: ezpublish.fieldType.externalStorageHandler.gateway, alias: ezurl, identifier: LegacyStorage}
ezpublish.fieldType.eztblock:
class: %ezpublish.fieldType.eztblock.class%
parent: ezpublish.fieldType
tags:
- {name: ezpublish.fieldType, alias: eztblock}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment