CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Trying to remove an option from a QuickForm select in hook buildForm
Pages: [1]

Author Topic: Trying to remove an option from a QuickForm select in hook buildForm  (Read 3537 times)

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Trying to remove an option from a QuickForm select in hook buildForm
November 16, 2011, 07:23:16 am
Use case: in all Cases (the customer uses CiviCase heavily) the user wants to be able to have a special type of activity that is only available to members of a specific group (only they will have the privilige to see the details of this case activity). I have set up the group and the activity types, and have made a customization to make sure that the user that is NOT a member of this group does not see the details of the activity. All fine!

Now I also want to make sure that the special activity only shows up in the select list for 'New Activity' if the user is member of that special group. I was planning to do this with the buildForm hook. Now I can get a list of all the values in the select, I can get the options in an array and even remove the element of the array but that does not affect the select list. If I try a loadOptions, the options are added but do not override the existing ones. Here is the snippet I am playing with:

Code: [Select]
function dgwmaatwerk_civicrm_buildForm($formName, &$form) {
if ($formName == "CRM_Case_Form_CaseView") {
if ( $form->elementExists( 'activity_type_id' ) ) {
$elements = $form->getElement('activity_type_id');
$options = $elements->_options;
foreach ($options as $sleutel=>$optie) {
if ( $optie['attr']['value'] == 107) {
unset($options[$sleutel]);
$elements->load($options);
}
}
}
}
}
Now I know I still have to format the options array, but I do not know (and can not find in the documentation) how to clean the options of the select or, even better, remove an individual option? Hints anyone?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Trying to remove an option from a QuickForm select in hook buildForm
November 16, 2011, 07:42:19 am
Hi,

don't know if it's the same but for events, they are fields that definition that are duplicated, and you need to modify them in the right place (don't recall the details, but it involved a lot of words my mother wouldn't approve).

Anyway, try to print_r $form see if there is another place where you have that list.

Otherwise, do it reverse, start with the template and see where the data comes from.

If security isn't a super issue, I'd add a .extra.tpl that contains a {crmAPi to fetch group=your group &contact_id=current user and based on that, add a bit of jquery to hide the option

Hackish, but workaround.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Trying to remove an option from a QuickForm select in hook buildForm
November 16, 2011, 07:50:10 am
Have done the $form print as one of the first things, no more groups with the specific values. I will have a go with the tpl if I have to, but I thought let me not try the hackish stuff as a start for a change  ;D. Not familiar with jQuery, although I guess I could work myself out of this one. Need to corner you for an hour or two at the eurosprint to get jQuery training, see if there are others with that requirement :-)...thanks!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Trying to remove an option from a QuickForm select in hook buildForm
November 16, 2011, 11:34:05 am

i think u need to use references there, i.e:

Code: [Select]
function dgwmaatwerk_civicrm_buildForm($formName, &$form) {
   if ($formName == "CRM_Case_Form_CaseView") {
      if ( $form->elementExists( 'activity_type_id' ) ) {
         $elements =& $form->getElement('activity_type_id');
         $options =& $elements->_options;
         foreach ($options as $sleutel=>$optie) {
            if ( $optie['attr']['value'] == 107) {
               unset($options[$sleutel]);
               $elements->load($options);
            }
         }
      }
   }
}

Look at the $elements and $options assignment which i made a reference assignment

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Trying to remove an option from a QuickForm select in hook buildForm
November 17, 2011, 12:54:37 am
The references did the trick, thanks Lobo! The reference basically mean that I keep my objects open for updates rather than just retrieve the values in my array, right?
My working code is now:
Code: [Select]
function dgwmaatwerk_civicrm_buildForm($formName, &$form) {
if ($formName == "CRM_Case_Form_CaseView") {

if ( $form->elementExists( 'activity_type_id' ) ) {
$elements = & $form->getElement('activity_type_id');
$options = & $elements->_options;
foreach ($options as $sleutel=>$optie) {
if ( $optie['attr']['value'] == 107) {
unset($options[$sleutel]);
}
}
}
}
}
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Trying to remove an option from a QuickForm select in hook buildForm
November 17, 2011, 01:54:15 am
Duh,

well spotted.

yes, reference is that you get a link to the original data instead of copying it.

(that's a bit more complicated than that, for instance on the api $param is by value -ie. a read only- but PHP is clever enough to not duplicate and use the original param unless you modify it when only then it does a copy and modify your local version)

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Trying to remove an option from a QuickForm select in hook buildForm
November 17, 2011, 05:28:31 am
That is why you only get 'I'm like Lobo'  ;D
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

michaellenahan

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 0
    • mick.appspot.com
Re: Trying to remove an option from a QuickForm select in hook buildForm
February 21, 2012, 10:49:18 am
Erik, Lobo, Xavier ... thank you for this it really helped me today ... I'm still at the stage of working out how QuickForm works.

Here is some code which removes extra from-addresses from the Contacts > New Email form:
civicrm/activity/email/add?atype=3&action=add&reset=1&context=standalone
... so that only the top-most (preferred) address is visible.

Code: [Select]
function MYMODULE_civicrm_buildForm($formName, &$form) {
  if ($formName == 'CRM_Contact_Form_Task_Email') {
    // ensure that only one email is displayed in the fromEmailAddress box
    if ($form->elementExists('fromEmailAddress')) {
      $fromEmailAddressElement =& $form->getElement('fromEmailAddress');
      $options =& $fromEmailAddressElement->_options;
      // retain $options[0], unset the rest
      $options_count = count($options);
      for ($i = 1; $i < $options_count; $i++) {
        unset($options[$i]);
      }
    }
  }
}
« Last Edit: February 21, 2012, 10:53:50 am by michaellenahan »

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Trying to remove an option from a QuickForm select in hook buildForm
February 22, 2012, 04:51:28 am
Thanks for sharing Michael!  ;)
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

idmacdonald

  • I post occasionally
  • **
  • Posts: 69
  • Karma: 1
Re: Trying to remove an option from a QuickForm select in hook buildForm
May 24, 2013, 10:10:24 am
Hi,

I know that this thread is rather old, but I'm trying to use a version of michaellenahan's code to hide some of the tokens that appear in the 'Insert tokens' list on Email and PDF letter screens (tokens for fields that the client doesn't use). I can see the form element's options array, and I can see that my code (which is based on the suggestions above) does unset the options correctly. However, I still get the full list of tokens when the 'Insert tokens' pop-up appears. Is there something else I have to do? Or maybe modifying the CRM_Contact_Form_Task_Email form is not the correct way of removing tokens from the pop-up list? I haven't yet actually found the CiviCRM code that produces that list of available tokens, so I was hoping the buildForm hack would do it. But I don't think it will.

Any suggestions or tips greatly appreciated. Thanks!

-Ian
« Last Edit: May 24, 2013, 10:19:49 am by idmacdonald »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Trying to remove an option from a QuickForm select in hook buildForm
May 27, 2013, 04:02:42 am
Hi, this is a different topic/thread, could you start a new one to distinguish?

short answer:
http://civicrm.org/blogs/colemanw/create-your-own-tokens-fun-and-profit
longer answer in the new post you create ;)

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Trying to remove an option from a QuickForm select in hook buildForm

This forum was archived on 2017-11-26.