cluesshop.com

Tuesday, 22 July 2014

how to call default image in drupal 7 using location

$backgroundimage="public://privateevents_media/overview/defaultbackground.png";
                                            --- filename-------------------/--image------------
example: loaction

Default/file/filename



Friday, 11 July 2014

function to ceate file url


 Function to create file url in drupal 7

public static function getImagPath($imageid){
        $image = file_load($imageid);
       $imageurl = file_create_url($image->uri);
       return $imageurl;
    }
 

Thursday, 3 July 2014

Add mutilpe image gallery custom drupal 7 with ajax

 /Implementing the hook menu//

 function gallery_menu() {
    $items = array();
    $items['gallerysadd'] = array(
        'title' => 'Test Module WWC',
        'page callback' => 'wwc_test_gallery',
        'access callback' => 'user_access',
        'access arguments' => array('access content'),
    );
    return $items;

}


//calling the  drupal form

function wwc_test_gallery(){
    return(drupal_get_form('upload_Gallery_form'));
    }


//custome  the form

function upload_Gallery_form($form, &$form_state){
    
$form['private_gallery'] = array(
        '#prefix' => '<div id="privategallery-fieldset-wrapper">',
        '#suffix' => '</div>',
        '#type' => 'fieldset',
        '#title' => t('Gallery'),
    );

    if (empty($form_state['num_private_gallery'])) {
        $form_state['num_private_gallery'] = 1;
    }

    for ($i = 0; $i < $form_state['num_private_gallery']; $i ++) {
        $form['private_gallery']['privategallery' . $i] = array(
            '#name' => 'files[privategallery' . $i . ']',
            '#type' => 'managed_file',
            '#title' => t(''),
            '#upload_location' => 'public://cruisetemplates/',
            '#default_value' => $privategalleryArray[$i]
        );
    }

    $form['private_gallery']['privateevent_addgallery'] = array(
        '#name' => 'private_gallery',
        '#type' => 'submit',
        '#value' => t('Add one more Gallery'),
        '#submit' => array('ajax_add_private_gallery_submit'),
        '#ajax' => array(
            'callback' => 'ajax_add_private_gallery_callback',
            'wrapper' => 'privategallery-fieldset-wrapper'
        )
    );

        return $form;   
}
//calling the  ajax function when ajax and retun the  form
function ajax_add_private_gallery_callback($form, $form_state) {
    return $form['private_gallery'];
}
//submit rebuit the  form
function ajax_add_private_gallery_submit($form, &$form_state) {
    $form_state['num_private_gallery'] ++;
    $form_state['rebuild'] = TRUE;
}

Monday, 30 June 2014

Drupal 7 basic db Querys

 Drupal 7 basic db_querys

 

Examples

How to do Database Queries with db_query and db_select.

SUMMARY

1. Setup

2. db_query

3. db_select


1. Setup

- Create an article node/add/article
- Create a template for it (node--[nid].tpl.php)

Now we can test queries easily inside this template:

- Create dummy content with Devel (Generate users/generate content).

2. db_query

Basic fetch

To fetch all node titles you would normally do this kind of sql statement
<?php
$sql
= 'SELECT n.title FROM node n'; ?>
In Drupal you do it like this with db_query() :
<?phpif (!$page) { print l($node->title, "node/2") ; } $result = db_query('SELECT n.title FROM {node} n'); foreach($result as $item) {
  print
$item->title;
}
?>
So pretty much the same expect curly brackets are used around the table { table } .
These add table prefix to your tables so that you can share your database across multiple sites.

Limit results

You can limit results like this in sql :
<?php
$sql
= 'SELECT n.title FROM node n LIMIT 0,20'; ?>
In Drupal you would do this:
<?php
$result
= db_query_range('SELECT n.title FROM {node} n',0,20);?>
In Drupal LIMIT is abstracted in db_query_range() because of syntax differences between different databases (like in MSSQL you have to write TOP(2) to get top two rows but in MySQL you would use LIMIT 2)

Variables

In sql you use variables like this:

<?php
$sql
= 'SELECT n.title, n.uid FROM node n WHERE n.uid = $uid LIMIT 0,20'; ?>
In Drupal you use placeholders like this:
<?php
$result
= db_query_range('SELECT n.title, n.uid FROM {node} n WHERE n.uid = :uid',0,20,
                          array(
':uid' => $uid)); ?>
Placeholders are used to add a security layer that inserts variables in a secure manner so you don't have to escape or quote them before they are added into the query.

Printing the values

