access setFormatter data parameter

access setFormatter data parameter

INTONEINTONE Posts: 153Questions: 58Answers: 6
edited October 2014 in Editor

I am trying to access the setformatter data params through a closure function but not having success. Here is my code:

  Field::inst( 'cases.notes' )->setFormatter( 
     function ( $val, $data, $opts ) {

          return $val."\n\n\n".$data['notes_poster']."Posted: ".date('D, d M y')." by user: ".$session->getVar('user_full_name');
    } )

I want to get the value of the notes but concatenate to it notes that was posted using notes_poster field plus other data. How can this be accomplished?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Is it the $data or $session variable you are having a problem with? For the $session variable you probably need to add using ($session) to the closure function since PHP doesn't automatically scope variables for inclusion in the closure (frustratingly if you come from a JS background!).

    Allan

  • INTONEINTONE Posts: 153Questions: 58Answers: 6

    " For the $session variable you probably need to add using ($session) to the closure function since PHP doesn't automatically scope variables "

    I have already removed the $session from the return but I am still not getting any value from $data['notes_poster']. Is this the correct way to access values from $data:

     $data['some_column_name']
    
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Assuming that you are sending notes_poster to the server on edit, then yes that should be the correct way of doing it. Perhaps you can show me the full PHP configuration you are using for the Editor class?

    Allan

  • INTONEINTONE Posts: 153Questions: 58Answers: 6
    edited October 2014

    I found the solution. I tried $data['cases']['notes_poster'] instead of $data['notes_poster'] and it worked. That's because I have the Field::inst( 'cases.notes_poster' ) for my server field. so full code would be:

           Field::inst( 'cases.notes' )->setFormatter( 
         function ( $val, $data, $opts) {
                   return $val."\n\n\n".$data['cases']['notes_poster']."Posted: ".date('D, d M y')." 
                   at   ".date("H:i:s");
             } )
    
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Answer ✓

    Yup that would do it. The dot notates that it is in a nested object (or array in PHP land).

    Allan

This discussion has been closed.