Drupal 7 Code Snippets

Info I have not updated this site in 2 Years and I am about to upgrade It and move old YouTube tutorials somewhere else as they give people the wrong impression.
Tags:

ok so you have an awesome template and you want to render a menu but give it custom classes ect here you go:

<?php
 
//main-menu machine name of menu
$menu = menu_navigation_links('main-menu');
print
theme('links__system_main_menu', array('links' => $menu , 'attributes' => array('id' => 'someid', 'class' => array('links', 'inline', 'clearfix', 'someclass'))));
// or
print theme('links__system_main_menu', array('links' => $menu , 'attributes' => array('id' => 'main-menu', 'class' => array('sidebar-nav'))));
?>

Tags:

In template.php of your theme check out the contact page for the render :)

<?php
function THEMENAME_preprocess_node(&$vars, $hook) {
// this is node with id 3 example
 
if ($vars['elements']['#node']->nid == '3') {
   
drupal_add_css(path_to_theme() . '/css/simple-sidebar.css', array ('group' => CSS_THEME));
// go get some external fonts bro httpXX the xx are so this code filter wont turn it to a link
   
drupal_add_css('httpXX://fonts.googleapis.com/css?family=Play' ,array('type' => 'external'));
  }
?>

Tags:

Using get vars to set hidden value of a webform field in custom module

<?php
/**
 * Implements hook_form_alter().
 */
function agentmail_form_alter(&$form, &$form_state, $form_id) {
 
//drupal_set_message($form_id);
 
if ($form_id == 'webform_client_form_3') {
    if (isset(
$_GET['aid']) && isset($_GET['pid'])) {
     
$agent_id = trim(strip_tags($_GET['aid']));
     
$property_id = trim(strip_tags($_GET['pid']));
     
// DO YOUR OWN VALIDATION LIKE is_numeric(); isset ect
     
$theagent = user_load($agent_id);
     
$agent_mail = $theagent->mail;
     
$extrastring ="hello ".$theagent->name." this is in regards to property http://".$GLOBALS['base_url']."node/".$property_id;

     
// change the hidden fields default value
     
$form['submitted']['agentid']['#default_value']=$agent_mail;
     
$form['submitted']['propertyid']['#default_value']=$extrastring;
    } else {
     
drupal_set_message('Error there seems to be an issue please try again' , 'error');
    }
  }
}
?>

Tags:

Working with images in a theme

<?php
//dsm($node);
//var_dump($node->field_property_images_["und"]);

foreach( $node->field_property_images_["und"] as $key => $value){
//print file_create_url($value['uri']);
print theme('image_style',array('style_name' => 'tiny', 'path' => $value['uri'], 'attributes' => array('class' => 'img-responsive')));

}
?>

if you want to embed a view in to a theme or a module here is a useful snippet.

<?php
// the machine name of the view 
$viewMname = '';
// the display type eg default , block_1 ect
$dispalytype = 'default';
//Optional arguments (Contextual filters) you pass to the view () you can pass multiple but in the  Contextual tick the box under "more"
$my_arg = $node->nid;
print
views_embed_view($viewMname, $dispalytype, $my_arg);
?>

Tags:

Here is some good info on themes and theming Drupal

How to theme a individual node ?

Well there is a few ways to do this: basically you need to give Drupal a template suggestion in you active theme directory

node.tpl.php this is the default template you will be copying. but keep a copy in your theme folder don't just overweight it.

This is the template for the "content" not the full page

  1. node--4.tpl.php Themes a node with id 4
  2. node--article.tpl.php Themes all nodes of a content type with Machine name: article

Helper function

want to add custom css to a single node ? and don't want to put it in the themename.info file ?

open up your themes template .php file and put

<?php
function THEMENAME_preprocess_node(&$vars, $hook) {
   
//var_dump($vars['elements']['#node']->nid);
// assumes node id 4
 
if ($vars['elements']['#node']->nid == '4') {

  
//drupal_add_css('<a href="http://somesite.com/css/somefile.css'">http://somesite.com/css/somefile.css'</a>, array('type' => 'external'));

  
drupal_add_css(path_to_theme() . '/css/frontpage.css', array ('group' => CSS_THEME));
  
drupal_add_css(path_to_theme() . '/css/loader.css', array ('group' => CSS_THEME));
  
//drupal_add_css(path_to_theme() . '/font-awesome/css/font-awesome.min.css', array ('group' => CSS_THEME));

  
      //var_dump(drupal_get_path('theme', 'themename') . "/css/frontpage.css");
 
}
}
?>

Theme Full pages

copy your page.tpl.php and make it page--node--4.tpl.php and then you can theme a full page ,

or page--contenttype.tpl.php themes all pages with content type (Machine name contenttype , could be article , news , whatever ) but you need to do this in your template.php file

<?php
function THEMENAME_preprocess_page(&$vars) {

   
// template file called page--contenttype.tpl.php
   
if (isset($vars['node']->type)) {
       
$vars['theme_hook_suggestions'][] = 'page__' . $vars['node']->type;
    }
}
?>

Remember to flush Drupal theme cache

Tags:

Setting custom breadcrumbs in views , this is to be inserted in to a views header (global php field) .

<?php
//title , path
$breadcrumb[] = l('Home', null);
$breadcrumb[] = l('Tutorials', 'tutorials');
$breadcrumb[] .= l('Drupal 7 Code Snippets', 'drupal-code-snippets');
drupal_set_breadcrumb($breadcrumb);
?>

Tags:

here is a few basic examples in the use of database query's drupal 7

<?php
 
// use of db_query

$q = "SELECT * FROM {node} WHERE uid = :uid AND status =:status";
$result = db_query($q, array(":uid"=>$uid ,":status" => "1"));

// NUM ROWS for people use to that.
$num = $result->rowCount();

//use a foreach
foreach ($result as $row){
// don't echo but it is just an example
echo $row->nid;
echo
$row->title;

}


// use of db_insert
$id = db_insert('node')
->
fields(array(
 
'title' => 'Example',
 
'uid' => 1,
 
'created' => REQUEST_TIME,
))
->
execute();

// last insert id = the above id;

// use of db_update

$some_email = 'an-email-address';
$user_updated_id = db_update('users')
  ->
fields(array(
   
'mail' => $some_email,
  ))
  ->
condition('uid', '8', '=')
  ->
execute();



// use of a db_delete
$uid = 8;

$uid_deleted = db_delete('users')
  ->
condition('uid', $uid)
  ->
execute();
?>

Tags:

Here are a few ideas on how to use the global user object

<?php
// get the global user (logged in user)
global $user;
// user if

$uid = $user->uid;
$mail = $user->mail;

// with delv module installed
// dsm($user);

// check if user is loged in or not

if(!$uid==0){
// user is logged in
}



// user roles is an array  or not
$roles = $user->roles;

// with delv module installed
// dsm($roles);

if (in_array('3', $roles)) {
//  hello admin user
}
?>