Tuesday 24 September 2013 4:09:35 pm

I've been looking for an easy way to create files and images from the default "attribute edit GUI" forms, without having to deal with $_FILES myself.

So, to upload a file from the default forms :

$http = eZHTTPTool::instance();
$base = "ContentObjectAttribute";

$datamap = $object->dataMap();
$attribute = $datamap['image'];

$datatype = $attribute->dataType();

// Enregistre le fichier physique directement depuis les données postées et enregistre un eZBinaryFile
$datatype->fetchObjectAttributeHTTPInput($http, $base, $attribute);

And to upload an image :

$http = eZHTTPTool::instance();
$base = "ContentObjectAttribute";

$datamap = $object->dataMap();
$attribute = $datamap['image'];

$datatype = $attribute->dataType();

// Objet de manipulation des attributs image
$imageHandler = new eZImageAliasHandler($attribute);
$attribute->setContent($imageHandler);

// Accroche le eZHTTPFile au eZImageAliasHandler et récupère le texte alternatif depuis les valeurs postées
$datatype->fetchObjectAttributeHTTPInput($http, $base, $attribute);

$alternativeText = $imageHandler->attribute('alternative_text');

// Récupération du fichier posté
$httpFile = $imageHandler->httpFile();

if ($httpFile)
{
    $imageHandler->initializeFromHTTPFile($httpFile, $alternativeText);
}

$imageHandler->store($attribute);

You can wrap it into the datatype's validation method, if you want to :

// Validation de la valeur postée
$state = $datatype->validateObjectAttributeHTTPInput($http, $base, $attribute);
$valid = $state == eZInputValidator::STATE_ACCEPTED;
 
if ($valid)
{
    // Enregistrement
}

Vince

Monday 08 November 2010 12:22:59 pm

Thanks for this veteran remark blunk.gif Emoticon

Cheers,

Monday 08 November 2010 12:03:00 pm

An extremely thorough and useful article, even for an eZ Publish veteran like myself. blunk.gif Emoticon

I would add, though, that allthough placing your scripts directly in the /bin/php/ folder of your eZP installation is fine for development purposes, you should place it in its own extension before deploying in order to ensure an easier upgrade process.

You must be logged in to post messages in this topic!