Skip to content

Instantly share code, notes, and snippets.

@americkson
Last active July 8, 2019 15:49
Show Gist options
  • Save americkson/1173714 to your computer and use it in GitHub Desktop.
Save americkson/1173714 to your computer and use it in GitHub Desktop.
Drupal Goodies

A collection of Drupal snippets.

Home / Front Page Tricks

Remove the Default message on home page with our creating a post

<?php if(drupal_is_front_page()) {unset($page['content']['system_main']['default_message']);} ?>

No H1 title on home page

<?php if ($title && !drupal_is_front_page()): ?>

Debugging

Show when/where it is called

<?php krumo(); ?> 

Show in $messages

<?php dsm(); ?>
<?php dprint_r(); ?>  

#Themeing

Hide $title on Event pages

  function THEME_preprocess_page(&$vars) {
  $vars['show_title'] = !isset($vars['node']) || (isset($vars['node']) && $vars['node']->type !== 'event');  
}

Allow the theme to find template files for content types

function THEME_preprocess_page(&$vars) {
  
  // Do we have a node?
  if (isset($vars['node'])) {

    // Refernece suggestions.
    $suggests = &$vars['theme_hook_suggestions'];

    // Get path arguments.
    $args = arg();
    // Remove first argument of "node".
    unset($args[0]);

    // Set type.
    $type = "page__type_{$vars['node']->type}";

    // Bring it all together.
    $suggests = array_merge(
      $suggests,
      array($type),
      theme_get_suggestions($args, $type)
    );

    // if the url is: 'http://domain.com/node/123/edit'
    // and content type is 'blog'..
    //
    // This will be the suggestions:
    //
    // - page__node
    // - page__node__%
    // - page__node__123
    // - page__node__edit
    // - page__type_blog
    // - page__type_blog__%
    // - page__type_blog__123
    // - page__type_blog__edit
    //
    // Which connects to these templates:
    //
    // - page--node.tpl.php
    // - page--node--%.tpl.php
    // - page--node--123.tpl.php
    // - page--node--edit.tpl.php
    // - page--type-blog.tpl.php          << this is what you want.
    // - page--type-blog--%.tpl.php
    // - page--type-blog--123.tpl.php
    // - page--type-blog--edit.tpl.php
    //
    // Latter items take precedence.
  }
  
}

Call in a VIEWS block in to page.tpl.php - D7

  <?php echo views_embed_view('VIEW_NAME', 'BLOCK_NAME'); ?>
<?php
  // To get the 'BLOCK NAME' check the 'configure' URL on admin/structure/block 
  $block = module_invoke('views', 'block_view', 'BLOCK NAME');
  print render($block['content']);
?>

Views

Contextual Filters PHP - filter view by same text value

if (arg(0) == 'node' && is_numeric(arg(1))) {
  $node = node_load(arg(1));
return $node->FIELD_NAME['xxx'][0]['value'];
} else {
  return FALSE;
}
@dapseen
Copy link

dapseen commented Sep 3, 2017

Great stuff you have here.. Still trying to understand the last snippets about views

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment