eZ Community » Forums » Discussions » Creating eZ Publish Objects in PHP
expandshrink

Friday 04 June 2010 5:09:02 pm - 8 replies

» Read full tutorial

Introduction

This tutorial will show you how to create eZ Publish objects using PHP to allow you to dynamically create or import content from elsewhere. After reading this article you should be able to create objects for any situation including those with XML fields, image or file fields and object relations.

Wednesday 09 June 2010 2:09:26 pm

Hi David

Thanks for this piece of knowledge happy.gif Emoticon

However, I noticed that you access information from your objects directly from their properties :

//setting general node details
$params = array();
$params ['class_identifier'] = 'folder'; //class name (found within setup=>classes in the admin if you need it
$params['creator_id'] = $user->ContentObjectID;//using the user created above
$params['parent_node_id']=$parent_node->NodeID;//pulling the node id out of the parent
$params['section_id'] = $parent_node->ContentObject->SectionID;

This is a bad practice as you should access these informations using the attribute() accessor method, as eZContentObject inherits from eZPersistentObject. With this accessor, you can access to the very same properties than in templates as described in the online doc :

//setting general node details
$params = array();
$params ['class_identifier'] = 'folder'; //class name (found within setup=>classes in the admin if you need it
$params['creator_id'] = $user->attribute( 'contentobject_id' ); //using the user created above
$params['parent_node_id']=$parent_node->attribute( 'node_id' );//pulling the node id out of the parent
$params['section_id'] = $parent_node->attribute( 'object' )->attribute( 'section_id' );

This notice is available for all PHP classes inheriting from eZPersistentObject, which is the case for number of eZ Publish PHP classes. To view all available properties for these classes, see their static method definition() (field and function_attributes keys). See eZContentObject::definition() in API doc.

Another quick note about XML blocks (rich text) : It's better to use the input parser from eZOE extension (eZOEInputParser), which supports lots of additional HTML tags rather than eZSimplifiedInputParser blunk.gif Emoticon

Modified on Wednesday 09 June 2010 2:57:19 pm by Jérôme Vieilledent

Sunday 13 June 2010 4:35:06 pm

Hi,

There are a lot of examples how to access and manipulate ez publish
objects directly from PHP and not ez publish templates yours.
But all those examples require the php script to run using ez
publish's CLI/ezscript setup.

What I'm trying to achieve is to be able to use an ez publish instance
from within another web application, in this case an app written using
ez components' MVCTools.

Is there a way to initialize ez publish (like here
http://share.ez.no/tutorials/ez-p...publish-objects-in-php/%28page%29/2)
but not for a CLI-environment?

Thanks and kind regards,
Walter

Monday 13 September 2010 3:25:13 pm

An alternative to the standard CLI script which takes time to write is to use ezexec.php.

For example create /tmp/foo.php with the following content:

<?php
print_r(eZContentObjectTreeNode::fetch(2));
?>

And then run

php ./bin/php/ezexec.php /tmp/foo.php

And you will see that your script will be executed within an eZ Publish context.

That's useful for debugging or running quick tests.

happy.gif Emoticon

Modified on Monday 13 September 2010 3:26:29 pm by Jérôme Renard

Monday 13 September 2010 3:35:37 pm

An alternative to the standard CLI script which takes time to write is to use ezexec.php.

For example create /tmp/foo.php with the following content:

<?php
print_r(eZContentObjectTreeNode::fetch(2));
?>

And then run

php ./bin/php/ezexec.php /tmp/foo.php

And you will see that your script will be executed within an eZ Publish context.

That's useful for debugging or running quick tests.

happy.gif Emoticon

Extremely useful, indeed !

Thanks for the hint Jérôme blunk.gif Emoticon

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.

Monday 08 November 2010 12:22:59 pm

Thanks for this veteran remark blunk.gif Emoticon

Cheers,

Monday 17 September 2012 3:14:23 pm

The reply has been removed because of violation of forum rules.

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

expandshrink

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

36 542 Users on board!

Forums menu

Proudly Developed with from