Drupal

Categories:

Remove "Split summary at cursor" button

function foo_form_alter(&$form, $form_state, $form_id) {
  if ($form['#id'] == 'node-form') {
    if (!empty($form['body_field'])) {
      $form['body_field']['teaser_js']['#type'] = 'hidden';
      $form['body_field']['teaser_include']['#type'] = 'hidden';
    }
  }
}

Credit Snipplr.com

Add items to cart with dynamically set attributes

You first create some attributes for a product that have no options and are set as text field, then add to cart with their attribute IDs set.

<?php
	uc_cart_add_item(95, 1, array('attributes' => array(3 => $form_state['values']['property_name'], 2 => $form_state['values']['property_nid'])));
?>

Form alter a text field to be readonly the ghetto way

<?php
 
$form['Association_Name']['#title'] = $form['field_association_name']['#title'];
		$form['Association_Name']['#value'] = "<label><strong>Association Name:</strong></label><br />" . $form['field_association_name'][0]['#default_value']['value'];
		$form['Association_Name']['#weight'] = $form['field_association_name']['#weight'];
		$form['field_association_name']['#access'] = 0;
 
?>

Get current path

$path = drupal_get_path_alias($_GET['q']);

Drupal form Onfocus attribute

$form['search_theme_form']['#attributes'] = array('onfocus' => "if (this.value == 'Search My Site') {this.value = '';}" );

Make Ubercart Order Comment

This example makes an admin comment

uc_order_comment_save($order->order_id, 0, $str, 'admin');

Standard cron job for Drupal

Hourly cron job:

crontab -e
* 1 * * * /usr/bin/wget -O - -q -t 1 http://www.domain.com/cron.php

Assign user to a role programmatically

<?php
function _assign_user_role($order, $account) // This assigns the user to "Consumer" role.
{
	$usr = user_load($order->uid);
	$details = array(
		'uid' => $order->uid,
		'roles' => array(
			6 => 'Consumer' // 6 is the role ID, may need to update this on going live
			)
		);
	user_save($usr, $details);
}
?>

Add conversion script to Ubercart's checkout completion page

<?php
 
function phptemplate_preprocess_page(&$vars) {
 
	if(drupal_get_path_alias($_GET['q']) == 'cart/checkout/complete') {
 
		$vars['closure'] .= 'conversion script';
 
	}
}
 
?>

Output Footer links from primary nav links

<?php
	$links = menu_primary_links(); 
	foreach($links as $link):
		$x++;
		echo('<a href="' . url($link['href']) . '">' . $link['title'] . '</a> ' . ((count($links) == $x) ? "" : "|") );
	endforeach;
?>

or

<?php
$links = menu_navigation_links("primary-links");
	foreach($links as $link):
		$x++;
		echo('<li><a href="' . url($link['href']) . '">' . $link['title'] . '</a></li> ');
	endforeach;
?>