Snippets

Lista de snippets

uso de drupal_add_js en page.tpl.php

drupal_add_js no funciona en page.tpl.php, se pone en el archivo template.php para que funcione.
function theme055_preprocess_page(&$variables) {
 
 
	$js='$(document).ready(function(){
    $("div#cabecera").flash(
        { src: "/prodialogo/sites/all/themes/theme055/cabecera.swf",
          width: 932,
          height: 192 },
        { version: 8 }
    );
});';
 
 
	drupal_add_js($js,'inline');
 
	$variables['scripts'] = drupal_get_js();
 
}
y en el archivo page.tpl.php va
<div id="cabecera"> </div>

Paginar en php

<?php
/**
 * Adam Sánchez Ayte julio 2009
 * Este script es mi primer ejercicio personal sobre paginacion en php
 * $numItemsPorPagina es el numero de filas por pagina
 * $paginaActual es la pagina actual
 * $totalItems es el numero de filas totales de la tabla 'content'
 * $paginas es el numero total de paginas que usaremos para navegar
 * 
 */ 
 
 
$conn=mysql_connect('localhost','root','');
 
mysql_select_db('aa');
 
 
$paginaActual=$_GET['page'];
$numItemsPorPagina=10;
 
 
$sql="SELECT * FROM content LIMIT ".($paginaActual-1)*$numItemsPorPagina.",".$numItemsPorPagina;

Medir el tiempo de ejecución de un script php

Tomado de http://forum.powweb.com/archive/index.php/t-7928.html
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
 
$time_start = getmicrotime();
 
mysql_query( "" );
 
$time_end = getmicrotime();
$time = $time_end - $time_start;
 
echo "Did nothing in $time seconds";

Tips: Print out available variables

<?php
print '<pre>';
print_r(get_defined_vars());
print '</pre>';
?>
<?php
print '<pre>';
print htmlspecialchars(print_r(get_defined_vars(), TRUE), ENT_QUOTES);
print '</pre>';
?>

modificación de campos de formulario de edición de un nodo en Drupal

Copiado de http://carlos.rinconsanchez.com/como-modificar-el-formulario-de-un-tipo-de-contenido-en-drupal Ponemos en el fichero template.php del theme lo siguiente:
function phptemplate_node_form($form) {   
    drupal_set_message("Reescribiendo el formulario de " . $form['#node']->type);
    if($form['#node']->type) {
        if(file_exists(path_to_theme() . '/forms/form-' . $form['#node']->type . '.tpl.php')) {
            drupal_set_message("Encuentra el fichero y lo reescribe");
            return _phptemplate_callback('forms/form-' .

Ejemplo de clase extendida para hacer global una variable

La duda que me despejé en la clase de TECSUP :)
<?php
 
class itemviews {
 
function test(){
 
$items =array('uno','dos','tres');
 
return $items;
 
 
}
 
}
 
class itemview extends itemviews {
 
public $items;
 
function __construct () {
 
global $list;
 
$list=parent::test();
 
}
 
function test() {
 
$list = $this->items;
 
return $list;
 
}
 
}
 
$clase = new itemview();
 
$list[]= "Hola Mundo";
 
print_r ($list);
 
?>

theme table de drupal

Muestra como se usa el theme table de drupal
<?php
//$header=array('primero', 'seguro');
$header= array(array('data'=>'primero','width'=>'200px'),array('data'=>'segundo','width'=>'600px'));
$rows[]= array(array('data'=>'1','align'=>'center'),array('data'=>'2','align'=>'right'));
$rows[]= array(array('data'=>'3','align'=>'left'),array('data'=>'2','align'=>'right'));
 
print theme('table',$header, $rows, $attributes = array(), $caption = NULL);
 
?>

Acortar titulos de menus de bloques

Primero colocar en template.php
function _phptemplate_overwrite_links_menu_block ($delta){
 
	$menus=array();
	$menu=array();	
 
	$menus=menu_navigation_links($delta);
 
foreach($menus as $menu) {
 
	$output[]=l(((strlen($menu['title'])>24) ?
Distribuir contenido