Skip to content

Instantly share code, notes, and snippets.

Created September 8, 2015 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/be210cb033f3408b83aa to your computer and use it in GitHub Desktop.
Save anonymous/be210cb033f3408b83aa to your computer and use it in GitHub Desktop.
<?php
function icrs_registration_register_form($form, &$form_state) {
$form['heading'] = array(
'#type' => 'markup',
'#markup' => '<h2>' . t('Clinic Details') . '</h2>',
);
$form['clinic_postal_code_label'] = array(
'#markup' => '<span class="outer-label">' . t('Clinic Postal Code') . '</span>',
);
$form['clinic_postal_code_lookup'] = array(
'#type' => 'textfield',
'#title' => t('Postal Code'),
);
// Submit.
$form['actions']['search'] = array(
'#type' => 'button',
'#value' => t('Search'),
'#attributes' => array(
'class' => array(
'icrs-primary-button',
),
),
'#ajax' => array(
'event'=> 'click',
'callback' => 'ajax_postal_code_lookup',
'wrapper' => array('results', 'add-new-clinic'),
'method' => 'replace',
// 'progress' => array('type' => 'none'),
),
);
//replace_div_1
$form['results'] = array(
'#type' => 'item',
'#prefix' => '<div id="results">',
'#suffix' => '</div>',
);
// replace_div_2
$form['add_new_clinic'] = array(
'#type' => 'item',
// '#title' => t("My conditional field two"),
'#prefix' => '<div id="add-new-clinic">',
'#suffix' => '</div>',
);
$form['#attached']['js'] = array(
drupal_get_path('module', 'icrs_registration') . '/js/postal_code.js',
);
return $form;
}
function ajax_postal_code_lookup($form, &$form_state) {
// Init vars.
$params = '';
$variables = array();
// Set values.
$params = array(
'postalCode' => $form_state['values']['clinic_postal_code_lookup'],
);
// Make call to web service.
$response = sti_restful_clinic_search($params);
// If response is OK.
if ($response['status'] === 200) {
$variables = array(
'results' => $response['data'],
'text' => t('Not your clinic?'),
'add_new_clinic' => l(t('Add New Clinic'), 'clinic-register', array(
'attributes' => array(
'class' =>
'icrs-secondary-button',
)
)
),
);
$form['results'] = theme('icrs_registration_postal_code_lookup_results', $variables);
$form['add_new_clinic'] = '';
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace("#results", render($form['results'])),
ajax_command_replace("#add-new-clinic", render($form['add_new_clinic']))
)
);
}
// If there are no clinic results found.
elseif ($response['status'] != 200) {
$variables = array(
'no_clinic_results' => TRUE,
'text' => t('No matching clinic found!'),
'add_new_clinic' => l(t('Add New Clinic'), 'clinic-register', array(
'attributes' => array(
'class' =>
'icrs-secondary-button',
)
)
),
);
$form['results'] = '';
$form['add_new_clinic'] = theme('icrs_registration_postal_code_lookup_results', $variables);
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace("#results", render($form['results'])),
ajax_command_replace("#add-new-clinic", render($form['add_new_clinic']))
)
);
}
}
// Works on jsfiddle
// http://jsfiddle.net/z492b657/14/
// // Jquery wrapper for drupal to avoid conflicts between libraries.
// (function ($) {
// // Jquery onload function.
// // Add class
// $(document).ready(function() {
// $('.clinic-results').change(function() {
// $('.set-as-my-clinic').attr("href", "/icrs/user-registration?id=" + $(this).val());
// });
// });
// })(jQuery);
jQuery(function($) {
// console.log('foo');
$('.clinic-results').change(function() {
$('.set-as-my-clinic').attr("href", "/icrs/user-registration?id=" + $(this).val());
});
});
<?php
/**
* @file
* Template for remove medication confirmation modal.
*/
?>
<?php if (!empty($no_clinic_results)) : ?>
<div class="clinic-results-wrapper">
<div class="clinic-results-contents">
<span class="clinic-results-items">
<?php print $text; ?>
</span>
</div>
<div class="clinic-results-button">
<?php print $add_new_clinic; ?>
</div>
</div>
<?php else: ?>
<div class="clinic-results-wrapper">
<div class="clinic-results-contents">
<span class="clinic-results-items">
<form action="">
<?php foreach($results['entityLocation'] as $key=>$value): ?>
<div>
<input id="clinic-<?php print $value['id']; ?>" class="clinic-results" type="radio" value="<?php print $value['id']; ?>" name="clinic-results"><?php print $value['name']; ?>, <?php print $value['province']; ?>, <?php print $value['postalCode']; ?>
</div>
<?php endforeach; ?>
</form>
</span>
</div>
<div class="clinic-results-button">
<a class="icrs-primary-button set-as-my-clinic" href="/icrs/user-register"><?php print t('Set As My Clinic'); ?></a>
</div>
</div>
<div class="clinic-results-wrapper">
<div class="clinic-results-contents">
<span class="clinic-results-items">
<?php print $text; ?>
</span>
</div>
<div class="clinic-results-button">
<?php print $add_new_clinic; ?>
</div>
</div>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment