Archive

Archive for the ‘Drupal’ Category

Twitter Sign-in & Tweet with Drupal 7.10

  1. Download the module from http://drupal.org/project/twitter
  2. We also need Oauth module, so get it from http://drupal.org/project/oauth
  3. Unpack the modules and place it in the sites/all/modules folder
  4. Enable OAuth & Twitter in the admin/modules

5. Goto Twitter configuration page, http://10.21.1.14/drupal-7.10/admin/config/services/twitter

6. Click register your application link, will take us to the https://dev.twitter.com/apps/new page. We need to add our website URL link to Twitter applications. Note: Developer site link – https://dev.twitter.com/apps/
7. Login with twitter account credentials, mouse over on the name(Right-Top Corner). Click “My Applications”. Click “Create a new application” and we will get the below form,

8. Fill the form:

Name –The name which the user decides

Description – About website

Website – Provide the website URL/ we can give our development server IP based URL

Application Type (Mandatory) – Select  Read and Write

Callback URL (Mandatory) – The URL given in Website & add /twitter/oauth as suffix

Save and Update the configuration details.

9. We will get now the OAuth token values,

10. Enter the Consumer Key in OAuth Consumer key & Consumer Secret in OAuth Consumer secret in http://10.21.1.14/drupal-7.10/admin/config/services/twitter and click Save Configuration
11. 
If each and every Article/ Page has to be posted into Twitter Select the Node Types in the POST tab.
12.
If required default format of submitting the text into Twitter can be changed

13.
Change the basic settings of Twitter SIGN-IN if required.

14. Then Save Configuration.

15. Select the Content type selected in Step:12

16. Add the content and we have an option to post it to Twitter. Select the checkbox.

17. Click Save. Now the content is tweeted in Twitter.

Categories: Drupal

How to bring Administration Menu on the top of the page in Drupal

1. Enable Toolbar in admin/modules and click Save Configuration

Categories: Drupal

How to Enable SSL in Drupal?

STEPS:

  1. Download & Install “SMTP Authentication Supportmodule from http://drupal.org/project/smtp
  2. Configure the SMTP settings in Admin -> Configuration -> System -> SMTP Authentication Support
  3. Gmail SMTP server address: smtp.gmail.com
  4. Gmail SMTP user name: Your full Gmail address (e.g. me@gmail.com)
  5. Gmail SMTP password: Your Gmail password
  6. Gmail SMTP port: 465
  7. Gmail SMTP TLS/SSL required: yes

In Use encrypted protocol select Use SSL

–     If  Use SSL is not  appeared in drop-down, enable extension=php_openssl.dll in php.ini

8. In SEND TEST E-MAIL, Enter the mail address to test. We will get a test mail if SMTP is configured perfectly.

Categories: Drupal

Clear Drupal Cache Manually

DRUPAL 7

If we want clear the cache by default, without admin user action. Normaly we use to clear the cache by admin/performance/.
This can be indirectly achieved for nodes as below,

function [module_name]_node_update($node){cache_clear_all(‘*’, ‘cache_views_data’, TRUE);
}

For Blocks,

While creating blocks for the module we can write as,

function [module_name]_block_info() {$blocks[‘product_filter’] = array(
‘info’ => t(‘Product Filter’),
‘cache’ => DRUPAL_NO_CACHE,
);

return $blocks;
}

Categories: Drupal

Drupal – video popout option as vimeo

Steps for integration:
1. Add the below code where you have video link,

<?phpecho

‘<link href=”‘.$base_url.’/sites/all/themes/scaccarium/jquery-ui.css” rel=”stylesheet” type=”text/css”/>

<script src=”‘.$base_url.’/sites/all/themes/scaccarium/js/jquery.min.js”></script>

<script src=”‘.$base_url.’/sites/all/themes/scaccarium/js/jquery-ui.min.js”></script>

<script type=”text/javascript” src=”‘.$base_url.’/sites/all/themes/scaccarium/js/fancybox/jquery.fancybox-1.3.4.js”></script>

<link rel=”stylesheet” type=”text/css” href=”‘.$base_url.’/sites/all/themes/scaccarium/js/fancybox/jquery.fancybox-1.3.4.css” media=”screen” />

<script type=”text/javascript”>

$(document).ready(function() {

$(“.videolist”).fancybox({

“width” : “48%”,

“height” : “44%”,

“autoScale” : false,

“transitionIn” : “none”,

“transitionOut” : “none”,

“type” : “iframe”

});

});

</script>’;

$bpath = $base_url.”/keyarticle/flowplayer?href=”.$base_url.”/”.$vidval[“filepath”].”&width=540&height=304″;

$attributes = array(‘attributes’ => array(‘id’ => ‘various3’, “class”=>”videolist”));

$cont = “<li>”.l($vidval[“filename”], $bpath, $attributes).”</li>”;

?>

2.  Include all the relevant files appropriately

3. Customize the jquery.fancybox-1.3.4.js as below,

$(document).ready(function() {

$.fancybox.init();

$( “#fancybox-wrap” ).draggable();

$( “#fancybox-wrap” ).resizable();

$(“#fancybox-wrap”).resize(function() {

var height = $(“#fancybox-wrap”).height();

var width = $(“#fancybox-wrap”).width();

var height = height-0;

var width = width-0;

var newheight = height-40;

var newwidth = width-40;

var newheight = newheight + “px”;

var newwidth = newwidth + “px”;

var height = height + “px”;

var width = width + “px”;

var framename = document.getElementById(‘fancybox-frame’);

var frmname = (framename.contentDocument) ? framename.contentDocument :framename.contentWindow.document;

frmname.getElementById(‘player’).style.height = newheight;

frmname.getElementById(‘player’).style.width = newwidth;

document.getElementById(‘fancybox-frame’).style.height = height;

document.getElementById(‘fancybox-frame’).style.width = width;

document.getElementById(‘fancybox-content’).style.height = height;

document.getElementById(‘fancybox-content’).style.width = width;

});

});

4.  Also the jquery.fancybox-1.3.4.css to fix our own style,

#fancybox-wrap {

position: absolute;

top: 0;

left: 0;

padding: 20px;

z-index: 1101;

outline: none;

display: none;

background:#0B183B;

-moz-border-radius:10px;

cursor:pointer

}

#fancybox-outer {

position: relative;

width: auto;

height: auto;

padding:10px

}

#fancybox-content {

width: 0;

height: 0;

padding: 0;

outline: none;

position: relative;

overflow: hidden;

z-index: 1102;

}

#fancybox-frame {

overflow:hidden;

border: none;

display: block;

}

5. Now click the video link, we will get a popout option with draggable & resizable.

Categories: Drupal

Drupal, webform and Dynamic Checkboxes

Create a new file in the webform/components directory called dynamicselect.inc and added the function below.

<?php
function _webform_help_dynamicselect($section) {
switch ($section) {
case ‘admin/settings/webform#dynamicselect_description’:
$output = t(“A dynamic list of upcoming events.”);
break;
}return $output;} // end function _webform_help_dynamicselect($section)
?>

The form that makes the form

Next, I had to build the form element that would be used by the webform module to set up my dynamicselect element…

 This is somewhat complicated. Here is what happens:

When you create a webform you have to fill out a form with things like the form’s name and description and any other fields you might fill out to create any type of node.

In addition, you have to add the form elements, like textfields and markup. When you add the element, you are redirected to another form that asks about the element’s properties, like its key value and whether or not it is mandatory. When you create a new component you have to create the form that asks these questions…

 Maybe it would be better if I just showed you the function.

<?php
function _webform_edit_dynamicselect($currfield) {
 
  $edit_fields = array();
  $edit_fields[‘value’] = array(
    ‘#type’          => “textfield”,
    ‘#title’         => t(“Default value”),
    ‘#default_value’ => $currfield[‘default’],
    ‘#description’   => t(“The default value of the field.”) .
      “<br />” . webform_help(“webform/helptext#variables”),
    ‘#size’          => 60,
    ‘#maxlength’     => 255,
    ‘#weight’        => 0,
  );
 
  return $edit_fields;

} // end function _webform_edit_dynamicselect($currfield)
?>

That’s it. Actually, all of this is just cut and pasted from the select component. All I am doing is setting up a field where you can enter a default value for your instance of the dynamicselect component. All the other form items like key, name, description, etc. are added later by the webform module.

Just in case you are wondering, I did not make a_webform_edit_validate_dynamicselect() function because I did not need to do anything beyond the built-in validation.

Rendering the dynamicselect element

Now, I need the code to actually build the dynamicselect element when the webform is displayed.

<?php
function _webform_render_dynamicselect($component, $data = false) {
 
  $form_item = array(
    ‘#title’       => htmlspecialchars($component[‘name’], ENT_QUOTES),
    ‘#required’    => $component[‘mandatory’],
    ‘#weight’      => $component[‘weight’],
    ‘#description’ => _webform_filtervalues($component[‘extra’][‘description’]),
    ‘#prefix’      => “<div class=’webform-component-” .
      $component[‘type’] . “‘ id=’webform-component-” .
      $component[‘form_key’] . “‘>”,
    ‘#suffix’      => “</div>”,
  );
 
  // set the default value
  $default_value = _webform_filtervalues($component[‘value’]);
  if ($default_value) {
    $form_item[‘#default_value’] = $default_value;
  }
 
  // set the component options
  if ($data) { // $data is set
    $options = _dynamicselect_display_options($data);
  } else { // $data is not set
    $options = _dynamicselect_load_options();
  }
  $form_item[‘#options’] = $options;

  // set display as a checkbox set
  $form_item[‘#type’] = “checkboxes”;
 
  return $form_item;

} // end function _webform_render_dynamicselect($component)
?> 

 
Again, almost all of the code above is stripped out of the select component and simplified. If you need to build a different type of select, like a drop-down or radio buttons, you will need to change the code here.

Experienced Drupal developers may have noticed two things. First, I stripped the code that handles multiple default values. This is because I am only ever planning on setting the default value to %get[id]. Obviously, a more modular version of this component would not skip this step.

Second, you may have noticed that I added a second, optional parameter to the function:$data = false. This will make sense later when we display submission results. For now, the important line in the code above is this one.

<?php
    $options = _dynamicselect_load_options();
?>

Dynamically loading the options

The _dyanmicselect_load_options() function is what makes this component unique. It dynamically generates a list of options each time the form is loaded. Here it is.

<?php
/**
* dynamically load events that are happening in the next 13
* weeks excluding events that are not published or not open
* for registration
*/
function _dynamicselect_load_options() {$options = array();
$options[-1] = “Other”;$one_quarter = 7 * 13 * 24 * 60 * 60;
$query = “SELECT * FROM {countries}”;

$results = db_query($query, OMI_OPEN_STRING, $one_quarter, $default_value);

while ($result = db_fetch_array($results)) {
//$start = format_date($result[‘event_start’], “custom”, “n/j”);
$ccode = $result[‘ccode’];
$option = $ccode .’ – ‘. $result[‘name’];
$options[$result[‘id’]] = $option;
} // end while

return $options;

} // end function _dynamic_select_load_options()
?>

 
As you can see this function is unique to my needs. This would need to be re-written if you need to filter your options differently. In addition, you may not want to do everything inside a query. In fact, if someone was really ambitious they could turn this into a “real” webform component by implementing filters and fields, like the views module.

In the end the code above returns an associative array that becomes the checkboxes on the webform. It looks something like this.

Array
(
[-1] => Other
[1] => IND - INDIA
...
)

That’s all there is to creating the element. I don’t even have to write a custom_webform_submit_dynamicselect() function because I don’t need to change the values created by the default form functions.

Displaying the results

A very useful feature of the webform module is the fact that it stores all submissions in the database, and you can look at them in a variety of ways. However when the options are generated dynamically, displaying the submissions becomes more complicated. Here is the function for viewing one single submission.

<?php
function _webform_submission_display_dynamicselect($data,$component) {$form_item = _webform_render_dynamicselect($component,$data);// set the selected values as checked, i.e. default
foreach ((array)$data[‘value’] as $value) {
if ($value) {
$form_item[‘#default_value’][] = $value;
}
}

$form_item[‘#attributes’] = array(“disabled” => “disabled”);

return $form_item;

} // function _webform_submission_display_dynamicselect()
?>

 
Again, most of this is just a simplified version of what is done in the select component, but, as you can see, this is where the second argument, $data, for_webform_render_dynamicselect() is used. This causes the rendering function to switch tracks when building the options.
<?php
if ($data) { // $data is set
//$options = _dynamicselect_display_options($data); // customize this according to ur requirement

    $options = _dynamicselect_load_options();

  } else { // $data is not set
$options = _dynamicselect_load_options();
}
?>

 
There are several reasons that I want to display the options differently when I am showing submission results. First, I only want to show the events that were selected, not every open event in the next 13 weeks. Especially since I might be viewing the submission after the start date has passed or the event has been closed for registration. Also, I need to handle invalid data that might have gotten into the database, including events that have been deleted. Here is how I dealt with that.
<?php
function _dynamicselect_display_options($data) {$options = array();foreach ($data[‘value’] as $key => $val) {
if ($val == -1) {
$options[-1] = “Other”;
} else if ($val && ctype_digit((string)$val)) {
$event_node = node_load($val);
if ($event_node->type == “event”) {
$start = format_date($event_node->event_start,”custom”, “n/j/Y”);
$end = format_date($event_node->event_end, “custom”,”n/j/Y”);
$option = $event_node->title . ” $start – $end”;
$options[$val] = $option;
} else { // deal with deleted events
$options[$val] = “non-event id: $val”;
}
} else if ($val) { // deal with invalid values
$options[] = “invalid value: ” . check_plain($val);
}
}

return $options;

} // end function _dynamicselect_display_options($data)
?>

 

Of course, there are other ways to display the submissions, and last but definitely not least, the submission can be sent as an email. Each of these has to be handled in a similar fashion to the function above. I will not include each of those functions here, but I will include them in the code below…

PHP webform.module code
======================

In webform.module include the below code in

Function Name: function webform_webform_component_info()

Code to write :

‘dynamicselect’ => array(
‘label’ => t(‘Dynamic Select options’),
‘description’ => t(‘Allows creation of checkboxes, radio buttons, or select menus.’),
‘file’ => ‘components/dynamicselect.inc’,
‘features’ => array(
’email_address’ => TRUE,
’email_name’ => TRUE,
),
),

PHP dynamicselect.inc code
========================

In webform/components include the below code with filename dynamicselect.inc

<?php

// TODO: add CSV output someday

define(“OMI_OPEN_STRING”, “Open”);

/**
* Creates a “default value” form item to be displayed on the form for
* editing an instance of the dynamicselect component.  Other form items
* like key, name, description, etc. are added later by the webform module.
* @return
*   An array of form items to be displayed on the edit component page.
*/
function _webform_edit_dynamicselect($currfield) {

 $edit_fields = array();
 $edit_fields[‘value’] = array(
      ‘#type’          => “textfield”,
      ‘#title’         => t(“Default value”),
      ‘#default_value’ => $currfield[‘default’],
      ‘#description’   => t(“The default value of the field.”).”<br />”. webform_help(“webform/helptext#variables”),
      ‘#size’          => 60,
      ‘#maxlength’     => 255,
      ‘#weight’        => 0,
      );

 return $edit_fields;

} // end function _webform_edit_dynamicselect($currfield)

/**
* Build a form item array containing all the properties of this component.
* @param $component
*   An array of information describing the component, directly correlating to
*   the webform_component database schema.
* @return
*   An array of a form item to be displayed on the client-side webform.
*/
function _webform_render_dynamicselect($component, $data = false) {

 $form_item = array(
  ‘#title’       => htmlspecialchars($component[‘name’], ENT_QUOTES),
  ‘#required’    => $component[‘mandatory’],
  ‘#weight’      => $component[‘weight’],
  //’#description’ => $component[‘extra’][‘description’],
  ‘#prefix’      => “<div class=’webform-component-” .
   $component[‘type’] . “‘ id=’webform-component-” .
   $component[‘form_key’] . “‘>”,
  ‘#suffix’      => “</div>”,
  );
 
 // set the default value
 $default_value = $component[‘value’];
 if ($default_value) {
  $form_item[‘#default_value’] = $default_value;
 }
 
 // set the component options
 if ($data) { // $data is set
  //$options = _dynamicselect_display_options($data);
  $options = _dynamicselect_load_options($data);
 
 } else { // $data is not set
  $options = _dynamicselect_load_options($default_value);
 }
 $form_item[‘#options’] = $options;
 
 // set display as a checkbox set
 $form_item[‘#type’] = “select”;
 
 return $form_item;

} // end function _webform_render_dynamicselect($component)

/**
* Display the results of a dynamicselect submission.
* @param $data
*   An array of information containing the submission result, directly
*   correlating to the webform_submitted_data database schema.
* @param $component
*   An array of information describing the component, directly correlating to
*   the webform_component database schema.
* @return
*   Textual output formatted for human reading.
*/
function _webform_submission_display_dynamicselect($data, $component) {

 $form_item = _webform_render_dynamicselect($component, $data);
 
 // set the selected values as checked, i.e. default
 foreach ((array)$data[‘value’] as $value) {
  if ($value) {
   $form_item[‘#default_value’][] = $value;
  }
 }
 
 $form_item[‘#attributes’] = array(“disabled” => “disabled”);
 
 return $form_item;

} // function _webform_submission_display_dynamicselect()

/**
* instance of hook_help().
*/
function _webform_help_dynamicselect($section) {

 switch ($section) {
  case ‘admin/settings/webform#dynamicselect_description’:
   $output = t(“A dynamic list of upcoming events.”);
  break;
 }
 
 return $output;

} // end function _webform_help_dynamicselect($section)

/**
* dynamically load events that are happening in the next 13
* weeks excluding events that are not published or not open
* for registration
*/
function _dynamicselect_load_options($default_value) {

 $options = array();
 $options[-1] = “Other”;

 $one_quarter = 7 * 13 * 24 * 60 * 60;
 $query = “SELECT * FROM {states1}”;

 //$results = db_query($query, OMI_OPEN_STRING, $one_quarter, $default_value);
 $results = db_query($query);

 foreach ($results as $result) {
  $ccode = $result -> code;
  $option = $ccode .’ – ‘. $result -> state;
  $options[$result -> id] = $option;
 }

 /* DRUPAL 6
 while ($result = db_fetch_array($results)) {
  //$start = format_date($result[‘event_start’], “custom”, “n/j”);
  $ccode = $result[‘code’];
  $option = $ccode .’ – ‘. $result[‘state’];
  $options[$result[‘id’]] = $option;
 } // end while */
 
 return $options;

} // end function _dynamic_select_load_options()

function _dynamicselect_display_options($data) {

 $options = array();

 foreach ($data[‘value’] as $val) {
  _dynamicselect_format_option($options, $val);
 }

 return $options;

} // end function _dynamicselect_display_options($data)

function _dynamicselect_format_option(&$options, $val) {

 if ($val == -1) {
  $options[-1] = “Other”;
 } else if ($val && ctype_digit((string)$val)) {
  $event_node = node_load($val);
  //if ($event_node->type == “event”) {
  //$start = format_date($event_node->event_start, “custom”, “n/j”);
  // $end = format_date($event_node->event_end, “custom”, “n/j”);
  $option = $event_node->title . “$start – $end”;
  $options[$val] = $option;
  //} else { // deal with deleted events
  //  $options[$val] = “non-event id: $val”;
  //}
 } else if ($val && $val != “”) { // deal with invalid values
  $options[] = “invalid value: ” . check_plain($val);
 }

} // end function _dynamicselect_format_option($val)

/**
* Format the output of emailed data for this component.
*
* @param $data
*   A string or array of the submitted data.
* @param $component
*   An array of information describing the component, directly correlating to
*   the webform_component database schema.
* @return
*   Textual output to be included in the email.
*/
function theme_webform_mail_dynamicselect($data, $component) {

// NOTE: this is slightly redundant, but it is easy to
//       plug in to existing functions
 $options = array();
 foreach ($data as $val) {
  _dynamicselect_format_option($options, $val);
 }
 $output = $component[‘name’] .”:\n”;
 foreach ($data as $value) {
  if ($value) {
   $output .= ”    • ” . $options[$value] .”\n”;
  }
 }
 
 return $output;

} // end function theme_webform_mail_dynamicselect()

/**
* Return the result of this component’s submission for display in a table. The
* output of this function will be displayed under the “results” tab then “table”.
* @param $data
*   An array of information containing the submission result, directly
*   correlating to the webform_submitted_data database schema
* @return
*   Textual output formatted for human reading.
*/
function _webform_table_data_dynamicselect($data) {

 // TODO: refactor
 $options = array();
 foreach ($data[‘value’] as $val) {
  _dynamicselect_format_option($options, $val);
 }
 $output = “”;
 foreach ($data[‘value’] as $value) {
  if ($value) {
   $output .= ”    • ” . $options[$value] .”<br />\n”;
  }
 }
 
 return $output;

}

/**
* Calculate and returns statistics about results for this component from all
* submission to this webform. The output of this function will be displayed
* under the “results” tab then “analysis”.
* @param $component
*   An array of information describing the component, directly correlating to
*   the webform_component database schema
* @return
*   An array of data rows, each containing a statistic for this component’s
*   submissions.
*/
function _webform_analysis_rows_dynamicselect($component) {

// NOTE: this isn’t very informative, but since I’ve never
//       used it I’m not going to do any more work on it

 $query = ‘SELECT data, count(data) as datacount FROM {webform_submitted_data} WHERE nid = %d AND cid = %d ‘.
 ” AND data != ‘0’ AND data != ‘ ‘ GROUP BY data”;
 $result = db_query($query, $component[‘nid’], $component[‘cid’]);
 $rows = array();
 while ($data = db_fetch_array($result)) {
  $rows[] = array($data[‘data’], $data[‘datacount’]);
 }
 
 return $rows;

}

 

Categories: Drupal

Fatal error: User ‘username’ has exceeded the ‘max_questions’ resource (current value: 50000) query: INSERT INTO watchdog

For instance powweb limits max questions to 50000 queries per hour per user for a database. A work around is to add the max usernames allowed per database (which is 3 I believe) edit the drupal CMS mysql login with a rotating mysql login script where it either chooses one of these usernames randomly to login to the database or a username in a set order to log in with an array. That way is increases your queries to 150,000 per hour. Unless you have well over 500 active users at once, that should get you by for an hour pretty good. But deffinatly right now, 50,000 isn’t enough if you have 100 current users. Anything more than 500 users at once, you should be on dedicated hosting by now.

So, set up the extra usernames in your webhost, the most allowed.
Then, go into your “sites” folder within the drupal directory and open up settings.php. Locate the $db_url line and put in an array that replicates how the $db_url line is set. Here is a code snippet example

$db_user_array[] = 'mysql://username1:pass23@mysql16.powweb.com/database_drupal1'; //enter 1st user name
$db_user_array[] = 'mysql://username2:pass23@mysql16.powweb.com/database_drupal1'; //enter 2nd user name
$db_user_array[] = 'mysql://username3:pass23@mysql16.powweb.com/database_drupal1'; //enter 3rd user name
$db_url = $db_user_array[ rand( 0, ( sizeof($db_user_array) -1 ) ) ];
Categories: Drupal, PHP