Skip to content

Instantly share code, notes, and snippets.

@vejnoe
Last active August 29, 2015 14:17
Show Gist options
  • Save vejnoe/7fec42070817517d029d to your computer and use it in GitHub Desktop.
Save vejnoe/7fec42070817517d029d to your computer and use it in GitHub Desktop.
Drupal 7 Theming Snippets

#Drupal 7 Theming Snippets By Andreas Vejnø Andersen – vejnoe.dk

##Image node.tpl.php

URL to nodes:

<?php print $node_url; ?>
<?php print $url('node/10'); ?>

Image url:

<?php print file_create_url($field_image[0]['uri']); ?>

Hide $fields

<?php hide($content['field_image']); ?>

Admin edit buttons:

<?php if ($is_admin) {
  // Translate btn
  print '<a href="'; if ($language != 'da') { print '/' . $language; } print '/node/' . $nid . '/translate" class="button tiny translate">' . t('Translate') .'</a>';
  // Edit btn
  print '<a href="'; if ($language != 'da') { print '/' . $language; } print '/node/' . $nid . '/edit?destination=' . current_path() . '" class="button tiny edit">' . t('Edit') .'</a>';
} ?>

Print only body field:

<?php print render($content['body'][0]); ?>

Substring

substr($field_website[0]['value'], 0, 4) == 'http'

If teaser:

<?php if ($teaser): ?>

<?php else: ?>

<?php endif; ?>

Loops

while($i < sizeof($field_image)) {
  print '<div class="image">';
    print render($content['field_image'][$i]);
    if ($field_image[$i]['alt'] != '') {
      print '<p class="description">' . $field_image[$i]['alt'] . '</p>';
    }
  print '</div>';

  $i++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment