I'm trying to make some date fields appear if a dropdown above it says 'on leave'- is this possible?

Cheers,
Chris.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

merlinofchaos’s picture

I'm afraid I don't understand the question or the context of the question.

chrism2671’s picture

Sorry merlin, I was referring to the function described on the Ctools homepage as "Dependent -- a simple form widget to make form items appear and disappear based upon the selections in another item."

I took a look at dependent.inc but it only refers to checkboxes/radios. I was hoping to do the same with date & select. Is this possible?

merlinofchaos’s picture

Status: Active » Fixed

Ah! Yes you can make any field dependent on a dropdown; checkboxes and radios need special handling, is all. Views does it with textfields all over the place (it uses its own version of the same code).

chrism2671’s picture

Status: Fixed » Active

Brilliant thanks!

Just trying to get this to work, does this make sense to you?

 $form['Personal']['rfxuser_work_status'] = array(
      '#type' => 'select',
      '#title' => t('Status'),
      '#default_value' => isset($edit['rfxuser_work_status'])? $edit['rfxuser_work_status'] : NULL,
      '#required' => TRUE,
      '#disabled' => !(user_access('edit own rfxuser active or on leave') || user_access('edit group rfxuser active or on leave')),
      '#options' => array(
        0 => t('Active'), 
        2 => t('On Leave'),
      )
    );

    $form['Personal']['onleave_from'] = array(
      '#type' => 'date',
      '#default_value' => isset($edit['onleave_from'])? $edit['onleave_from'] : NULL,
      '#required' => FALSE, 
      '#disabled' => TRUE,
      '#process' => array('ctools_dependent_process'),
      '#dependency' => array('select:Personal[rfxuser_work_status]' => array(2)),
    );

I think my problem lies in the #dependency- I'm not sure if my syntax is quite right. It's based on '#dependency' => array('id-of-form-without-the-#' => array(list, of, values, that, make, this, gadget, visible))

Any help much appreciated! Thanks! :-)

merlinofchaos’s picture

Only radios use the special 'radio:' notation. You want to just use the id of the select which would be

'#dependency' => array('edit-personal-rfxuser-work-status' => array(2)),

(probably best to compare against the id in firebug of course)

chrism2671’s picture

OK great- I've got it working using textfields, BUT, it appears to fail on date fields. I think this might be because a date field actually consists of three selects with different id's (month, day, year). Interestingly, these get blocked at the PHP level, i.e. the date field doesn't get written to the page at all, so the JS has nothing to hide.

I had a good go at debugging dependent.inc but had no joy- what do you think?

merlinofchaos’s picture

Oh it may just be about ids. It's possible that date fields have the same problem that checkboxes and radios have, that Drupal doesn't provide the wrapper ID? Check the HTML source (compare to the textfield you've got working) and see. Sometimes just using #prefix and #suffix to provide the right #id will work.

chrism2671’s picture

Ok cool, I'm at the airport heading to turkey but I'll check it when I return! :-) thanks for all the help! :-)

perusio’s picture

I'm trying to get it to work in a ctools modal. It's for some style settings in a panels everywhere theme. Here's my code:

 $form['border']['test'] = array(
    '#type' => 'checkbox',
    '#id' => 'border-test',
    '#title' => t('test'),
    '#description' => t('test dependent'),
    '#default_value' => FALSE,
  );

  $form['border']['color']= array(
    '#title' => t('Border color'),
    '#type' => 'checkboxes',
    '#process' => array('ctools_dependent_process'),
    '#dependency' => array('border-test' => array(1)),
    '#options' => array(
      'light' => t('Light'),
      'dark' => t('Dark'),),
    '#description' => t('Choose a dark or light border color'),
    '#prefix' => '<div id="edit-border-color-wrapper"><div id="edit-border-color">',
    '#suffix' => '</div></div>',
    '#default_value' => (isset($style_settings['border']['color'])) ? $style_settings['border']['color'] : 'light',

I've checked with the browser inspector in chromium and everything it's ok, related to CSS IDs and such. But the radios never expand. I might add that this is a simplification of a previous situation with checkboxes. I tried this single checkbox, but to no avail. Any help is much appreciated.

Thank you

perusio’s picture

Ok. I grokked it. Between the HTML elements view and the console of the chromium browser inspector I managed to figure it out by inspecting the Drupal.CTools.dependent object in the console.
Here's my code. I want the hidden element to show if any of the checkboxes is selected.

$form['border']['position'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Border position'),
    '#options' => array(
      'top' => t('top'),
      'right' => t('right'),
      'bottom' => t('bottom'),
      'left' => t('left'),
    ),
    '#description' => t('Please check where the border should appear.'),
    '#default_value' => (isset($style_settings['border']['position'])) ? $style_settings['border']['position'] : array(),
  );

$form['border']['color']= array(
    '#title' => t('Border color'),
    '#type' => 'radios',
    '#options' => array(
      'light' => t('Light'),
      'dark' => t('Dark'),),
    // This ID *must be* the wrapper ID of the form element that gets hidden.
    '#prefix' => '<div id="edit-settings-border-color-wrapper"><div id="edit-settings-border-color">',
    '#suffix' => '</div></div>',
    '#process' => array('ctools_dependent_process', 'expand_radios'),
    '#dependency' => array('edit-settings-border-position-top' => array(1),
                           'edit-settings-border-position-right' => array(1),
                           'edit-settings-border-position-bottom' => array(1),
                           'edit-settings-border-position-left' => array(1),),
    '#description' => t('Choose a dark or light border color'),
    '#default_value' => (isset($style_settings['border']['color'])) ? $style_settings['border']['color'] : 'light',
  );

This is much nicer than the FAPI ahah form framework for this type of action. Merlin would an example module for dependent.inc interest you for inclusion in CTools? Right now there's a example plugin module, IMHO, there should be more examples of usage if the tools.

Of course I can always add a documentation page at d.o and include the example code.

Thank you for CTools.

merlinofchaos’s picture

Merlin would an example module for dependent.inc interest you for inclusion in CTools? Right now there's a example plugin module, IMHO, there should be more examples of usage if the tools.

Yes, yes, and more yes. Absolutely. Documentation is one place where people can really help improve CTools, because it's a place where I've been very poor at focusing my time.

merlinofchaos’s picture

Status: Active » Fixed

Marking fixed, the answer has been posted.

If anyone who participated in this would like to help improve the documentation, that would be greatly appreciated.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

adf1969’s picture

A note for anyone who has issues getting the ctools_dependent_process to work with date_popup widgets.
I ran into the same problem as chrism2671. It would display the date labels, but the date fields didn't display at all. (although the "dependent" functionality worked just fine, it just worked *only* with the labels since the date input elements did not get sent to the page)
I found out (by stepping thru the code numerous times) that I had to add a "date_popup_process" as indicated below. Then it works fine.

The following is the code I am using.

  $form = array();
  $form['bgs_filter'] = array(
    '#type' => 'fieldset',
    '#title' => 'Filter',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['bgs_filter']['bgs_date_filter_op'] = array(
    '#type' => 'select',
    '#title' => t('Date'),
    '#default_value' => '=',
    '#tree' => True,
    '#options' => array(
      '<'   => t('Is less than'),                 // Show Value
      '<='  => t('Is less than or equal to'),     // Show Value
      '='   => t('Is equal to'),                  // Show Value
      '!='  => t('Is not equal to'),              // Show Value
      '>='  => t('Is greater than or equal to'),  // Show Value
      '>'   => t('Is greater than'),              // Show Value
      'between'   => t('Is between'),             // Show Min + Max
      'not between' => t('Is not between'),       // Show Min + Max
      'empty'     =>  t('Is empty (NULL)'),       // Show None
      'not empty' => t('Is not empty (NOT NULL)'),// Show None
     ),    
  );
  $default_date = date('Y', time()).'-'.date('m', time()).'-'.date('d', time()).' 00:00:00';
  $default_date = '';
  $form['bgs_filter']['bgs_date_filter'] = array(
    '#tree' => TRUE,
    '#theme' => 'date_views_filter_form',
  );
  // ADF NOTE: To use the 'ctools_dependent_process' with a date_popup you MUST
  //  add the "date_popup_process" process entry FIRST. IF you dont', the Popup
  //  will NOT work.
  $form['bgs_filter']['bgs_date_filter']['value'] = array(
    '#type' => 'date_popup',
    '#date_format' => 'm/d/Y',
    //'#type' => 'date_select',
    //'#date_format' => 'd m Y',
    //'#date_year_range' => '-2:+1',
    '#default_value' => $default_date,
    '#process' => array('date_popup_process','ctools_dependent_process'),
    '#dependency' => array('edit-bgs-date-filter-op' => array('<','<=','=','!=','>=','>')),
  );
  $form['bgs_filter']['bgs_date_filter']['min'] = array(
    '#type' => 'date_popup',
    '#title' => 'From date',
    '#date_format' => 'm/d/Y',
    //'#type' => 'date_select',
    //'#date_format' => 'd m Y',
    //'#date_year_range' => '-2:+1',
    '#default_value' => $default_date,
    '#process' => array('date_popup_process','ctools_dependent_process'),
    '#dependency' => array('edit-bgs-date-filter-op' => array('between','not between')),
  );
  $form['bgs_filter']['bgs_date_filter']['max'] = array(
    '#type' => 'date_popup',
    '#title' => 'To date',
    '#date_format' => 'm/d/Y',
    //'#type' => 'date_select',
    //'#date_format' => 'd m Y',
    //'#date_year_range' => '-2:+1',
    '#default_value' => $default_date,
    '#prefix' => '<div id="edit-bgs-date-filter-to-wrapper"><div id="edit-bgs-date-filter-to">',
    '#suffix' => '</div></div>',
    '#process' => array('date_popup_process','ctools_dependent_process'),
    '#dependency' => array('edit-bgs-date-filter-op' => array('between','not between')),
  );
  $form['bgs_filter']['bgs_filter_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Apply'),
    '#attributes' => array('onclick' => 'javascript:bgs_helper_filter(); return false;'),
  );
  return $form;

Hope this helps save someone else some time...

Andrew.

sepla’s picture

If you're making a date field dependent make sure that you're not removing expand_date callback by overwriting the #process array. Below is a working example of a checkbox dependent date field:

  $form['scheduler_settings']['periodic']['expiry']['sms_panel_scheduler_expiry'] = array(
    '#type' => 'checkbox',
    '#title' => t('Check if you wish to expire this periodical cron job at a specific time.'),
    '#description' => t('By checking this option the job will be deleted from the cron queue at the specified time.'),
    '#default_value' => 0,
    // CTools dependent API compliant.
    '#id' =>'sms-panel-scheduler-expiry',
  );

  $form['scheduler_settings']['periodic']['expiry']['sms_panel_scheduler_expiry_date'] = array(
    '#type' => 'date',
    '#title' => t('Expiry Date'),
    // CTools dependent API compliant.
    '#process' => array('expand_date', 'ctools_dependent_process'),
    '#dependency' => array('sms-panel-scheduler-expiry' => array(TRUE)),
  );
joachim’s picture

Addendum to #15: depending on what type of date field you have (popup, select, etc) the process item is different, eg for a popup:

    '#process' => array('date_popup_process', 'ctools_dependent_process'),

The code in _date_api_elements() shows which one to use.

joachim’s picture

Title: Can I make some date fields dependent on a dropdown? » Document special treatment for date fields
Component: Miscellaneous » Documentation
Category: support » bug
Status: Closed (fixed) » Needs review
FileSize
2.18 KB

Here's a patch with that information.

japerry’s picture

Issue summary: View changes
Status: Needs review » Closed (outdated)

Closing this issue as outdated as Drupal 6 ctools is not supported. If this issue is relevant for Drupal 7, feel free to re-open and mark for Drupal 7 (or 8)