I used foreach to iterate through the result set.
You could also get an array with this:
<?php
$result
= db_query_range('SELECT n.title, n.uid FROM {node} n WHERE n.uid = :uid',0,20,
                          array(
':uid' => $uid))->fetchAll();?>
You can iterate through that exactly the same way:
<?phpforeach($result as $item) {
  print
$item->title;
}
?>
If you need just one item, you can use fetchField();
<?php
$result
= db_query_range('SELECT n.title, n.uid FROM {node} n WHERE n.uid = :uid',0,20,
                          array(
':uid' => $uid))->fetchField();
print
$result; ?>

3. db_select

You can also use more flexible but slower db_select() to fetch items.
Here is how to do same thing with db_select:

Basic fetch

<?php
$result
= db_select('node','n')
          ->
fields('n',array('title'))
          ->
execute();?>
Here you don't use brackets around the table. it's handled for you.

Limit results

<?php
$result
= db_select('node','n')
          ->
fields('n',array('title'))
          ->
range(0,20)
          ->
execute();?>

Variables

<?php
$result
= db_select('node','n')
          ->
fields('n',array('title','uid'))
          ->
range(0,20)
          ->
condition('n.uid',$uid,'=')
          ->
execute();?>

Monday, 19 May 2014

drupal 7 theme hoocks

Template hoocks

template.php

function claimjockey_subtheme_preprocess_page(&$vars) {
  
// custom content type page template
  // Renders a new page template to the list of templates used if it exists
  if (isset($vars['node']->type)) {
// This code looks for any page--custom_content_type.tpl.php page
    $vars['theme_hook_suggestions'][] = 'page__' . $vars['node']->type;
  }
     
if ($vars['is_front']) {
    $vars['title'] = '';
  }
 
//  var_dump(current_path());
//  exit();
   switch (current_path()) {
      case 'user':
        $vars['title'] = t('Customer Login');
       
//        var_dump($vars['contextual_links']);
//        exit();
       
        unset($vars['tabs']);
        break;
      case 'user/password':
        $vars['title'] = t('Forgot your password?');
        unset($vars['tabs']);
        break;

      case 'node/63':
          $vars['title'] = '';
        break;
     
  } 

}

mutiple image uplaod

 custome_block.module

<?php

/**
 * Implements hook_block_info().
 */
function custom_block_block_info() {
  $blocks = array();
  $blocks['my_block'] = array(
    'info' => t('My Custom Block'),
  );

  return $blocks;
}

/**
**
 * Implements hook_block_configure().
 */
function custom_block_block_configure($delta='') {
  $form = array();

  switch($delta) {
    case 'my_block' :
      // Text field form element
      $form['text_body'] = array(
        '#type' => 'text_format',
        '#title' => t('Enter your text here in WYSIWYG format'),
        '#default_value' => variable_get('text_variable', ''),
      );

      // File selection form element
      $form['file'] = array(
        '#name' => 'block_image',
        '#type' => 'managed_file',
        '#title' => t('Choose an Image File'),
        '#description' => t('Select an Image for the custom block.  Only *.gif, *.png, *.jpg, and *.jpeg images allowed.'),
        '#default_value' => variable_get('block_image_fid', ''),
        '#upload_location' => 'public://block_image/',
        '#upload_validators' => array(
          'file_validate_extensions' => array('gif png jpg jpeg'),
        ),
      );
      break;
  }
  return $form;
}

/**
 * Implements hook_block_save().
 */
function custom_block_block_save($delta = '', $edit = array()) {
  switch($delta) {
    case 'my_block' :
      // Saving the WYSIWYG text     
      variable_set('text_variable', $edit['text_body']['value']);

      // Saving the file, setting it to a permanent state, setting a FID variable
      $file = file_load($edit['file']);
      $file->status = FILE_STATUS_PERMANENT;
      file_save($file);
      $block = block_load('custom_block', $delta);
      file_usage_add($file, 'custom_block', 'block', $block->bid);
      variable_set('block_image_fid', $file->fid);
      break;
  }
}


/**
 * Implements hook_block_view().
 */
function custom_block_block_view($delta='') {
  $block = array();

  switch($delta) {
    case 'my_block' :
      $block['content'] = my_block_view();
      break;
  }

  return $block;
}

/**
 * Custom function to assemble renderable array for block content.
 * Returns a renderable array with the block content.
 * @return
 *   returns a renderable array of block content.
 */
