Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Example Module for Londug Intro to Drupal Modules (2014/04/22)

View README.md

Sandwich Module

This gist contains sample code for use in lessons.

View README.md
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
<?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!');
}
}
}
View README.md
1 2 3 4 5 6
name = Sandwiches
description = Lists sandwiches
core = 7.x
package = other
version = 0.3
configure = admin/config/sandwich
View README.md
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<?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
Something went wrong with that request. Please try again.