Skip to content

Instantly share code, notes, and snippets.

@axooh
Created December 22, 2012 14:45
Show Gist options
  • Save axooh/4359146 to your computer and use it in GitHub Desktop.
Save axooh/4359146 to your computer and use it in GitHub Desktop.
Drupal template functions
// Prepare files from content type field
function prepare_files($node) {
$files = field_get_items('node', $node, 'field_attachments');
// break up and return nothing, if there are no files
if (empty($files)) {
return NULL;
}
// iterate through the links and render the output
foreach ($files as $file) {
if (empty($file['description'])) {
$file_title = $file['filename'];
}
else {
$file_title = $file['description'];
}
$file_type = array_pop(explode('/', file_get_mimetype($file['uri'])));
$file_size = format_size($file['filesize']);
$file_array[] = array(
'href' => file_create_url($file['uri']),
'title' => $file_title.' (' .$file_type.', '.$file_size.')',
);
}
// return the array with all the links
return $file_array;
}
// Render files as links
print theme('links', array('links' => $file_array));
// Example output
//
// <ul>
// <li>File number one (pdf, 23 KB)</li>
// <li>File number two (doc, 1.4 MB)</li>
// </ul>
$image_path = $field_header_image[0]['uri'];
// Basic Image
print theme('image', array('path' => $image_path));
// Image with several attributes
$variables = array(
'path' => $image_path,
'alt' => 'Test alt',
'title' => 'Test title',
'width' => '50%',
'height' => '50%',
'attributes' => array('class' => 'some-img', 'id' => 'my-img'),
);
print theme('image', $variables);
// Render image with image style (e.g. scaled and/or cropped)
print theme('image_style', array('path' => $image_path, 'style_name' => 'content_box_header_image'));
// HINT: Check if the sites/default/files/styles folder is writable!
/**
* Include a local settings file if it exists.
* http://drupal.org/node/1118520
*/
$local_settings = dirname(__FILE__) . '/settings.local.php';
if (file_exists($local_settings)) {
include $local_settings;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment