eileenmcnaughton / nz.co.fuzion.extendedreport

Extended Reports for CiviCRM
GNU Affero General Public License v3.0
20 stars 57 forks source link

hook_civicrm_alterReportVar is not working #577

Open fajlay opened 7 months ago

fajlay commented 7 months ago

I am trying to alter a report to show an extra column. Before updating the extension it worked fine. But after updating, it is not working anymore. Following is my code. The error shows "Call to a member function getVar() on string in ..."

I also noticed that $var->_select value is empty and $var->_columnHeaders, $var->getVar(), $var->setVar() are not working anymore. Any thoughts?

function extname_civicrm_alterReportVar($varType, &$var, $reportForm) { $instanceValue = $reportForm->getVar('_instanceValues');

if (!empty($instanceValue) && in_array($instanceValue['report_id'], array('price/lineitem',))) { if ($varType == 'sql') { $var->_columnHeaders['civicrm_card_name'] = array( 'title' => 'Credit Card Type', 'type' => 2, ); $var->_select .= ' , GROUP_CONCAT(DISTINCT (card_type.card_type_id)) as civicrm_credit_card_type , GROUP_CONCAT(DISTINCT (card_type_label.name)) as civicrm_card_name';

  $from = $var->getVar('_from');
  $from .= ' LEFT JOIN civicrm_financial_trxn card_type 
           ON (contribution.trxn_id = card_type.trxn_id) 
           LEFT JOIN civicrm_option_value card_type_label 
           ON (card_type.card_type_id = card_type_label.value AND card_type_label.option_group_id = 9)';
  $var->setVar('_from', $from);
}

} }

eileenmcnaughton commented 7 months ago

@fajlay calling getVar() is probably best avoided because it is a way to interact with a property on a class that wasn't intended for external interaction

I guess there are few different ways to do what you are trying to do but the first question is - could it be done with SearchKit? From my point of view I'm not aware of anything this extension does that search kit doesn't do better these days

fajlay commented 7 months ago

Thanks for your reply. The official CiviCRM guide suggested using getVar() to alter report output. So I was just following the official approach. https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterReportVar/

eileenmcnaughton commented 7 months ago

ouch - I might update that!

seamuslee001 commented 7 months ago

@eileenmcnaughton I will just say that getVar() has been used in some JMA extensions to add or modify things like the from etc

eileenmcnaughton commented 7 months ago

@seamuslee001 yeah - it has a lot of legacy uses - but it is basically used to access things that are not supported for external access - so it is totally liable to break at any time

adevapp commented 7 months ago

Yes, find the columns do not appear in the report. Example below. The code executes and freezes with a var_dump, but doesn't seem to work with the hook.

function eventidlink_civicrm_alterReportVar($varType, &$var, $reportForm) {

    $class = get_class($reportForm);

      if (in_array($class, ['CRM_Extendedreport_Form_Report_Event_ParticipantExtended']) && $varType == 'rows') {

            $var['civicrm_contact']['filters']['civicrm_contact_civicrm_contact_contact_id'] = [
            'name' => 'current_user',
            'title' => ts('Limit To Current User'),
            'type' => CRM_Utils_Type::T_INT,
            'operatorType' => CRM_Report_Form::OP_SELECT,
            'options' => ['0' => ts('No'), '1' => ts('Yes')],
          ];

    }

Might look at search kit.

adevapp commented 7 months ago

Built with Search Kit and looks amazing! Turned into a dashlet/report. Thank you @eileenmcnaughton for the suggestion.

eileenmcnaughton commented 7 months ago

@devappsoftware thanks for the feedback - & hopefully it will be the first of many search kit dashlets for you!