//function my_block_view() {
//  $block = array();
//
// 
// 
//  // Capture the image file path and form into HTML with attributes
//  $image_file = file_load(variable_get('block_image_fid', ''));
//  $image_path = '';
//
//  if (isset($image_file->uri)) {
//    $image_path = $image_file->uri;
//  }
//
//  $image = theme_image(array(
//    'path' => ($image_path),
//    'alt' => t('Image description here.'),
//    'title' => t('This is our block image.'),
//    'attributes' => array('class' => 'class_name'),
//  ));
//
//  // Capture WYSIWYG text from the variable
//  $text = variable_get('text_variable', '');
//
//  // Block output in HTML with div wrapper
//  $block = array(
//    'image' => array(
//      '#prefix' => '<div class="class_name">',
//      '#type' => 'markup',
//      '#markup' => $image,
//    ),
//    'message' => array(
//      '#type' => 'markup',
//      '#markup' => $text,
//      '#suffix' => '</div>',
//    ),
//  );
//
//  return $block;
//}


//function my_block_view($delta = ''){
//    $block = array();
//   switch ($delta) {
//    case 'slider_custom_block':
//      $block['subject'] = '';
//      $block['content'] = _bannerblocks_slider();
//      break;
// 
//  }
//  return $block;
//}


function my_block_view() {
   
    $image_file = file_load(variable_get('block_image_fid', ''));
  $image_path = '';

  if (isset($image_file->uri)) {
    $image_path = $image_file->uri;
  }
  $image = theme_image(array(
    'path' => ($image_path),
  ));

//  var_dump($image);
//  exit();
  // Capture WYSIWYG text from the variable
  $text = variable_get('text_variable', '');
     
    $bannerHtml='';
   $bannerHtml.='<div class="container-fluid">';
     $bannerHtml.='<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">';
              
                $bannerHtml.='<ol class="carousel-indicators">';
                    $bannerHtml.='<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>';
                    $bannerHtml.='<li data-target="#carousel-example-generic" data-slide-to="1"></li>';
                    $bannerHtml.='<li data-target="#carousel-example-generic" data-slide-to="2"></li>';
                    $bannerHtml.='<li data-target="#carousel-example-generic" data-slide-to="3"></li>';
                $bannerHtml.='</ol>';

                $bannerHtml.='<div class="carousel-inner">';
               
               
                    $bannerHtml.='<div class="item active">';
                    $bannerHtml.='<img src="'.base_path() . path_to_theme().'/images/banner1.jpg" alt="">';
                   $bannerHtml.='</div>';
                                    
                    $bannerHtml.='<div class="item">';
                    $bannerHtml.=$image;
                    $bannerHtml.='</div>';               
                    $bannerHtml.='<div class="item">';
                    $bannerHtml.='<img src="'.base_path() . path_to_theme().'/images/banner2.jpg" alt="">';
                    $bannerHtml.='</div>';
                   
                   
                    $bannerHtml.='<div class="item">';
                    $bannerHtml.='<img src="'.base_path() . path_to_theme().'/images/banner4.jpg" alt="">';
                    $bannerHtml.='</div>';
                   
              

                $bannerHtml.='</div>';
                $bannerHtml.='<a href="#" class="download-banner-pdf"><div class="carousel-caption downloder">';
                    $bannerHtml.='<div class="d_tag">';
                        $bannerHtml.='<p>';
                            $bannerHtml.='<span class="download-tag1">download guide</span><span class="download-tag2">: how to<br />';
                            $bannerHtml.='file a successful long term<br />';
                             $bannerHtml.='care insurance claim</span>';
                           
                        $bannerHtml.='</p>';
                        $bannerHtml.='</div>';
                    $bannerHtml.='<div class="d_image">';
                   
                       $bannerHtml.='<img src="'.base_path() . path_to_theme().'/images/downloader.png" class="img-responsive" alt="">';
                    $bannerHtml.='</div>';
                        $bannerHtml.='</div>';

              
                $bannerHtml.='<a class="left carousel-control extra"  data-slide="prev">';

                     $bannerHtml.='<img id="left_carousel" src="'.base_path() . path_to_theme().'/images/prev.png" class="img-responsive" alt="">';
                  $bannerHtml.='</a>';
                  $bannerHtml.='<a class="right carousel-control extra"  data-slide="next">';

                       $bannerHtml.='<img id="right_carousel" src="'.base_path() . path_to_theme().'/images/next.png" class="img-responsive" alt="">';
                  $bannerHtml.='</a>';
            $bannerHtml.='</div>';
           
$bannerHtml.='</div>';
   
      return $bannerHtml;
}

=============
.info
name = custom block
description = Test banner images test
core = 7.x
package =Claim Jockey

Friday, 16 May 2014