This gist contains sample code for use in lessons.
Last active
August 29, 2015 14:00
-
-
Save Cyborg572/11166196 to your computer and use it in GitHub Desktop.
Example Module for Londug Intro to Drupal Modules (2014/04/22)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Admin pages for the Sandwiches module. | |
*/ | |
/** | |
* Form constructor for the sandwich admin form. | |
* | |
* @ingroup forms | |
*/ | |
function sandwich_admin($form, &$form_state) { | |
$sudo_state = array( | |
'visible' => array( | |
':input[name="sandwich_advanced_mode"]' => array('checked' => TRUE), | |
':input[name="sandwich_custom_mustard"]' => array('error' => TRUE), | |
), | |
); | |
$form['sandwich_vegetarian_only'] = array( | |
'#title' => t('Vegetarian Only'), | |
'#description' => t('Hide non-vegetarian sandwiches.'), | |
'#default_value' => variable_get('sandwich_vegetarian_only', FALSE), | |
'#type' => 'checkbox', | |
); | |
$form['sandwich_advanced_mode'] = array( | |
'#title' => t('Use Sudo'), | |
'#description' => t('Force more sandwiches.'), | |
'#default_value' => variable_get('sandwich_advanced_mode', FALSE), | |
'#type' => 'checkbox', | |
); | |
$form['sandwich_no_crusts'] = array( | |
'#title' => t('No Crusts'), | |
'#description' => t('Cuts the crusts off.'), | |
'#default_value' => variable_get('sandwich_no_crusts', FALSE), | |
'#type' => 'checkbox', | |
'#states' => $sudo_state, | |
); | |
$form['sandwich_custom_mustard'] = array( | |
'#title' => t('Custom mustard brand'), | |
'#default_value' => variable_get('sandwich_custom_mustard', FALSE), | |
'#type' => 'textfield', | |
'#states' => $sudo_state, | |
); | |
return system_settings_form($form); | |
} | |
/** | |
* Validation callback for sandwich admin form. | |
* | |
* @ingroup forms | |
*/ | |
function sandwich_admin_validate($form, &$form_state) { | |
$values = $form_state['values']; | |
if (!$values['sandwich_advanced_mode']) { | |
if ($values['sandwich_custom_mustard']) { | |
form_set_error('sandwich_custom_mustard', 'You\'re not sudo!'); | |
} | |
if ($values['sandwich_no_crusts']) { | |
form_set_error('sandwich_no_crusts', 'You\'re not sudo!'); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name = Sandwiches | |
description = Lists sandwiches | |
core = 7.x | |
package = other | |
version = 0.3 | |
configure = admin/config/sandwich |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Sandwiches module. | |
*/ | |
/** | |
* Implements hook_menu(). | |
*/ | |
function sandwich_menu() { | |
$items = array(); | |
$items['admin/config/sandwich'] = array( | |
'title' => 'Configure Sandwiches', | |
'description' => 'Allows sandwich-related settings to be adjusted.', | |
'page callback' => 'drupal_get_form', | |
'page arguments' => array('sandwich_admin'), | |
'access arguments' => array('administer sandwiches'), | |
'file' => 'sandwich.admin.inc', | |
); | |
return $items; | |
} | |
/** | |
* Implements hook_permission(). | |
*/ | |
function sandwich_permission() { | |
return array( | |
'administer sandwiches' => array( | |
'title' => t('Administer sandwiches'), | |
'restrict access' => TRUE, | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment