Posted by pwolanin on November 8, 2010 at 4:11pm
Start:
2010-12-01 19:00 - 21:00 America/New_York Organizers:
Event type:
User group meeting
Location: Institute for Advanced Study, Bloomberg Hall, Room 201 (Physics Library)
Topics:
- Presentation on getting started with module development
- Interactive help on coding your first module
- Mutual assistance with development and site building challenges
Hosted by the Institute for Advanced Study Computing Department.
Directions: There is a map of the IAS campus on this page - http://www.ias.edu/about/directions.
Bloomberg Hall is building 4 on the map, next to Parking Lot A. Parking is free and generally there are plenty of spaces. We will meet in the Physics Library, Room 201.
Comments
Here's a starting point for
Here's a starting point for discussion. This custom code code is from Aaron. He discussed it during the Audubon site showcase. It displays the content type and its description when adding or editing content:
<?php
/**
* Place this code in a custom site module,
* and replace MYMODULE with the module name.
*
* Example: modules/custom/MYMODULE/MYMODULE.module
*/
/**
* Implementation of hook_form_alter().
*/
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
$node_type_name = '';
// Avoid PHP notices by checking if '#node' exists.
if (!empty($form['#node']->type)) {
$node_type_name = $form['#node']->type;
}
switch ($form_id) {
// Alter all node edit forms
case $node_type_name .'_node_form':
// Add content type label/description to edit form
// as a new form variable called "formal-title"
if (arg(1) =='add' || arg(2) =='edit') {
// Get data on a content type
$node_type = node_get_types('type', $form['#node']);
// create new form element, and pass content type data
$form['formal-title'] = array(
'#type' => 'item',
'#weight' => -15,
'#title' => t('Content Type'),
'#value' => '<h3 class="node-type-name">'. check_plain($node_type->name) .'</h3>'. filter_xss_admin($node_type->description),
);
} // end if
break;
} // end switch
} // end form_alter
?>
Looking forward to the meetup
Looking forward to the meetup - have been slogging through some sample modules and could really use some practical guidance.
Any chance we could about debug techniques at this or some future meetup
Ian
Sure. We are planning on
Sure. We are planning on discussing module development, so that will naturally lead to debugging. If you have specific questions, post them or bring them along.
Just a reminder that this is
Just a reminder that this is tonight.
Below is the code from the
Below is the code from the module we worked on. And these are the links to some of the web pages we discussed.
http://drupal.org/node/231036 - writing .info files
http://drupal.org/coding-standards
http://api.drupal.org
http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hoo...
http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hoo...
http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hoo...
http://drupal.org/project/coder
http://drupal.org/project/module_builder
http://drupal.org/project/examples
<?php
/<strong>
* A simple example module demonstrating some hook implementations and
* custom functions.
*/
/</strong>
* Implementation of hook_menu().
*/
function mymodule_menu() {
$items = array();
$items['mymodule'] = array(
'title' => 'My Module Title', // t() unnecessary if text is hard-coded.
'description' => 'Description of my module page',
'access callback' => TRUE,
'page callback' => 'mymodule_page1',
'type' => MENU_CALLBACK,
);
$items['admin/build/customblock'] = array(
'title' => 'My Module Custom Blocks',
'description' => 'Custom block administration.',
'page callback' => 'mymodule_customblock_admin',
'access arguments' => array('administer mymodule customblocks'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/build/customblock/edit'] = array(
'title' => 'Edit Custom Block',
'page callback' => 'drupal_get_form',
'page arguments' => array('mymodule_customblock_edit'),
'access arguments' => array('administer mymodule customblocks'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/<strong>
* Implementation of hook_perm().
*/
function mymodule_perm() {
return array('administer mymodule customblocks');
}
/</strong>
* Menu callback to display a custom page.
*
* This is a custom function referenced in mymodule_menu as a page callback.
*/
function mymodule_page1() {
$content = "<p>Lots of PHP code here, if you want.</p>";
$content .= "<p>Hello World.</p>";
return $content;
}
/<strong>
* Menu callback to display a custom administration page.
*
* This is a custom function referenced in mymodule_menu as a page callback.
*/
function mymodule_customblock_admin() {
$header = array(array('data' => 'Block'));
$rows = array(
array(l('Custom Block 0', 'admin/build/customblock/edit/0')),
array(l('Custom Block 1', 'admin/build/customblock/edit/1')),
);
$output = theme('table', $header, $rows);
return $output;
}
/</strong>
* Form builder function used by a menu callback.
*
* This is the page argument from mymodule_menu. The page callback was drupal_get_form and
* this function is the argument. It provides the form elements.
*/
function mymodule_customblock_edit(&$form_state, $offset = 0){
$form['customblock'] = array(
'#type' => 'textarea',
'#rows' => 20,
'#title' => 'Custom block body text',
'#description' => 'This text will go into the body of the block',
'#default_value' => variable_get('mymodule_customblock' . $offset, ''),
'#required' => TRUE,
);
$form['customblock_id'] = array(
'#type' => 'hidden',
'#value' => $offset,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save changes',
);
return $form;
}
/<strong>
* Form submit function.
*
* This is the submit function for the mymodule_customblock_edit form.
* This does the work when the form is submitted.
*
* @see mymodule_customblock_edit()
*/
function mymodule_customblock_edit_submit($form, &$form_state) {
variable_set('mymodule_customblock' . $form_state['values']['customblock_id'], $form_state['values']['customblock']);
drupal_set_message(t('The block was saved.'));
// Send the user to a different page after submission.
$form_state['redirect'] = '/admin/build/customblock';
}
/</strong>
* Implementation of hook_block().
*/
function mymodule_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = 'Custom Block 0'; // These values will be overridden by block administration.
$blocks[1]['info'] = 'Custom Block 1';
return $blocks;
case 'view':
if ($delta == 0) {
return mymodule_customblock0();
}
elseif ($delta == 1) {
return mymodule_customblock1();
}
}
}
/<strong>
* Custom function to build block 0 content.
*/
function mymodule_customblock0() {
$block['subject'] = 'Custom Block 0';
$block['content'] = variable_get('mymodule_customblock0', 'Custom Block 0');
return $block;
}
/</strong>
* Custom function to build block 1 content.
*/
function mymodule_customblock1() {
$block['subject'] = 'Custom Block 1';
$block['content'] = variable_get('mymodule_customblock1', 'Custom Block 1');
return $block;
}
?>
Great skeleton of a module
Thanks very much for posting. This is very helpful for me.
I wonder why you used 'type' => MENU_CALLBACK, in 'My Module Title'. Drupal does not render MENU_CALLBACK, so the menu item can appear in a menu only after extra steps are taken in 'admin/build/menu'.
If you change 'type' => MENU_CALLBACK to 'type' => MENU_NORMAL_ITEM, or leave out 'type' completely since MENU_NORMAL_ITEM is the default, 'My Module Title' will appear in the Navigation menu after enabling the module. MENU_CALLBACK doesn't seem to be required for the page callback 'mymodule_page1' to load either.
I don't mean to nitpick. Just wondering.
You are correct. If you go
You are correct. If you go with MENU_NORMAL_ITEM or remove it, you will get 'My Module Title' in the menu system. In this example, I wanted to concentrate on registering the URL, not the menu, so I set it to MENU_CALLBACK. There is no other particular reason for it.