Skip to content

Instantly share code, notes, and snippets.

@rscotton
Created July 22, 2015 01:35
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 rscotton/a4a8678182dd244a0f2b to your computer and use it in GitHub Desktop.
Save rscotton/a4a8678182dd244a0f2b to your computer and use it in GitHub Desktop.
Duke Scholars Add Scholar Form
<?php
/*
* Demonstrate setting up Add Scholar page in Drupal
*/
function dom_scholars_menu() {
$items = array();
//Use drupal_get_form and pass custom form below as an argument in order to pull form to page
$items['add-scholar'] = array(
'title' => 'Add Scholar',
'page callback' => 'drupal_get_form',
'page arguments' => array('dom_scholars_add_form'),
'access callback' => '_dom_scholars_access',
);
return $items;
}
//See Drupal form API: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7
function dom_scholars_add_form($form, &$form_state) {
switch (current_path()) {
case 'add-scholar':
$form['scholar_id'] = array(
'#type' => 'textfield',
'#title' => 'Scholar Id',
);
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => 'Add Scholar',
);
$form['#submit'][] = 'dom_scholars_add_form_submit';
break;
}
return $form;
}
function dom_scholars_add_form_submit($form, &$form_state) {
switch (current_path()) {
case 'add-scholar':
if (!empty($form_state['values']['scholar_id'])) {
_dom_scholars_retrieve_content($form_state['values']['scholar_id']); //Call core import function
}
else {
drupal_set_message(t('You must provide a scholar id.'), 'error');
}
break;
}
}
//Only allow administrators to add scholars
function _dom_scholars_access() {
global $user;
return in_array('administrator', $user->roles);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment