03
Apr, 2015

0 Comments | Submitted by Gurumoorthi

This blog describes how to hide "Promoted to front page" and "Sticky at top of lists" options from node editing page in Drupal. When adding or editing a node, you can see "Publishing options" at bottom of the page which contains 'Published', 'Promoted to front page' and 'Sticky at top of lists' checkbox options. The below image shows you the three options:
 
   Disable promoted to front page and sticky at top of lists options
 
The "Published" option is used to publish the content. The "Promoted to front page" option is used to display content in the front page. The 'Sticky at top of lists' option is used to keep the content sticked to the top of front page. If you don't need to show "Promoted to front page" and "Sticky at top of lists" options, then you can hide those options easily. You can hide these options by altering the form either using either hook_form_alter() or hook_form_form_id_alter().
<?php
/**
 * Implement hook_form_FORM_ID_alter().
 */
function kf_form_article_node_form(&$form, $&form_state) {
  // hide promoted to front page option
  if (isset($form['options']['promote'])) {
    $form['options']['promote']['#access'] = FALSE;
  }

  // hide sticky at top of lists option
  if (isset($form['options']['sticky'])) {
    $form['options']['sticky']['#access'] = FALSE;
  }
}
?>

Read more →

25
Mar, 2015

0 Comments | Submitted by Gurumoorthi

This blog describes how to solve PDOException - SQLSTATE[22003] - Numeric value out of range: 1264 Out of range. When you try to store large integer value in 'integer' field type, then you will get this error. It is because the maximum value of 'integer' field type is exceeded. 
For example, you want to store phone number in a content type. Then you may create new field with 'integer' field type in the content type. When you store 10 digit phone number in this field, "PDOException error" will be shown. MySQL allows you to store values from -2147483648 to 2147483647 in integer field type if signed, so you can't store phone number in 'integer' field type. MySQL allocates 4 bytes for 'integer' field type ie.,
4 bytes = 4 x 8 = 32 bits
2^32 => 4294967296 values
2^32 - 1 => 4294967295 (if unsigned, 0 to 4294967295 values)
-2^(32 - 1) to (2^(32 - 1) - 1) => -2147483648 to 2147483647 (if signed, -2147483648 to 2147483647 values).
If you want to store large integer value, then you need to use field type either 'bigint' or 'text field'. If you choose 'bigint', then there is no need to add custom validation for phone number field. On the other hand, if you choose 'text field' then you need to add custom validation using either hook_form_alter() or hook_form_form_id_alter().
<?php
/**
 * Implement hook_form_alter()
 */
function kf_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'your_form_id') {

Read more →

25
Mar, 2015
vishnu's picture

0 Comments | Submitted by vishnu

If you know the file id, it is really simple,
Code:

$file = file_load($fid);
file_move($file, 'public://new_file_name);

How it works:

We need a source file object to move file to the new location and update the files database entry. Moving a file is performed by copying the file to the new location and then deleting the original. To get source file object, we can use file_load() function.
file_load($fid) - loads a single file object from the database
$fid - file id
return object representing the file, or FALSE if the file was not found.

After that, we can use file_move() function to move the file new location and delete the original file..

file_move($file, 'public://new_file_name)

Parameter1 - Source File Object
Parameter2 - A string containing the destination that $source should be moved to. This must be a stream wrapper URI.
Parameter3 - Replace behaviour (Default value : FILE_EXISTS_RENAME)

For more details, check here.
 

Read more →

19
Mar, 2015
vishnu's picture

0 Comments | Submitted by vishnu

Steps for programmatically creating node in drupal7,
1. Create a new node object.
2. Save the object using the node_save() function.

Basic Node Creation :

$complaint_body = 'Your node complaint body text';
$node = new stdClass();  // Create a new node object
$node->type = 'company';  // Content type
$node->language = LANGUAGE_NONE;  // Or e.g. 'en' if locale is enabled
node_object_prepare($node);  //Set some default values

$node->title = 'Your node title';
$node->body[$node->language][0]['value'] = $complaint_body;
$node->body[$node->language][0]['summary'] = text_summary($complaint_body);
$node->body[$node->language][0]['format'] = 'full_html';

$node->status = 1;   // (1 or 0): published or unpublished
$node->promote = 0;  // (1 or 0): promoted to front page or not
$node->sticky = 0;  // (1 or 0): sticky at top of lists or not
$node->comment = 1;  // 2 = comments open, 1 = comments closed, 0 = comments hidden
// Add author of the node
$node->uid = 1;
// Set created date
$node->date = 'complaint_post_date';
$node->created = strtotime('complaint_post_date');

$path = 'content/mytest-' . date('YmdHis');
$node->path = array('alias' => $path);
// Save the node
node_save($node);

Add custom fields

Read more →

19
Mar, 2015

1 Comments | Submitted by Gurumoorthi

      This blog describes how to hide and override drupal status messages while creating and editing nodes.

      When you create or edit the nodes, drupal displays status messages like 'node has been created' and 'node has been updated'. Some may not be interested to view these messages. You can hide these messages using drupal_get_messages(). It returns all messages that have been set using drupal_set_message(). You need to add it to a custom submit handler for the corresponding form. You can add custom submit handler using either hook_form_alter() or hook_form_form_id_alter() in drupal. 

       To hide drupal status messages, use the code below. For example, i have hidden the status messages only for article node form.

<?php
/**
 * Implement hook_form_alter()
 */
function kf_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'article_node_form') {
    // to add custom submit handler
    $form['actions']['submit']['#submit'][] = 'kf_article_form_submit';
  }
}

/**
 * Implement kf_article_form_submit
 */
function kf_article_form_submit($form, &$form_state) {
  // to hide drupal status messages
  drupal_get_messages('status');
}
?>

       Similarly you can override these status messages. For that, you need to add messages after drupal_get_messages() in the custom submit handler.

Read more →

Subscribe to RSS - Drupal 7