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.