1

I am trying to set custom fields in a Custom Group. Here is my code:

function dfi_civicrm_buildForm($formName, &$form) {
  if ($formName == 'CustomData') {
    if ($form->getAction() == CRM_Core_Action::ADD) {
      echo 'Hello World';
      $defaults['custom_160_-5'] = mt_rand();
      $defaults['custom_70_-5'] = '23/10/2019';
      $form->setDefaults($defaults);
    }
  }
}

I have added it as an extension. It is not working even the Hello World isn't showing.

Thanks

  • Try -1 instead of -5 – Jaap Jansma - CiviCooP Oct 23 at 11:37
  • @JaapJansma-CiviCooP - Thank you for trying to help me but unfortunately no, if i put -1 or even remove the _-5 or _-1 it still isn't working. – Moe Oct 23 at 11:57
  • The formName probably isn't CustomData, it's usually something like CRM_XX_Form_XX. You can use a step-debugger or log it to the ConfigAndLog folder using CRM_Core_Error::debug_log_message($formName). – Demerit Oct 23 at 13:14
  • @JaapJansma-CiviCooP - You were right, the formname was incorrect the issue now is after populating the form doesn't save the values. – Moe Oct 23 at 14:29
1

Moe's answer is technically workable - but oof ouch my performance!

Instead, something like this:

function getElementNameFromFieldName($fieldName, $elementIndex) {
  // This function could be further optimized so it's only called once, but meh.
  $elementNames = array_keys($elementIndex);
  foreach ($fieldNames as $fieldName) {
    if (strpos($elementName, $fieldName) === 0) {
      return $elementName;
    }
  }
}

if ( $formName == 'CRM_Contact_Form_CustomData' || $form == CRM_Core_Action::ADD) {
  $custom198ElementName = getElementNameFromFieldName('custom_198', $elementIndex);
  $custom197ElementName = getElementNameFromFieldName('custom_197', $elementIndex);
  $custom160ElementName = getElementNameFromFieldName('custom_160', $elementIndex);

  $form->updateElementAttr($custom198ElementName, ['readonly']);
  $form->updateElementAttr($custom197ElementName, ['readonly']);
  $form->updateElementAttr($custom160ElementName, ['readonly']);
}

if ($form == CRM_Core_Action::ADD) {
  $customid = rand (1,10000);
  $currentDate = date('d/m/Y');

  $defaults[$custom160ElementName] = $customid;
  $defaults[$custom197ElementName] = $currentDate;
  $defaults[$custom198ElementName] = $currentDate;
  $form->setDefaults($defaults);
}

Additionally - your code (and mine) as written will set defaults on EVERY form's ADD action. You probably want to limit it to those that have these custom fields.

  • Do i use the buildform hook or a different hook? – Moe Oct 28 at 11:10
  • buildForm, yes. The helper function goes outside the hook of course. – Jon G - Megaphone Tech Oct 28 at 12:07
  • Thanks for your help but have got back these two error codes: Notice: Object of class CRM_Activity_Form_ActivityLinks could not be converted to int in dfi_civicrm_buildForm() Error: Using $this when not in object context in getElementNameFromFieldName() – Moe Oct 28 at 13:38
  • @Moe Sorry - I didn't test this code, it's intended more as a guideline than a copy-paste :) However, I'll modify the code to handle the error. The notice isn't being generated from this code (or even this hook) – Jon G - Megaphone Tech Oct 28 at 14:00
  • Thanks for all your help, really appreciated! – Moe Oct 28 at 14:18
1

With the help of JaapJansma-CiviCooP

I was able to make this work using the below code:

function dfi_civicrm_buildForm($formName, &$form) {
    if ( $formName == 'CRM_Contact_Form_CustomData' ) {
        for ($i = 1; $i < 10000; $i++) {
            $form->updateElementAttr('custom_198_'. $i,array('readonly'));
            $form->updateElementAttr('custom_197_'. $i,array('readonly'));
            $form->updateElementAttr('custom_160_'. $i,array('readonly'));
        }
     if ($form == CRM_Core_Action::ADD) {
        $customid = rand (1,10000);
        $currentDate = date('d/m/Y');
        for ($i = 1; $i < 10000; $i++) {
            $defaults['custom_160_-' . $i] = $customid;
            $defaults['custom_197_-' . $i] = $currentDate;
            $defaults['custom_198_-' . $i] = $currentDate;
            $form->updateElementAttr('custom_198_-'. $i,array('readonly'));
            $form->updateElementAttr('custom_197_-'. $i,array('readonly'));
            $form->updateElementAttr('custom_160_-'. $i,array('readonly'));
        }
        $form->setDefaults($defaults);
       } 
    }
}

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.