PHP variables passed into Field to display in Editor

PHP variables passed into Field to display in Editor

dp@ii.netdp@ii.net Posts: 38Questions: 10Answers: 0

I'd like to be able to use a PHP variable $_SESSION['username'] as the value for a field that Editor makes visible.

How can I `call out' in the PHP server side code to assign a PHP variable to a field.

What I'm trying to do is akin to;

Field::inst('username')-> XXX ($_SESSION['username'])

so the currently logged in username appears in a editor field, filled in automatically.

Can I do it ?
thanks!

Replies

  • allanallan Posts: 63,871Questions: 1Answers: 10,522 Site admin

    Good question. I've got one in return before I answer (because this will help frame the problem):

    Can the user name change per row in the table, or is it always going to be the same in all rows and it can't be edited by the end user (i.e. a read only field)?

    Allan

  • dp@ii.netdp@ii.net Posts: 38Questions: 10Answers: 0

    Hi Allan,

    Actually it is more a default value for a field - so I what I really want is for a field to be called "last updated by" and have it show the username of the logged in user - and allow it to be overwritten if needed.

    And worse, I need it independently for multiple fields in one row - which makes me think that it can't be sensibly done on the server side PHP because it would be filling in this default value for all fields of this type on every transaction - not knowing which ones were actually going to be touched in this transaction.

    So perhaps a variant of my question would be how can I pass items such as the php session variables from the server side to the client side so that I can as required set values to particular `last updated by' fields on the fly - ie. when I draw the editor with just the 3 or 4 fields in this operation, I can fill in the lastupdated field for this transaction using some new part of the json object that the server side is sending that contains the username?

    thanks.

  • allanallan Posts: 63,871Questions: 1Answers: 10,522 Site admin

    So perhaps a variant of my question would be how can I pass items such as the php session variables from the server side to the client side

    The way to do this is to use the Editor->data() method rather than Editor->json(). The former will give you the data array that Editor would return to the server, so you can add values and then use them however you wish on the client-side:

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

    Then on the client-side use initComplete to be able to access the value. For example:

    initComplete: function ( json ) {
      editor.on( 'initCreate initEdit', function () {
        editor.field( 'updatedBy' ).val( json.mySessionVar );
      } );
    }
    

    I've used initEdit and initCreate with a closure there to be able to access the JSON data. THat will set the value when the edit or create action starts, but it allows the end user to then override that value if that is what you want.

    Regards,
    Allan

  • dp@ii.netdp@ii.net Posts: 38Questions: 10Answers: 0

    Brilliant, exactly what I needed. Very elegant to be able to insert it into the JSON stream and read it out like a normal value.

    Here are some more snippets for those following along later.

    server side

        $data['sessionFullName'] = $_SESSION['user_real_name'];
    

    js side

      initComplete: function ( settings, json ) {
    ..
        editor.on( 'initCreate initEdit', function () {
            editor.field( 'valves.visualinspectionby' ).val( json.sessionFullName );
          });
    
This discussion has been closed.