Monday, May 30, 2011

Essential modules for Ubercart

Here are some essential modules for ubercart you need to install if you want
a store developed using ubercart. You can download these modules from http://drupal.org.

Monday, May 23, 2011

How to build a drupal 7 template

Site Building :
With Drupal 7, you get a new and much nicer administrative

interface than Drupal 6|5|4 etc. The admin interface uses a new theme, Seven which is preety nice.

Structure:
Now create a folder into sites/all/themes folder then an .info file

inside that folder to describe the theme to Drupal, and then make some CSS

stylesheets to insert your own styles.
Your theme's info file name must exactly match with you theme folder name.

.info file content:
.info file contains inforamation related to your theme. You will need to enter into your themes .info file.


; $Id$
name = Test templat
description = This is a Test theme for Drupal.
core = 7.x
stylesheets[all][] = style.css


Theme designing:
You can see this post for theme designing for drupal 7 Drupal theme designing tutorial.

Saturday, May 21, 2011

How to display field_image in node--article.tpl.php with Drupal 7

With Drupal 7, the node type "article" comes with a field_image to upload an image.

To render the body content:
print render($content['body']); 

Then to render the image field:
print render($content['field_image']); 

Setup a new content type on install and add fields- Drupal 7 field API

Adding customs Fields on Drupal module installation:


At the moment we are building a Drupal 7 site which requires everything to be enabled
on install via modules / custom profiles. Below is a content type with some extra fields
added using Field API so you can see how they are added. This should go in the module
*.install file. We go through the standard process of setting up a content type, and call
installed_fields and installed_instances functions. You might want to rename it from
‘article’ to something else, from what I remember on the standard install profile, Drupal 7
already creates a content type called article.
drupal-field-api
Screenshot


function article_install() {
  // get the translation function relevant to our current localisation
  $t = get_t();
  // define the content type as an array (same as in hook_node_info())
  $article = array(
    'type' => 'article',
    'name' => $t('Article'),
    'base' => 'node_content',
    'description' => $t('Content type to handle articles.'),
    'body_label' => $t('Article Description'),
    'promote' => 0,
    'status' => 1,
    'comment' => 0,
  );
 
  // set default values for anything not explicitly defined in the above array
 
  $content_type = node_type_set_defaults($article);
 
  // add the body field to the content type
  node_add_body_field($content_type, 'Body');
 
  // create the content type
 
  node_type_save($content_type);
 
  variable_set('node_options_article', array('status'));
  // hide comments for this node. http://api.drupal.org/api/drupal/modules--comment--comment.module/7
  variable_set('comment_article', 'COMMENT_NODE_HIDDEN');
 
  // Hide date and author information
 
  variable_set('node_submitted_article', FALSE);
 
  // Create all the fields we are adding to our content type.
  // http://api.drupal.org/api/function/field_create_field/7
  foreach (_article_installed_fields() as $field) {
    field_create_field($field);
  }
 
  // Create all the instances for our fields.
 
  // http://api.drupal.org/api/function/field_create_instance/7
  foreach (_article_installed_instances() as $instance) {
    $instance['entity_type'] = 'node';
    $instance['bundle'] = 'article';
    field_create_instance($instance);
  }
 
  // adjust the weight so it's called after a dependant module called 'categories'
 
  $weight = db_query("SELECT weight FROM {system} WHERE name = :name", array(':name' => 'categories'))->fetchField();
  db_update('system')->fields(array(
    'weight' => $weight + 1,
  ))
  ->condition('name', 'article')
  ->execute();
}
 
 
 
function _article_installed_fields() {
  $t = get_t();
  $fields = array(
    // text field
    'article_source' => array(
      'field_name'   => 'article_source',
      'label'        => $t('Artcile Source'),
      'cardinality'  => 1,
      'type'         => 'text',
      'settings'     => array(
        'max_length'  => 1000,
      ),
    ),
    // taxonomy term reference field, referencing a vocabulary called 'authors'
 
    'article_author' => array(
      'field_name' => 'article_author',
      'type' => 'taxonomy_term_reference',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      'settings' => array(
        'allowed_values' => array(
          array(
            'vocabulary' => 'authors',
            'parent' => 0,
          ),
        ),
      ),
    ),
      // node refererence auto complete field (see the instance), referencing a content-type called 'work'
 
    'article_work_ref' => array(
      'field_name'  => 'article_work_ref',
      'label'       => $t('Work refrerence'),
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      'type'        => 'node_reference',
      'settings'    => array(
        'referenceable_types'  => array('work'),
      ),
    ),
    // a text (textarea) field
 
    'article_pullquote' => array(
      'field_name'   => 'article_pullquote',
      'label'        => $t('Pull Quote'),
      'cardinality'  => 1,
      'type'         => 'text',
      'settings'     => array(
        'max_length'  => 1000,
      ),
    ),
    // image field
 
    'article_image' => array(
      'field_name' => 'article_image',
      'label' => $t('Image'),
      'cardinality' => 1,
      'type' => 'image',
      'settings' => array(
        'default_image' => 0,
        'uri_scheme' => 'public',
      ),
    ),
    // date field (date module required)
 
    'article_date' => array(
      'field_name'   => 'article_date',
      'label'        => $t('Date'),
      'cardinality'  => 1,
      'type'         => 'date',
    ),
  );
  return $fields;
}
 
 
 
