eZ Community » Forums » General » How to access to content object...

How to access to content object attribute from a php script ?

How to access to content object attribute from a php script ?

Wednesday 14 May 2014 5:16:09 pm - 2 replies

Hello i try to get a json response without rendering a view with eZ Publish.
So I trying to use a custom module to do that:
    function jsonConvert(){
         $articles = eZFunctionHandler::execute(
                      'parent_node_id' => $myParentNodeId
        header("Content-Type: application/json"blunk.gif Emoticon;
        return json_encode($articles);
      echo jsonConvert();

But I don't know how to access to my article's attributes. I only get the nodes' attributes...

Is anyone has an idea ?


Thursday 15 May 2014 10:46:43 pm

I assume you are stuck on a legacy version, otherwise I would recommend using the existing REST API, which does all the work for you.

Then the question is: what do you mean "not to render a view" ? I still see some http header in your code. Maybe you mean "not to use a template" ?

As a Side Note: a function which is called JsonConvert should not output http headers. You are designing your APIs with no good separation of concerns.

And another side note: if you are producing json output, it is generally recommended to create functions for ezjscore rather than plain modules. This way you only produce php arrays and do not worry about encoding them, and you get the js client to query them for free...

Then the answer is: eZ4 persistent objects and all objects you can use in templates implement an interface based on usage of the attribute() call to get their properties. A simple json_encode will not work, as it can not "see" the dynamic attributes such as $node.data_map.

If you want to do properly a custom json or xml encoding, I recommend to stand on the shoulder of giants and reuse existing extensions. Pick one, two or three out of:

a) use templates for encoding: a bit slower, but flexible - http://projects.ez.no/ggxmlview

b) expose existing fetch functions as webservices: http://projects.ez.no/ezwebservicesapi

c) has the magic "recursive encoding of peristentobjects" already done: http://projects.ez.no/ezpersistentobject_inspector

Modified on Thursday 15 May 2014 10:48:39 pm by Gaetano Giunta

Monday 19 May 2014 4:47:01 pm

Hello Gaetano,

Yes I mean "not to use a template",  I'll explore all your advises, thank you very much for your help!


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

36 542 Users on board!

Forums menu

Proudly Developed with from