Fixing Drupal’s Clearfix CSS

Drupal’s default clearfix CSS leaves a gap at the bottom of the page, this removes this:


.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
font-size: 0;
}

.clearfix { display: inline-block; }
/* \*/
* html .clearfix { height: 1%; }
.clearfix { display: block; }
/* */

Drupal 7 – Starting point for creating a file object programatically

If you need to create a file object through the Drupal api then use the following code:


filename = basename('{CURRENT PATH}');
$file->filepath = '{CURRENT PATH}';
$file->filemime = file_get_mimetype($file->filename);
$file->filesize = filesize('{CURRENT PATH}');
$file->uid = $user->uid;
$file->status = FILE_STATUS_TEMPORARY;
$file->timestamp = time();
$file->list = 1;
$file = file_save($file);
$file = file_move($file, 'public://');
$file->status = FILE_STATUS_PERMANENT;
$file = file_save($file);

Drupal 7 – Starting point for programmatically creating a node

If you need to create a node within your module then use the following code:


title = '{NODE TITLE}';
$node->type = '{NODE TYPE}';
node_object_prepare($node);
$node->language = LANGUAGE_NONE;
$node->uid = $user->uid;
$node->status = 1;
$node->promote = 0;
$node->comment = 0;
$node = node_submit($node);
node_save($node);

Define a Drupal 7 Block in a module

The following code is used if you want to define a new block within a Drupal module. Replace hook_ with your modules name.


t('Block Description'),
);
return $blocks;
}

function hook_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'block-name':
$block['content'] = hook_custom_function();
break;
}
return $block;
}

function hook_custom_function(){
$content = "Block Content";
return $content;
}

Drupal 7 Skip to First Search Result if Only One

If you want to alter the default Drupal 7 search so that it skips to the URL of the first result if there is only one then add the following code to your template.php

Change <template> with your template name.

 

 

 

 

function <template>_preprocess_search_results(&$variables) {

$resultcount =0;
foreach ($variables[‘results’] as $result) {
$resultcount += 1;
}

if($resultcount == 1){
drupal_goto($variables[‘results’][0][‘link’]);
}
}

 

Alter the node add form in Drupal 7

If you want to change the Save button text to Submit in Drupal 7 add the following to your template.php file.

Simply replace YOURTHEME with the name of your theme and CONTENTTYPE to the machine name of your content type.

 

 

 

 

function YOURTHEME_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == ‘CONTENTTYPE_node_form’) {
$form[‘actions’][‘submit’][‘#value’] = t(‘Submit’);
}
}