function _article_installed_instances() {
  $t = get_t();
  $instances = array(
    // instance of the text field above
    'article_source' => array(
      'field_name'  => 'article_source',
      'label'       => $t('Article Source'),
      'cardinality' => 1,
      'widget'      => array(
        'type'       => 'text_textfield',
        'settings'   => array('size' => 60),
      ),
    ),
    // instance of the taxonomy term reference field above
 
    'article_author' => array(
      'field_name' => 'article_author',
      'entity_type' => 'node',
      'label' => $t('Author'),
      'bundle' => 'article',
      'required' => FALSE,
      'widget' => array(
        'type' => 'options_select',
      ),
    ),
      // instance of the node reference 'work' auto complete field above
 
    'article_work_ref' => array(
      'field_name'  => 'article_work_ref',
      'label'       => $t('Work refrerence'),
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      'widget'      => array(
        'type'          => 'node_reference_autocomplete',
      ),
    ),
    // instance of the textarea field above
 
    'article_pullquote' => array(
      'field_name'  => 'article_pullquote',
      'label'       => $t('Pull Quote'),
      'cardinality' => 1,
      'widget'      => array(
        'type'       => 'text_textarea',
        'settings'   => array('rows' => 5),
      ),
    ),
    // instance of the image field above
 
    'article_image' => array(
      'field_name' => 'article_image',
      'label' => $t('Image'),
      'cardinality' => 1,
      'type' => 'article_image',
      'settings' => array(
        'alt_field' => 1,
        'file_directory' => 'image',
        'file_extensions' => 'png gif jpg jpeg',
        'max_filesize' => '',
        'max_resolution' => '',
        'min_resolution' => '',
        'title_field' => 1,
        'user_register_form' => FALSE,
      ),
      'widget' => array(
        'settings' => array(
          'preview_image_style' => 'thumbnail',
          'progress_indicator' => 'throbber',
        ),
      ),
      'display' => array(
        'default' => array(
          'label' => 'hidden',
          'type' => 'image',
          'settings' => array('image_style' => 'bfi_common_features_image', 'image_link' => ''),
          'weight' => -1,
        ),
        'teaser' => array(
          'label' => 'hidden',
          'type' => 'image',
          'settings' => array('image_style' => 'thumbnail', 'image_link' => 'content'),
          'weight' => -1,
        ),
      ),
    ),
    // instance of the date field above
 
    'article_date' => array(
      'field_name'  => 'article_date',
      'label'       => $t('Date'),
      'cardinality' => 1,
      'widget'      => array(
        'type'       => 'date_select',
        'settings'   => array(
          'input_format' => date_default_format('date_select'),
          'increment' => 1,
          'year_range' => '-3:+3',
        ),
        'behaviors' => array(
          'multiple values' => FIELD_BEHAVIOR_CUSTOM,
          'default value' => FIELD_BEHAVIOR_CUSTOM,
        ),
      ),
    ),
  );
  return $instances;
}
 
 
 
 
function article_uninstall() {
  // Gather all the example content that might have been created while this
  // module was enabled.  Simple selects still use db_query().
  // http://api.drupal.org/api/function/db_query/7
  $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  $result = db_query($sql, array(':type' => 'article'));
  $nids = array();
  foreach ($result as $row) {
    $nids[] = $row->nid;
  }
 
  // Delete all the nodes at once
 
  // http://api.drupal.org/api/function/node_delete_multiple/7
  node_delete_multiple($nids);
 
 
  // Loop over each of the fields defined by this module and delete
  // all instances of the field, their data, and the field itself.
  // http://api.drupal.org/api/function/field_delete_field/7
  foreach (array_keys(_article_installed_fields()) as $field) {
    field_delete_field($field);
  }
 
  // Delete our content type
 
  // http://api.drupal.org/api/function/node_type_delete/7
  node_type_delete('article');
 
  // Purge all field information
  // http://api.drupal.org/api/function/field_purge_batch/7
  field_purge_batch(1000);
}

Wednesday, May 18, 2011

How to set up Drupal 7 site

Drupal is a very powerful and robust open-source content management system.
There are  many content management systems (eg. Joomla, ModX etc .) available. But in my
opinion Drupal is the best. Drupal 7 is now more powerful than Drupal 6. Drupal's
theming layer now contains a new file named html.tpl.php which dramatically decreases
theme file's size and its complexity. Many people want to install Drupal 7 for making
their website more secured. Considering their requirement, I have found out a good video
in YouTube describing how to set up a Drupal 7 website from the scratch 
and embedded this on my site.

Monday, May 16, 2011

How to develop Drupal 7 theme

Drupal 7 theming tutorial | Drupal 7 theming | Drupal 7 theme tutorial | Drupal themes


Theming is an important part of building an attractive site. There are some
changes in Drupal 7 theming as compare to its previous version (Drupal 6) theming.
In Drupal 7 many of the css Ids for blocks defined in Drupal core have changed for
indicating the purpose of the block more clearly.


Example:
Recent blog posts
Old CSS ID (Drupal 6): block-blog-0
New CSS ID (Drupal 7): block-blog-recent

Friday, May 13, 2011

Print Comments anywhere in drupal

Drupals standard practice of printing node comments below the node can get a bit stale.
Sometimes you may want, or simply need to print them somewhere else. Wouldn’t be
great if comments and the comment form were just variables you could move around?
Well you can—thanks to Drupal 6’s phptemplate_preprocess_page function we can easily
store both the comments and the comment form in variables. The only caveat is that you
must set the comment reply form to a separate page first, or something might break.
The following snippets store the comment form and comments in variables and allow you print
them in page.tpl.php.

// Store comments and the comment form in variables
function phptemplate_preprocess_page(&$vars) {
  $vars['comments'] = $vars['comment_form'] = '';
  if (module_exists('comment') && isset($vars['node'])) {
    $vars['comments'] = comment_render($vars['node']);
    $vars['comment_form'] = drupal_get_form('comment_form',
    array('nid' => $vars['node']->nid));
  }
}

// Unset comments from node
function phptemplate_preprocess_node(&$vars) {
  $vars['node']->comment = 0;
}


Now you can print $comments and $comment_form anywhere in page.tpl.php,
and they will only print when you are viewing a node.

Version:Drupal 6

You can also download this as module for Drupal 6 Comment Display Module.

Wednesday, May 11, 2011

How to install CCK in Drupal 7


As CCK has moved to Drupal core you do not need to install CCK.
The following features are now handled by separate, dedicated projects:
  • nodereference and userreference : References project - co-maintainer needed
  • content_permissions: Field permissions module. See this issue for progress on the D7 version.
  • fieldgroup : Field group module - with much more awesomeness than in D6 !
CCK Tutorial
Visit http://drupal.org/project/cck for more information.

Drupal7 node translation

