Tuesday, June 28, 2011

Displaying Views' exposed filters in a custom area

User the following function

$view = views_get_view('your_view_name');
$view->set_display('default');
$view->init_handlers();
$form_state = array(
'view' => $view,
'display' => $view->display_handler->display,
'method' => 'get',
'rerender' => TRUE,
'no_redirect' => TRUE,
);
$output = drupal_build_form('views_exposed_form', $form_state);
return $output;

Monday, June 27, 2011

PHP – parse a string between two strings

function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}

$fullstring = "this is my [tag]dog[/tag]";
$parsed = get_string_between($fullstring, "[tag]", "[/tag]");

echo $parsed; // (result = dog)

Friday, June 24, 2011

Customizing the user profile layout

Step 1 - is to override the default User Profile page layout by uploading a custom user-profile.tpl.php* file to your active theme folder.

Drupal will automatically detect the presence of your custom user-profile.tpl.php and override the default user profile layout. To make this happen, you need to rebuild the theme registry, which you can do by clearing the caches (for example using the button on the admin/settings/performance page), or simply by visiting the admin/build/modules page.

* note that in Drupal 6.x, your custom user profile layout file name uses a hyphen, instead of an underscore.

Step 2 - is to customize your user-profile.tpl.php layout file.

By default, all user profile data is printed out with the $user_profile variable. If there is a need to break it up you can use $profile instead.

As an example, the following snippet inserted in your custom user-profile.tpl.php will display the default user profile layout.



Available variables:

$user_profile: All user profile data. Ready for print.
$profile: Keyed array of profile categories and their items or other data provided by modules.

To check for all available data within $profile, insert the following snippet at the bottom of your custom user-profile.tpl.php.




Available variables

The following is a list of variables that is available to your custom user-profile.tpl.php.

print '
'. check_plain(print_r($profile, 1)) .'
';

Step 3 - Load profile variables

If you want to load the profile variables (profile module) you can use this code in user-profile.tpl.php


profile_load_profile($account);
// now you can call the profile field like profile_firstname
echo $account->profile_firstname;

Thursday, June 23, 2011

Displaying exposed filter form for views in Drupal 6

$view = views_get_view('your_view_name');
$view->set_display('default');
$view->init_handlers();
$form_state = array(
'view' => $view,
'display' => $view->display_handler->display,
'method' => 'get',
'rerender' => TRUE,
'no_redirect' => TRUE,
);
$output = drupal_build_form('views_exposed_form', $form_state);
return $output;

Change Views Exposed Filter Form Select Element To A List Of Checkboxes

Three steps for doing this.
/**
* Implementation of HOOK_theme.
*
* @return array An array of theme hooks.
*/
function mymodule_hooks_theme() {
return array(
'select_as_checkboxes' => array(
'function' => 'theme_select_as_checkboxes'
),
);
}

/**
* Implementation of HOOK_form_alter().
*
* @param array $form The form
* @param string $form_state The current state of the form.
*/
function mymodule_hooks_form_views_exposed_form_alter(&$form, &$form_state)
{
// We only want to change a certain form, so stop Drupal looking at any
// other exposed view form
if ($form['#id'] == 'views-exposed-form-myview-page-1') {
// Make the select box appear as a list of checkboxes
$form['formelement']['#theme'] = 'select_as_checkboxes';
$form['anotherformelement']['#theme'] = 'select_as_checkboxes';
}
}

function theme_select_as_checkboxes($element) {
$output = '';

$selected_options = (array) $element['#post'][$element['#name']]; // the selected keys from #options

foreach($element['#options'] as $key => $value) {

$id = $element['#id'] . '-' . $key; // custom

// is this option selected?
$selected = (array_search($key, $selected_options) !== false); // (returns key or false)

$checkbox = '';

$output .= '' . "\n";
}
return theme_form_element($element, $output); // wraps it neatly
}

Wednesday, June 22, 2011

user_external_login Perform standard Drupal login operations for a user object.

D6 user_external_login($account, $edit = array())

Perform standard Drupal login operations for a user object.

The user object must already be authenticated. This function verifies that the user account is not blocked/denied and then performs the login, updates the login timestamp in the database, invokes hook_user('login'), and regenerates the session.

Make sticky tableheaders optional

Drupal 6:

// Sticky tableheaders always enabled.
$table = theme('table', $headers, $rows, array(), NULL, array());

Drupal 7:
// With sticky tableheaders.
theme('table', array('header' => $header, 'rows' => $rows));
// Without sticky tableheaders.
theme('table', array('header' => $header, 'rows' => $rows, 'sticky' => FALSE));

Inline cascading stylesheets from drupal_add_css()

Drupal 6.x:

$color = variable_get('backgroundcolor', '#FFFFFF');
drupal_set_html_head("");

Drupal 7.x:
$color = variable_get('backgroundcolor', '#FFFFFF');
drupal_add_css("body {background-color: $color}", 'inline');

Friday, June 17, 2011

How to theme a form

function vs. file
Well, assuming the form doesn't add already a theme implementation, you have 2 ways to override the form rendering with hook_theme:

function in your template.php
file in your template directory

To add a function you can either specify a 'function' value in the array or leave empty (according hook_theme)

function: If specified, this will be the function name to invoke for this implementation. If neither file nor function is specified, a default function name will be assumed. For example, if a module registers the 'node' theme hook, 'theme_node' will be assigned to its function. If the chameleon theme registers the node hook, it will be assigned 'chameleon_node' as its function.

So this is the code:

array(
'arguments' => array('form' => NULL),
),
);
}

function MYTHEME_MYFORM_form($form) {
$form['#title'] = 'New form title'; // minimal change but you can do anything here
return drupal_render($form);
}
?>

BTW changes like the one above would be better handled on a form_alter hook...

On the other hand if you want to play with a file you should enter a value for the 'template' key:

template: If specified, this theme implementation is a template, and this is the template file without an extension. Do not put .tpl.php on this file; that extension will be added automatically by the default rendering engine (which is PHPTemplate). If 'path', above, is specified, the template should also be in this path.

So the code would be:

array(
'arguments' => array('form' => NULL),
'template' => MYFORM,
),
);
}
?>

Then the template file would be MYFORM.tpl.php.

Monday, June 6, 2011

Drupal An illegal choice has been detected. Please contact the site administrator

If you see in the core it this message will find in the form validate function. With this drupal say's that "you tried to submit this form with select, checkbox or radio button option that wasn't included in the original form definition! That's not allowed."So, make sure you have not changed the original values of these form elements.