Drupal Code Snippet
- Sean Gibson
- Feb 9, 2018
- 3 min read
Code snippets:
Code snippets in Drupal are a form of coding as requisite when you are planning to perform a function or action that compulsorily needs implementation.
Custom queries:
If you are doing a custom query yourself, there is only one important thing you need to do: put the build_mode key on an object you pass to a theme function. In this example, we are using a node object.
$output = '';
$result = db_query("SELECT nid FROM {node} WHERE whatever");
while ($row = db_fetch_object($result)) {
// Load the node - You can also built the object yourself if you know which fields you need.
$node = node_load($row->nid);
// Put a build_mode key on the $node object
$node->build_mode = 'teaser';
// Check the teaser flag and show_links flag.
$teaser = ($node->build_mode != 'full') ? TRUE : FALSE;
$show_links = ds_show_field('nd', $node->type, $node->build_mode, 'links');
// Use node_view to render.
$output .= node_view($node, $teaser, FALSE, $links);
}
return $output;
Node displays fields
Because we cannot include every single field out there by default, here are some PHP snippets you can use to create a custom field at admin/ds/module/fields or include in your own implementation of hook_ds_fields(). Many snippets, especially those from Node displays, are single prints which are usually found in $links variable available by default as your field.
If you are using functions defined in modules, it is advisable to wrap the code around the module_exists function. Take a look how we return the terms field when taxonomy enables in nd.module for inspiration.
Note: replace $object with $entity in the Drupal 7 Development version
Drupal Coding Methods
Print the comment count:
print $object->comment_count;
Print the node view stats
$statistics = statistics_get($object->nid);
if ($statistics) {
print format_plural($statistics['totalcount'], '1 read', '@count reads');
}
or
echo $object->links['statistics_counter']['title'];
Flags
Read http://drupal.org/node/295383 and replace $node (or $user) by $object in the flag_create_link() function.
Webform
return $object->content['webform']['#value'];
Organic Groups integration:
OG descriptions
return $object->og_description;
OG mission
return $object->body;
OG group post
return l('Add something', 'node/add/nodetype', array('query' => array('gids[]' => $object->nid)));
OG User groups (for UD)
return $object->content['summary']['groups']['#value'];
OG groups list
Youi will need to create a preprocessor field and use 'og_links' as the key and 'view' as the variable key.
return $object->content['image']['#value'];
print $object->content['print_links']['#value'];
Note that on the settings page of print you need to select 'Content corner' at Printer-friendly page link.
You will also have to reset the css a bit since it aligns right, but other than that, the link will show up (whether it is an icon or link)
Node displays: hide the title:
When looking at a full node, the page template will also print the $title variable. It is possible however to display the title also with ND and you might wish to put this in the available regions. The following snippet is an implementation of hook_preprocess_page, which you can use in either template.php or in one of your own custom modules to hide title in page.tpl.
function template_preprocess_page(&$variables) {
if ($node = menu_get_object()) {
// Some extra checks: nid, type and arg(2) also being empty.
if ($node->nid && $node->type == 'my_content_type' && arg(2) == '') {
$variables['title'] = '';
}
}
}
Drupal Methods of Programming
Limit multiple field:
When using views, you can choose if you want to limit the number of values of a field, e.g: from an image field. This is not possible by default and hard to implement since we have to override all theming functions defined by cck. We are not going to do that for now. There is an easy way though to add new formatters. Code underneath adds a new formatter for an imagefield which uses a custom function in the 'imagecache_for_nd_contrib' module.
/**
* @file
* Custom functions for this site.
*/
/**
* Implementation of CCK's hook_field_formatter_info().
*/
function imagecache_for_nd_contrib_field_formatter_info() {
$formatters = array(
'image_single' => array(
'label' => t('Image single'),
'field types' => array('filefield'),
'description' => t('Displays image files single in a format.'),
),
);
return $formatters;
}
/**
* Implementation of hook_theme().
*/
function imagecache_for_nd_contrib_theme() {
return array(
'imagecache_for_nd_contrib_formatter_image_single' => array(
'arguments' => array('element' => NULL),
),
);
}
/**
* Only show the first image with a specific image cache.
*/
function theme_imagecache_for_nd_contrib_formatter_image_single($element) {
static $images = array();
$nid = $element['#node']->nid;
// Stop after the first one.
if (isset($images[$nid])) {
return;
}
$images[$nid] = TRUE;
if (empty($element['#item']['fid'])) {
return '';
}
$item = $element['#item'];
$alt = isset($item['data']['alt']) ? $item['data']['alt'] : '';
$title = isset($item['data']['title']) ? $item['data']['title'] : NULL;
return theme('imagecache', 'uc_category', $item['filepath'], $alt, $title);
}
Comments