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) »
  • Removing options in select boxes using a hook
Pages: [1]

Author Topic: Removing options in select boxes using a hook  (Read 2034 times)

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Removing options in select boxes using a hook
September 09, 2009, 12:23:37 am
Hello,

Every now and again, I'd like to remove some of the options available in select form elements.

For example, to enforce workflows like 'admin staff cannot set contributions to received - only to in progress'  Or to remove the email and phone options from the activity types that appear in 'CRM_Activity_Form_Activity'.

I presume that there is a method in the $form object but am unsure of where to start to look for that. 

Should I be looking here: http://pear.php.net/package/HTML_QuickForm/?

Does anyone have any snippets they would be willing to share?

Thanks a lot,
Michael
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

Kiran Jagtap

  • Ask me questions
  • ****
  • Posts: 533
  • Karma: 51
Re: Removing options in select boxes using a hook
September 09, 2009, 03:09:52 am
I am not sure these are ideal solutions / ways, but I think hook_civicrm_buildForm will serve needful.

1. You need to register (add) new quick form element w/ same name and here you can modify or give select option list that you want.
this newly register quick form element w/ your modified options is going to suppress original element.

( make sure to properly overwrite original quick form element, like its attributes and so on ... ) 

OR

2. modify existing quick form element options.
eg : activity types

Code: [Select]
function civicrm_civicrm_buildForm( $formName, &$form ) {
    if ( $formName == 'CRM_Activity_Form_Activity' ) {
        //first check given element exists in quick form.
        if ( $form->elementExists( 'activity_type_id' ) ) {
            //get element.
            $element =& $form->getElement('activity_type_id');
            crm_Core_error::debug( 'options', $element->_options );
 
            //here you can play w/ options and you can modify those.
           //also you can add new options w/  $element->addOption( );
        }
    }
}


hope this help

kiran
You Are Designed To Choose... Defined By Choice.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Removing options in select boxes using a hook
September 10, 2009, 10:32:38 pm
Hi,

I'd go jQuery, not very secure, but dead easy

$('#paymentoption #direct').remove();


Depending of the form you might or might not have an id for each select value, change the code accordingly, or add a id= in the template (assuming that isn't a pregenereated html blob from php)

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

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Removing options in select boxes using a hook
September 15, 2009, 05:30:02 am
Thanks Xavier and Kiran,

I wonder which is most future proof in terms of upgrades.

Where do you stick the jQuery Xavier?  In the template?  If that's the case, I think it might be better for me to use the hook because I can specify all the forms in the if clause of the hook.  I'll let you know how I get on...
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Removing options in select boxes using a hook
September 15, 2009, 10:28:28 am
Hi,

two big options, depending on how many forms and pages are supposed to be impacted:

- I create a custom template that override the default for that element (contrib/search...) and add the jquery in that
- I put it in the page tpl of the theme, and if the page contains the right id/class, it applies the changes, otherwise silently does nothing.

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) »
  • Removing options in select boxes using a hook

This forum was archived on 2017-11-26.