Tag Archive for drupal

drupal – find available variable names and content for node

<?php
foreach($node->content as $outerarray=>$outerarrayvalue)
{
	foreach($outerarrayvalue as $innerarray=>$innerarrayvalue)
	{
		foreach($innerarrayvalue as $centrearray=>$centrearrayvalue)
		{
			foreach($centrearrayvalue as $key => $value)
			{
			echo("<em>".$outerarray."["".$innerarray.""]["".$centrearray.""]["".$key.""] = </em><b>".$value."</b><br />");
			}
		}
	}
}
?>

source

Render Taxonomy Terms based on Vocab ID

function render_terms($node,$vocabid,$title=null,$divclass=null) {

$terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabid );
if ($terms) {

$output = '<div class="field field-item'. ' '.$divclass .'"><div class="field-label-inline-first">' .$title .' </div> ' ;
$term_links = array();
foreach ($terms as $term) {
$term_links[] = l($term->name, taxonomy_term_path($term), array('attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description)))) ;
$output .= implode(", ", $term_links);
}
}
$output .='</div>';
return ($output);
}

source

Render a CCK Image Field

function render_image_field($node,$name,$title=null,$divclass=null,$limit=0,$start=0) 
{


$field_name = 'field_'.$name;
$field = $node->{$field_name};

$limit = ($limit == 0) ? count($field): $limit;

for ($i=$start; $i < $limit; $i++) {
$output .= '<div class="field field-item'. ' '.$divclass .'"> ' ;
$output .= $field[$i]['view'];
$output .= '<span>' .$field[$i]['data']['description'] .'</span>';

$output .='</div>';
}
return ($output);
}

source

Drupal JS namespace

Drupal.behaviors.typicallythemenamehere = function(){ 
 //code here
}

source

Odd and Even classes Shorthand

<?php print ($count % 2 ? 'odd' : 'even') ?>

source

Theme a form button / Admin Form Buttons auslassen

/**
 * Theme a form button.
 *
 * @ingroup themeable
 */

function mytheme_button($element) {
  // Make sure not to overwrite classes.
  if (isset($element['#attributes']['class'])) {
    $element['#attributes']['class'] = 'Button form-' . $element['#button_type'] . ' ' . $element['#attributes']['class'];
  }
  else {
    $element['#attributes']['class'] = 'Button form-' . $element['#button_type'];
  }

  // Skip admin pages due to some issues with ajax not being able to find buttons.
  if (arg(0) == 'admin') {
    return '<input type="submit" '. (empty($element['#name']) ? '' : 'name="'. $element['#name'] .'" ') .'id="'. $element['#id'] .'" value="'. check_plain($element['#value']) .'" '. drupal_attributes($element['#attributes']) ." />
";
  }

  return '<button type="submit" ' . (empty($element['#name']) ? '' : 'name="' . $element['#name']
         . '" ')  . 'id="' . $element['#id'] . '" value="' . check_plain($element['#value']) . '" ' . drupal_attributes($element['#attributes']) . '>'
         . '<span class="btn">'
         . '<span class="l"></span>'
         . '<span class="r"></span>'
         . '<span class="t">' . check_plain($element['#value']) . '</span>'
         . '</span></button>';
}

source

add spans to links drupal

function theme_links($links, $attributes = array('class' => 'links')) {
$output = '';

if (count($links) > 0) {
$output = '<ul'. drupal_attributes($attributes) .'>';

$num_links = count($links);
$i = 1;

foreach ($links as $key => $link) {
$class = $key;

// Add first, last and active classes to the list of links to help out themers.
if ($i == 1) {
$class .= ' first';
}
if ($i == $num_links) {
$class .= ' last';
}
if (isset($link['href']) && $link['href'] == $_GET['q']) {
$class .= ' active';
}
$output .= '<li class="'. $class .'">';

if (isset($link['href'])) {
// Pass in $link as $options, they share the same keys.
$link['html'] = TRUE;
$output .= l('<span>'. $link['title'] .'</span>', $link['href'], $link);
}
else if (!empty($link['title'])) {
// Some links are actually not links, but we wrap these in <span> for adding title and class attributes
if (empty($link['html'])) {
$link['title'] = check_plain($link['title']);
}
$span_attributes = '';
if (isset($link['attributes'])) {
$span_attributes = drupal_attributes($link['attributes']);
}
$output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
}

$i++;
$output .= "</li>n";
}

$output .= '</ul>';
}

return $output;
}

source

Views default function

function {module_name}_views_default_views() {

{...view code...}

$views[$view->name] = $view; // include this line after each view's code

return $views;
}

source

Views API module declaration

function {module_name}_views_api() {
return array('api' => 2.0);
}

source

Image Cache Preset manuell im Template verwenden

theme('imagecache', Imagecache-Preset, filepath,title,alt);

source