access setFormatter data parameter
access setFormatter data parameter
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
This discussion has been closed.
Answers
Is it the
$data
or$session
variable you are having a problem with? For the$session
variable you probably need to addusing ($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
" 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:
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
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:
Yup that would do it. The dot notates that it is in a nested object (or array in PHP land).
Allan