Drupal has long been a leader in the ability to present a website in multiple languages. In Drupal 7 we continue that tradition: Field translation or "content translation" made it into Drupal 7, but it's not obvious from a plain Drupal 7 install how you would use it.
This article is fundamentally about content translation, not about interface translation, which is a completely different thing in Drupal (and always has been). Interface translation is about prompts, labels, menu entries and the like, all of which are handled by the core locale module and various contrib modules like i18n. I will explain how to import basic interface translation files, however.
Drupal 6 has a node translation system which basically allows you to make "translation sets" of nodes. You start with a node in a source language (and set the language on that node), and then you can create additional translations of that node to other languages. So for example, we might start with a node in English, then use the "translate" tab to add additional nodes in different languages that are tied to that original node. That node translation system is still supported in Drupal 7, but it doesn't support translation of CCK fields, and is quite awkward in reality because you're dealing with a number of separate nodes, instead of translations of a single node.
Drupal 7 got field translation (yay!), also referred to as "content translation", which is an entirely different thing. In content translation you have a single node, but with different translations for each field on the node that should be translatable. And the body is a field. Unfortunately the title is not a field, and therefore is not translatable, but more on that later. To restate this: In Drupal 7 "content translation" there is just one node, and translatable fields on it are translated to provide the translations, but are still part of that single node. The gotcha in field translation (content translation) though is that in core it didn't get a user interface. As a result the contrib Entity Translation module improves content translation and provides a UI and works alongside the core content translation module, and it's fundamental to the approach described here.
Here is the process I went through to set up a basic multilingual site:
  1. The core field translation facilities have no user interface, so you'll need the Entity Translation module. After installing and enabling it (which also enables translation and locale modules), you may need to run update.php.
  2. You need to enable languages for your site.
    • Add languages at admin/config/regional/language
    • Download interface translation files from http://localize.drupal.org
    • Import interface translations into the configured languages at admin/config/regional/translate/import (under "Translate Interface"
    • Click "Enabled" on the languages you've configured
    • At "Detection and Selection" (admin/config/regional/language/configure), choose how you want languages to be chosen. My experience is that URL selection is the best approach, so you'll need to configure subdomains or some related approach. Note that when you use URLs you need to enter the full URL including "http://". There doesn't seem to be any error checking on this. Also remember to click "enabled" on the strategy that you choose.
    • On the same "Detection and Selection" page, in the lower section of the page under "Content language detection" select "Interface", so that the same language is chosen for content language detection as for interface language detection.
  3. Enable translation of nodes and any other entities that should be translatable at admin/config/regional/translation.
  4. Enable "Multilingual support: Enabled, with content translation" on the content type edit form under "Publishing options". For example, for a content type named "example", this would be at admin/structure/types/manage/example/edit in the "publishing options" fieldset.
  5. For each field that should be translatable, go to field settings and check "users may translate this field". For the body field of the "example" content type, this would be at admin/structure/types/manage/example/fields/body/field-settings.
  6. Now you can create and translate fields.
    • Create a node and set its language to a specific language.
    • When you've saved it a "Translate" tab will appear allowing you to translate into other configured languages.
    • Click "Add translation" to add translations for the fields that have translation enabled.
    • Make sure to publish the translation. Publication is unfortunately hidden in the collapsed-by-default "translation options" fieldset
Some gotchas regarding field translation:
  • The Title module is required to make titles translatable. This currently only works for nodes, not entities. All the work is actually being done in #924968 so you have to apply the patch from that issue.
  • Entities other than nodes are not translatable out of the box. The entity must be specifically enabled to allow its fields to be translated. das-peter will elaborate on this later.
Since I'm a newbie at the D7 translation scene, I'm sure there are errors or omissions here, so please correct this and I'll update the article based on your corrections.

Friday, April 22, 2011

Drupal 7 Field API tutorial-Create a new content type

With Drupal 7, most modules will use field API for the tasks outlined above. One example of this is Organic Groups which relies on field API instead of using custom tables. Drupal 7 coding is becoming more “precision surgery” as most of the work is done by the field API, you just need a few well placed alter statements to get the customization you need.
However, field API introduces many new concepts and because it is a new API, there are a number of “gotchas”. This writeup is a quick introduction to the new concepts and points out things that may make learning the API more challenging.

Field

A field is a data structure holding some settings. Fields must have a name and a field type.
Fields can have a cardinality; that is 'how many values will this field have?' Fields can have as cardinality: one (default), a pre-specified number or any number. This is where SQL does not shine and NoSql databases like MongoDB do. As we mentioned above, you can store arrays in MongoDB and when you query them it will use indexes (if indexes are defined). You can define storage per field but there is a default, too. Core ships with an SQL storage module, but the default is just a variable, so it's easy to change.
Note that these all are configurations that are hard to upgrade because they can change database schema, semantics, and other database specifics. This is why updating the field type is explicitly forbidden.

Instance

Attaching a Field to a Bundle results in an Instance. The instance, much like the field is an array containing various settings. The most important settings are the field name and the bundle. The instance also stores widget and formatter settings. We will get back to these settings; but for now realize that these instance settings are trivial to update. When we were developing field API and we needed to decide whether something gets into fields or instances, the important question was: Can this be updated easily?

Field attach API

The field attach API lets you deal with entities containing fields. The operations are again the usual: hook_field_attach_load, hook_field_attach_validate. The entity modules call the field attach functions directly, for example field_attach_presave('node', $node); If you are writing an entity module, this is how you interact with the field API. There is no super hook-set that the field API implements, it gets explicit calls. In turn, the field attach functions call same named hooks, for example module_invoke_all('field_attach_presave', $entity_type, $entity); The arguments are the entity type and the entity. These are real hooks, so every module can implement them.

Visit this from original site: http://drupal.org/node/707832

Thursday, April 21, 2011

Render image fields with Drupal 7

Suppose you have a site built in Drupal 7 containing an image field named field_image and you want this image inti your template page or want the image to be rendered through Style. In Drupal 6 it can be done using $node->field_image[0]['filepath'] and theme('imagecache').But in Drupal 7 it is little bit different because the File API abstracted from local file system to handle other types of storage and Imagecache is now in the core module.

First, I'm separating this into the preprocessor logic where we check if the image field exists and get the right code, and the template output where we use the finished value. The page preprocessor would probably be in your theme's template.php and look like function THEME_preprocess_page(&$vars), and your page template would look like page.tpl.php.
Fortunately, the node object is already in the page preprocessor's variables, as $vars['node']. Let's break this down a little with the available API functions:

// filename relative to files directory
// e.g. 'masthead.jpg'
$filename = $vars['node']->field_image['und'][0]['filename'];

// relative path to raw image in 'scheme' format
// e.g. 'public://masthead.jpg'
$image_uri = file_build_uri($filename);

// relative path to 'styled' image (using an arbitrary 'banner' style) in 'scheme' format
// e.g. 'public://styles/banner/public/masthead.jpg'
image_style_path('banner', $filename);

// raw URL with an image style
// e.g. 'http://mysite.com/sites/default/files/styles/banner/public/masthead.jpg'
// [passing a raw path here will return a very ungraceful fatal error, see http://api.drupal.org/api/drupal/modules--image--image.module/function/image_style_url/7#comment-12079]
$vars['masthead_raw'] = image_style_url('banner', $image_uri);

// html for a styled image
// e.g. '<img typeof="foaf:Image" src="http://mysite.com/sites/default/files/styles/banner/public/masthead.jpg" alt="" />'
$vars['masthead'] = theme('image_style', array('style_name' => 'banner', 'path' => $image_uri));


So to do something useful with this:

function THEME_preprocess_page(&$vars) {

// pull the masthead image into the page template

if (isset($vars['node']->field_image) && !empty($vars['node']->field_image['und'][0]['filename'])) {

$filename = $vars['node']->field_image['und'][0]['filename'];

$image_uri = file_build_uri($filename);

// url

$vars['masthead_raw'] = image_style_url('banner', $image_uri);

// html


$vars['masthead'] = theme('image_style', array('style_name' => 'banner', 'path' => $image_uri));


}


}

Now in your page template, you have $masthead (HTML) and $masthead_raw (URL) [with the $vars variables now being independently named] so you can do something like this in a PHP block:

<?php if ($masthead): ?>
<div id="masthead"><?php echo $masthead; ?></div>
<?php endif; ?>

Some helpful contents:
1.Setup a new content type on install and add fields- Drupal 7 field API
2.How to develop Drupal 7 theme


Friday, February 18, 2011

SMTP configuration for sending mail with Drupal.


Email is an important part of a website. Sometime we cannot rely on the default SMTP settings provided by the hosting server. In many cases I was unable to send mail because the default sendmail installation no longer functioning. Fortunately I quickly found a solution with the SMTP Authentication Support module.

This module allows Drupal to bypass the PHP mail() function and send email directly to an SMTP server. The module supports SMTP authentication and can even connect to servers using SSL if supported by PHP. 

Thursday, February 3, 2011

How to Integrate AdSense in Drupal 6

AdSense integration in Drupal 6  

Although Drupal  AdSense module is  available in Drupal website (http://drupal.org/project/adsense) , this module is only for Drupal 5 and Drupal 6. If you want to place adsense ad in your Drupal 7 site or do not want to install this module , hope this code will be useful.   

If php filter module not enabled, enable the php filter module. Create a new block and place the code below. Do not forget to change the “Pub Id”.
<?php
$adsencead="<script type='text/javascript'><!--
google_ad_client = 'pub-XXXXXXXXXXXXXXXX';
/* 160x600, created 2/2/2011 */
google_ad_slot = '3339531126';
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type='text/javascript'
src='http://pagead2.googlesyndication.com/pagead/show_ads.js'>
</script>"
;
echo
$adsencead;
?>


Change the input format from Filtered html to PHP code.



Tuesday, February 1, 2011

How to setup view in Drupal 6


Drupal view setup
Views is a powerful query builder for Drupal that allows you to fetch and present lists and tables of content  to the user in ways that are tailored to your site and your content. Views can provide output in a generic list, a table, or lists of teasers.