How can we create content objects from code ?

Author Message

Lazaro Ferreira

Tuesday 19 October 2004 9:27:48 am

Hi,

How can we create content objects without using Create Object Form ?

Lazaro
http://www.mzbusiness.com

Paul Forsyth

Tuesday 19 October 2004 10:02:46 am

What do you want to do here?

paul

Lazaro Ferreira

Tuesday 19 October 2004 10:47:52 am

Hi Paul,

We are writing an extension, we want to create a new object of a custom class, behind the scene, without using a form with NewButton object

How can we do it, without using

content/action Newbuttom feature 

?

For instance, calling an EZP kernel module from our extension code

we would prefer not to code (copy and change) all the low level code used by EZP itself in

/content/action

script

Lazaro
http://www.mzbusiness.com

Paul Forsyth

Tuesday 19 October 2004 10:55:48 am

Its perfectly possible to create the new object from your extension by writing a module. Though it depends upon how you activate the extension (how it is called). Can you say more?

paul

Lazaro Ferreira

Tuesday 19 October 2004 2:07:23 pm

Paul,

I will try to explain it further

We have an extension module, which basically implements a custom search then save the search critheria to re-use it later

Saving the search critheria means creating an object of Search Critheria class

We know we can do it, using the ezp standard feature to create new objects.

It means calling

content/action

( the view action of the content module ), from a http form that includes : submit button "NewButton", and input hidden fields for ClassID and NodeId data. As explained above this way we are forced to put a form with a visible button in a page to do the job.

What we want is to create our search critheria new object, calling some ezp api or feature that do the trick without using an HTML form

On the other hand, we know that we can put some ezp

content/action

code within our module to do it, but it would mean code that should be far more expensive to maintain in the future because it will include EZP kernel code directly

Well, I hope you have understood our drama, it could be short to a simple question

Is there a simple ezp api to do it properly ? (if it exists we didn't realize it from reading content/action code)

 

Lazaro
http://www.mzbusiness.com

Paul Forsyth

Tuesday 19 October 2004 2:41:40 pm

Since you are using an extension i assume you have a module of some kind. Something that is at the php level at least. While you are down there you could create an object and store the information needed in its attributes. Take a look at this thread to see examples of creating objects with php:

http://ez.no/community/forum/developer/importing_update_entry_if_it_already_exists

There are other examples on these forums about creating objects but post back here if you have problems.

paul

Lazaro Ferreira

Wednesday 20 October 2004 3:33:25 am

Paul,

Here again (sorry, I posted to the other forum, because I had understood you wrong)

Bringing the piece of code from http://ez.no/community/forum/developer/importing_update_entry_if_it_already_exists</code> here :

I perfectly understand that I will need to change the code to use with my own content objects, but I would like to confirm if all of the main steps are here :

1) Fetching the class id of the content object to be created

   
   $myClassID = eZContentClass::fetch( $myClassID );

2) Instantiating the content object

  $myContentObject =& $class->instantiate( $myUserCreatorID, $myDefaultSectionID, false);
  // what does mean the third parameter set to false?

3) Assign the new content object to the node tree

$nodeAssignment =& eZNodeAssignment::create( array(
                                                   'contentobject_id' => $mycontentObject->attribute( 'id' ),
                                                   'contentobject_version' => 1,
                                                   'parent_node' => $defaultUserPlacement,
                                                   'is_main' => 1
                                                  )
                                           );

$nodeAssignment->store();

4) Set members attributes

$myContentObjectAttributes =& $myContentObject->attribute('data_map');

$myFirstAttribute = $myContentObjectAttributes['my_first_attribute'];
$mySecondAttribute = $myContentObjectAttributes['my_second_attribute'];

5) Set and store each attribute

$myFirstNameAttribute->setAttribute("data_text", "My First Attribute data");  $myFirstNameAttribute->store();
    
$mySecondNameAttribute->setAttribute("data_text", "My Second Attribute data"); $myLastNameAttribute->store();


6) Publish the object.

$operationResult = eZOperationHandler::execute( 'content', 
                                                'publish',
                                                array( 'object_id' => $myContentObject->attribute( 'id' ),
                                                       'version' => $myContentObject->attribute('current_version' ) ) ); 

have I missed any important and basic step here ?

Lazaro
http://www.mzbusiness.com

Paul Forsyth

Wednesday 20 October 2004 4:54:19 am

1.Yes, but the code should be:

$myClass = eZContentClass::fetch( $myClassID );

This creates an object of your chosen class.

2. Yes.

Look in kernel/classes/ezcontentclass.php. The function line is:

function &instantiate( $userID = false, $sectionID = 0, $versionNumber = false, $newID = false )

As you can see the third parameter is for the version. Actually, you dont need the third parameter at all, the first two suffice.

3. Yes

4. Almost. The code is right but you must understand that all that code is doing is extracting the attributes/datatypes so they can be used later (point 5).

5. Yes. Though you must be careful here. The examples given work with for the text line datatype. Other datatypes require different settings. Have a read of this doc for more examples:

http://ez.no/ez_publish/documentation/development/importing_attribute_data

6. Yes, thats correct.

Thats pretty much it. You do have you to get your hands dirty here though, so dont worry if it doesnt work first time around :) Keep searching on the forums for more tips. This is a common topic.

Good luck,

Paul

Lazaro Ferreira

Thursday 21 October 2004 1:26:53 am

Paul,

I got it

Thank you

Lazaro
http://www.mzbusiness.com

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.