I am hooking into _buildForm for CRM_Contact_Form_Task_Email to add a hidden input called "order_id" that I will need for some token logic. This part is working great. But when I click Send Email, my _tokenValues hook fires and the &$values array is missing order_id.
Does Civi remove things from the $_POST array before building $values? Is this a bug or by design? I'm currently working around it by setting and getting my own session variables, but that means I have to create a session object in buildForm and again in tokenValues ($this->set() doesn't seem to work within an extension).
This code creates my extra input. I have verified that the input is written in the form when I view source in the browser. But doesn't make it to tokenValues:
function invoicetoken_civicrm_buildForm($formName, &$form) {
if($formName == 'CRM_Contact_Form_Task_Email') {
if (CRM_Utils_Request::retrieve('order_id', 'String')) {
$order_id = CRM_Utils_Request::retrieve('order_id', 'String');
$form->addElement('hidden','order_id', $order_id);
}
}
}
Edit: Based on Lobo's answer, I tried using $form->get(), but something still isn't working:
function invoicetoken_civicrm_buildForm($formName, &$form) {
//add order_id to our form
if($formName == 'CRM_Contact_Form_Task_Email') {
//buildForm runs on get and post. this line re-adds the input during post
if ($form->get('order_id')) {
$order_id = $form->get('order_id');
$form->addElement('hidden','order_id', $order_id);
}
//this line adds the input on initial load of the form
elseif (CRM_Utils_Request::retrieve('order_id', 'String')) {
$order_id = CRM_Utils_Request::retrieve('order_id', 'String');
$form->addElement('hidden','order_id', $order_id);
}
}
}
No matter what I do, the input doesn't make it over to tokenValues; when I log the $form object, 'order_id' field appears in ['_submitValues']. I have tried $form->set()
, $form->setVar
, and any other combination I could think of. In the end, the only thing that works is to use a session var.