Over the years, we compiled quite a bit of source code bits in our blog. This is the result! If you are a developer, you'll love. If you are not a developer - let's call em *geeks*.Linkshttp://api.drupal.orghttp://api.drupal.org/api/drupal/developer--topics--forms_api_reference....http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....SnippetsManually set path auto paths - in nodeapi op=update, insert<!--?phpif(module_exists('pathauto')) { _pathauto_include(); $origPath = 'node/' . $node--->nid; $terms = taxonomy_node_get_terms_by_vocabulary($node, 1); $term = array_shift($terms); $title = str_replace('ä', 'ae', $node->title); $title = str_replace('ü', 'ue', $title); $title = str_replace('ö', 'oe', $title); $title = str_replace('!', '', $title); $pathAlias = pathauto_cleanstring(str_replace('>', '', $term->name), true) . '/' . pathauto_cleanstring($title, true); path_set_alias($origPath, $pathAlias, null); } ?> Image Cache Usage<?phpprint theme('imagecache', $preset, $image['filepath'], $alt, $title, $attributes);?>Manually load a bunch of nodes with SQL<?php$nodes = array();$sql = "SELECT nid FROM content_type_event";$result = db_query($sql);while ($result && ($object = db_fetch_object($result))) { $nodes[] = node_load($object->nid);}?>Manually load a menu<?php$menu_global = menu_navigation_links("secondary-links", 0);$menu1 = menu_navigation_links("primary-links", 0);print theme('links', $menu1, array('class' => 'links_primary-links'));?>Add a javascript<?phpdrupal_add_js(drupal_get_path('theme', 'garland').'/custom.js');?>Get the nice url alias from raw path<?phpdrupal_get_path_alias('/node/'.$node->nid, $language->language)?>Manually filter text<?php$output_blogentry .= _filter_url(_filter_autop(($node->field_blog_content[0]['value'])), 2).'?>Programmatically output CCK node form<?php$node = new stdClass();$node->type = 'blog';$node->uid = $user->uid;$node->name = $user->name; // Important!module_load_include('inc', 'node', 'node.pages');$output .= drupal_get_form('blog_node_form', $node);?>Form alter example<?phpfunction custom_form_alter(&$form, $form_state, $form_id) { switch ($form_id) { case "search_form" : //debug($form); unset($form['title']); // unset the title break; }}?>Node Api Example<?phpfunction hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { switch ($op) { case 'presave': if ($node->nid && $node->moderate) { // Reset votes when node is updated: $node->score = 0; $node->users = ''; $node->votes = 0; } break; case 'insert': case 'update': if ($node->moderate && user_access('access submission queue')) { drupal_set_message(t('The post is queued for approval')); } elseif ($node->moderate) { drupal_set_message(t('The post is queued for approval. The editors will decide whether it should be published.')); } break; case 'view': $node->content['my_additional_field'] = array( '#value' => theme('mymodule_my_additional_field', $additional_field), '#weight' => 10, ); break; }}?>Additional documentation: https://api.drupal.org/api/drupal/developer%21hooks%21core.php/function/...Views programmatically Drupal 6<?phpviews_include('view');$view = view::load('newsticker');$view->execute();return print_r($view->result, true);?>or<?php$output .= views_embed_view('viewname', 'default', $user->uid);?>http://groups.drupal.org/node/10129 page template depending on parameters<?phpfunction custom_preprocess_page(&$vars) { if (arg(0)=='reservation') { $vars['template_files'] = array('page-iframe'); } if (arg(0)=='ajax') { $vars['template_files'] = array('page-ajax'); }}?>http://api.drupal.org/api/function/template_preprocess_page/6 http://drupal.org/node/249726 form _validate _submit functions Drupal 6<?phpdrupal_get_form(myname_form, $my_param1, $my_param2);function myname_form($form_state, $my_param1, $my_param2) {}function myname_form_validate($form, &$form_state) {}function myname_form_submit($form, &$form_state) {}?>Drupal 5<?phpdrupal_get_form(myname_form);function myname_form() {}function myname_form_validate($form_id, $form_values) {}function myname_form_submit($form_id, $form_values) {}?>create node programmatically Drupal 6<?php$reservation = new StdClass();$reservation->type = 'reservation';$reservation->field_reservation_tickets[0]['value'] = $form_state['values']['anzahl_tickets'];$reservation->field_reservation_email[0]['value'] = $form_state['values']['email'];$reservation->field_reservation_telefon[0]['value'] = $form_state['values']['telefon'];$reservation->field_reservation_veranstaltung[0]['nid'] = $form_state['values']['veranstaltung_nid'];$reservation->field_reservation_vorname[0]['value'] = $form_state['values']['vorname'];$reservation->title = $form_state['values']['name'];$reservation->taxonomy = array($form_state['values']['tickettyp'], array_shift(taxonomy_node_get_terms_by_vocabulary($veranstaltung, 1)));node_save($reservation);?>http://drupal.org/node/408540Drupal 5<?php$reservation = new StdClass();$reservation->type = 'reservation';$reservation->field_reservation_tickets[0]['value'] = $form_state['values']['anzahl_tickets'];$reservation->field_reservation_email[0]['value'] = $form_state['values']['email'];$reservation->field_reservation_telefon[0]['value'] = $form_state['values']['telefon'];$reservation->field_reservation_veranstaltung[0]['nid'] = $form_state['values']['veranstaltung_nid'];$reservation->field_reservation_vorname[0]['value'] = $form_state['values']['vorname'];$reservation->title = $form_state['values']['name'];$reservation->taxonomy = array($form_state['values']['tickettyp'], array_shift(taxonomy_node_get_terms_by_vocabulary($veranstaltung, 1)));node_submit($reservation); // This step is needed only in Drupal 5, not in Drupal 6.node_save($reservation);?>Basic Drupal 6 Form<?phpfunction woof_form($title, $body) { $form['wooftitle'] = array( '#type'=> 'textfield', '#title' => t('Woof'), '#default_value' => $title, '#required' => TRUE, ); $form['woofbody'] = array( '#type'=> 'textarea', '#rows' => '20', '#title' => t('Woof'), '#default_value' => $body, '#required' => TRUE, ); $form['submit'] = array( '#type'=> 'submit', '#title' => t('Speichern'), '#default_value' => 'Speichern', '#required' => TRUE, ); return $form;}?>Form alter<?phpfunction fflyr_form_alter(&$form, $form_state, $form_id) { if ($form_id == 'flyer_node_form') { unset($form['buttons']['preview']); unset($form['body_field']['format']); unset($form['body_field']['teaser_include']); } }?><?phptaxonomy_node_get_terms_by_vocabulary();taxonomy_node_get_terms();?>render a block<?php$block = module_invoke('block', 'block', 'view', $block_id);print $block['content'];?>Handling ISO date with MySQL<?php$where[] = " DATE_FORMAT(STR_TO_DATE(field_date_value , '%%Y-%%m-%%dT%%H:%%i:%%s'), '%%Y-%%m-%%d') = '".date('Y-m-d')."' ";?>Populate CCK Node Reference Field and hide it Drupal 6<?phpfunction yourmodule_form_alter(&$form, $form_state, $form_id) { $form['#after_build'][] = 'yourmodule_after_build';}function yourmodule_after_build($form, &$form_state) { switch ($form['form_id']['#value']) { case 'your_form': $form['field_event_portal_nid']['nid']['nid']['#value'] = (int) arg(3); $form['field_event_portal_nid']['#access'] = false; break; } return $form;}?>Drupal 6<?phpuser_is_logged_in();?>useful in hook_menu<?php$items['dashboard/overview'] = array( 'title' => t('My Dashboard'), 'page callback' => 'page_dashboard', 'access callback' => 'user_is_logged_in',);?>CCK Checkbox PHP Code with correct Label display<?phpreturn array(0=>'Image as Popup', 1=>'Image as Popup');?>Date Field for a form Drupal 6<?php$date = '2008-12-31'; // Provide a default date in the format YYYY-MM-DD HH:MM:SS.// Provide a format using regular PHP format parts (see documentation on php.net).// If you're using a date_select, the format will control the order of the date parts in the selector,// rearrange them any way you like. Parts left out of the format will not be displayed to the user.$format = 'm/d/Y';$form['my_date_field'] = array('#type' => 'date_text', // types 'date_popup', 'date_select', 'date_text' and 'date_timezone' are supported. See .inc file.'#title' => t('Select a date'),'#default_value' => $date,'#date_format' => $format,'#date_label_position' => 'within', // 'above', 'within' or 'none', default is 'above''#date_timezone' => 'America/Los Angeles', // Optional, if your date has a timezone other than the site timezone.'#date_increment' => 15, // Optional, used by the date_select and date_popup elements to increment minutes and seconds.'#date_year_range' => '-3:+3' // Optional, used to set the year range (back 3 years and forward 3 years is the default).);?>http://drupal.org/node/290758 Avoid wrong count query on theme_pager. remove newlines and whitespaces before using pager_query<?php$sql = str_replace(array("\n", "\r", "\t"), ' ', $sql);$result = pager_query($sql);?>Preserve Menu context<?php$item = menu_get_item();if ($item) { $item['href'] = 'job-vacancies'; //Menueintrag (Pfad) unter dem der Node angezeigt werden soll menu_set_item(NULL, $item);}?>pragmatically change user form<?phpfunction custom_theme(){ $register['user_profile_form'] = array( 'arguments' => array('form' => NULL), //'template' => 'user-profile-form', ); return $register;}function custom_user_profile_form($form) { $output = theme_part(t('Benutzer erfassen/bearbeiten'), drupal_render($form)); return $output;}?>how to change nodereference options title or value or whatever<?phpfunction custompresseberichte_form_alter(&$form, $form_state, $form_id) { if ($form_id == 'pressebericht_node_form') { $form['field_pb_standorte_rel']['#pre_render'] = array('custompresseberichte_form_pre_re