eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationListConverter::toFieldDefinition PHP Method

toFieldDefinition() public method

public toFieldDefinition ( StorageFieldDefinition $storageDef, eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef )
$storageDef eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition
$fieldDef eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition
    public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
    {
        // default settings
        $fieldDef->fieldTypeConstraints->fieldSettings = array('selectionMethod' => 0, 'selectionDefaultLocation' => null, 'selectionContentTypes' => array());
        // default value
        $fieldDef->defaultValue = new FieldValue();
        $fieldDef->defaultValue->data = array('destinationContentIds' => array());
        if ($storageDef->dataText5 === null) {
            return;
        }
        // read settings from storage
        $fieldSettings =& $fieldDef->fieldTypeConstraints->fieldSettings;
        $dom = new DOMDocument('1.0', 'utf-8');
        if ($dom->loadXML($storageDef->dataText5) !== true) {
            return;
        }
        if ($selectionType = $dom->getElementsByTagName('selection_type')) {
            $fieldSettings['selectionMethod'] = (int) $selectionType->item(0)->getAttribute('value');
        }
        if (($defaultLocation = $dom->getElementsByTagName('contentobject-placement')) && $defaultLocation->item(0)->hasAttribute('node-id')) {
            $fieldSettings['selectionDefaultLocation'] = (int) $defaultLocation->item(0)->getAttribute('node-id');
        }
        if (!($constraints = $dom->getElementsByTagName('constraints'))) {
            return;
        }
        foreach ($constraints->item(0)->getElementsByTagName('allowed-class') as $allowedClass) {
            $fieldSettings['selectionContentTypes'][] = $allowedClass->getAttribute('contentclass-identifier');
        }
    }

Usage Example

    /**
     * @group fieldType
     * @group relationlist
     */
    public function testToFieldDefinitionMultiple()
    {
        $storageFieldDefinition = new StorageFieldDefinition();
        $storageFieldDefinition->dataText5 = <<<EOT
<?xml version="1.0" encoding="utf-8"?>
<related-objects>
    <constraints>
        <allowed-class contentclass-identifier="forum"/>
        <allowed-class contentclass-identifier="folder"/>
    </constraints><type value="2"/>
    <object_class value=""/>
    <selection_type value="1"/>
    <contentobject-placement node-id="54321"/>
</related-objects>

EOT;
        $expectedFieldDefinition = new PersistenceFieldDefinition(array('fieldTypeConstraints' => new FieldTypeConstraints(array('fieldSettings' => array('selectionMethod' => Type::SELECTION_DROPDOWN, 'selectionDefaultLocation' => 54321, 'selectionContentTypes' => array("forum", "folder")))), 'defaultValue' => new FieldValue(array('data' => array('destinationContentIds' => array())))));
        $actualFieldDefinition = new PersistenceFieldDefinition();
        $this->converter->toFieldDefinition($storageFieldDefinition, $actualFieldDefinition);
        $this->assertEquals($expectedFieldDefinition, $actualFieldDefinition);
    }