Embed a view inside a node in Drupal 7

We can embed a view created by Views module inside a node in Drupal 7 with just some short code. Here is the code:

<?php
    $view = views_get_view('your_view_name');
    $view->set_display('your_view_display_name');
    $output = $view->preview();
    // now print the view.
    if ($view->result) {
        print views_embed_view('your_view_name', $display_id = 'your_view_display_name');
    }
?>

Just put that in your node.tpl.php, and don't forget to change the views name and display name with yours.

Creating custom user login page in Drupal 7

Maybe sometimes we want a custom style for our Drupal login page, different with the existing default login page in Drupal 7, whether it's for a personal project or client requests.

Here is the simplest way to create a custom login page in Drupal 7.

First open the template.php and add the following code:

function yourthemename_theme() {
  $items = array();
  // create custom user-login.tpl.php
  $items['user_login'] = array(
  'render element' => 'form',
  'path' => drupal_get_path('theme', 'yourthemename') . '/templates',
  'template' => 'user-login',
  'preprocess functions' => array(
  'yourthemename_preprocess_user_login'
  ),
 );
return $items;
}

create a new folder "templates" and than creating user-login.tpl.php file and place it inside.

Open the user-login.tpl.php file and paste the following code:

<?php 
  print drupal_render($form['name']);
  print drupal_render($form['pass']);
  print drupal_render($form['form_build_id']);
  print drupal_render($form['form_id']);
  print drupal_render($form['actions']);
?>

add the wrapper such as div, span, etc. to test it. Don't forget to clear your Drupal cache.

Creating responsive Drupal theme (Simplified)

There are various ways to create a responsive Drupal theme, one of them is using template_preprocess_html() function in template.php.

First create template.php if your theme do not already have it. Than add below codes:

/**
* Preprocess html
*/
function themename_preprocess_html(&$vars) {

  drupal_add_css(path_to_theme() . '/css/smartphone.css', array('group' => CSS_THEME, 'media' => 'only screen and (min-width : 240px) and (max-width : 480px)', 'preprocess' => FALSE));

  $meta_viewport = array(
   '#type' => 'html_tag',
   '#tag' => 'meta',
   '#attributes' => array(
     'name' => 'viewport',
     'content' => 'width=device-width, initial-scale=1'
   )
  );

  drupal_add_html_head($meta_viewport, 'viewport');
}

It is clear if the minimum screen size of 240px and 480px maximum it will use smartphone.css, so please create smartphone.css in css folder and you can add your css code there.

Besides using template.php, you can also add the css via theme's info file like below

stylesheets[screen and (min-width: 240px) and (max-width: 480px)][] = css/smartphone.css
stylesheets[screen and (max-width: 320)][] = css/320.css

And do not forget to add meta viewport via template.php or Android devices will not recognize our responsive layout.

Clear your Drupal cache, once you make above changes.

Creating custom user registration form in Drupal 7

Creating custom user registration form sometimes can be a little tricky, but lets try to create a custom registration form in Drupal 7 with just a few steps.

First we add a few lines of codes to template.php as follows

function themename_theme($existing, $type, $theme, $path){
  $hooks['user_register_form']=array(
    'render element'=>'form',
    'template' =>'templates/user-register',
  );
return $hooks;
}

function themename_preprocess_user_register(&$variables) {
  $variables['form'] = drupal_build_form('user_register_form', user_register_form(array()));
}

Then creating the user-register.tpl.php and place it inside templates folder in your theme, write the following in your user-register.tpl.php:

<?php 
  print render($form['form_id']);
  print render($form['form_build_id']);
  print render($form['account']['name']);
  print render($form['account']['mail']);
  print render($form['field_yourcustomfield']); // Your Drupal 7 profile field if any.
  print drupal_render($form['actions']); 
?>

Clear your Drupal cache and visit your user registration form to see the changes.