Custom content type creation and AHAH
| Project: | AHAH helper |
| Version: | 6.x-2.0 |
| Component: | Documentation |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
I managed to do this so far...
<?php
/**
* Implementation of hook_node_info().
*/
function resume_cck_node_info()
{
return array(
'resume_cck' => array(
'name' => t('resume_cck'),
'module' => 'resume_cck',
'description' => t('Node for testing'),
'has_title' => TRUE,
'title_label' => t('Title'),
'has_body' => TRUE,
'body_label' => t('Description'),
'min_word_count' => 0,
'locked' => FALSE
)
);
}
function get_country(){
$options = array('' => t('- select a country -'));
$res = db_query('SELECT countryid, title FROM {countries} ORDER BY title');
while ($o = db_fetch_object($res)) {
$options[$o->countryid] = $o->title;
}
return $options;}
function get_city($country){
$options = array('' => t('- select a city -'));
$res = db_query('SELECT cityid, city, countryid FROM {cities} WHERE countryid=%d ORDER BY city',$country);
while ($o = db_fetch_object($res)) {
$options[$o->cityid] = $o->city;
}
return $options;}
function resume_cck_menu()
{
$items['resume_cck'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('resume_cck_form'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);}
/**
* Implementation of hook_perm().
*/
function resume_cck_perm() {
return array('create resume_cck', 'edit own resume_cck');
}
/**
* Implementation of hook_access().
*/
function resume_cck_access($op, $node) {
global $user;
if ($op == 'create') {
// Only users with permission to do so may create this node type.
return user_access('create resume_cck');
}
// Users who create a node may edit or delete it later, assuming they have the
// necessary permissions.
if ($op == 'update' || $op == 'delete') {
if (user_access('edit own resume_cck') && ($user->uid == $node->uid)) {
return TRUE;
}
}
}
/**
* Implementation of hook_form().
*/
function resume_cck_form_alter(&$form, &$form_state, $form_id)
{
if ($form_id =='resume_cck_node_form')
{
ahah_helper_register($form, $form_state);
resume_cck_theform(&$form, $form_state);
}
}
function resume_cck_theform(&$form, $form_state)
{
if (!isset($form_state['storage']['country_fieldset']['country']))
{
$country_default_value = '0';
}
else
{
$country_default_value = $form_state['storage']['country_fieldset']['country'];
}
if (!isset($form_state['storage']['country_fieldset']['city']))
{
$city_default_value = '0';
}
else
{
$city_default_value = $form_state['storage']['country_fieldset']['city'];
}
$form['country_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Country information'),
'#prefix' => '<div id="billing-info-wrapper">', // This is our wrapper div.
'#suffix' => '</div>',
'#tree' => TRUE, // Don't forget to set #tree!
);
$form['country_fieldset']['country'] = array(
'#type' => 'select',
'#title' => t('country'),
'#options' => get_country(),
'#default_value' => $country_default_value,
'#ahah' => array(
'event' => 'change',
// This is the "magical path". Note that the parameter is an array of
// the parents of the form item of the wrapper div!
'path' => ahah_helper_path(array('country_fieldset')),
'wrapper' => 'billing-info-wrapper',
),
);
$form['country_fieldset']['update_country'] = array(
'#type' => 'submit',
'#value' => t('Update country'),
// Note that we can simply use the generic submit callback provided by the
// ahah_helper module here!
// All it does, is set $form_state['rebuild'] = TRUE.
'#submit' => array('ahah_helper_generic_submit'),
// We set the 'no-js' class, which means this submit button will be hidden
// automatically by Drupal if JS is enabled.
'#attributes' => array('class' => 'no-js'),
);
// If 'company' is selected, then these two form items will be displayed.
if ($country_default_value !== '')
{
$form['country_fieldset']['city'] = array(
'#type' => 'select',
'#title' => t('city'),
'#options' => get_city($country_default_value),
'#default_value' => $city_default_value,
'#ahah' => array(
'event' => 'change',
// This is the "magical path". Note that the parameter is an array of
// the parents of the form item of the wrapper div!
'path' => ahah_helper_path(array('country_fieldset')),
'wrapper' => 'billing-info-wrapper',
),
);
$form['country_fieldset']['update_city'] = array(
'#type' => 'submit',
'#value' => t('Update country'),
// Note that we can simply use the generic submit callback provided by the
// ahah_helper module here!
// All it does, is set $form_state['rebuild'] = TRUE.
'#submit' => array('ahah_helper_generic_submit'),
// We set the 'no-js' class, which means this submit button will be hidden
// automatically by Drupal if JS is enabled.
'#attributes' => array('class' => 'no-js'),
);
}
// And if 'private' is selected, then these two form items will be displayed.
else
{
print('hello');
}
}
/**
* Implementation of hook_help().
*/
function resume_cck_view($node, $teaser = FALSE, $page = FALSE) {
$node = node_prepare($node, $teaser);
return $node;
}
function resume_cck_help($path, $arg) {
switch ($path) {
case 'admin/help#resume_cck':
return t('ADMIN-HELP-TEXT');
break;
}
}
?>
everything works fine...the fields country and city populates ok but when i hit preview nothing shows up. I'm a little noob on drupal api and module developing so if anyone knows what is missing here please let me know. Thanks
