Editor - Returning additional custom data

Editor - Returning additional custom data

borconiborconi Posts: 56Questions: 19Answers: 1

Hi.

In a scenario, I ended up needing to return additional information to the client from the server which did not fit into any of the category (option, data, field errors, error) and I found no way to do that, hence I made a small modification of the Editor.php adding a new function to it:

 public function append_data ( $additional_data=array() )
     {
         $this->_out['additional_data']=$additional_data;
     }

And then I can just use it like:

$editor->append_data(array("This is a test message"));

Why is this needed?
For example, I process loads of data and I generate a PDF on the server (create an invoice from the submitted data), and I need to return an URL to the PDF, so on the client I can check and if there is an URL returned, I can present the user with a nice window giving options to download, email, etc the generated email, but for this I need to return data which falls outside the defined categories.

Hope this help somebody else as well, and maybe some similar function can get included in one of the next releases.

Replies

  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin

    Very useful - thanks for sharing this.

    With the PHP libraries there is another option whereby you don't need to modify the libraries - you can use the Editor->data() method to get the data array and then just echo out the encoded JSON yourself.

    For example

    var $data = Editor::inst( ... )
      ->fields( ... )
      ->process( ... )
      ->data();
    
    $data[ 'myData' ] = ...;
    
    echo json_encode( $data );
    

    All the Editor->json() method does is that echo json_encode() for you.

    Allan

  • borconiborconi Posts: 56Questions: 19Answers: 1

    A... ok, I didn't know about the ->data() function... I probably need to spend more time reading the documentation :blush:

  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin

    In fairness that one isn't really publicised much! The full PHP API docs are here if you are interested.

    Allan

This discussion has been closed.