Tag Archive for drupal

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

Drupal FiveStar custom widget

<?php
$votes = fivestar_get_votes($content_type, $content_id, $tag);
$node_type = $node->type;

$values = array(
'user' => empty($votes['user']['value']) ? 0 : $votes['user']['value'],
'average' => (int)$value,
'count' => empty($votes['count']['value']) ? 0 : $votes['count']['value'],
);

$settings = array(
'stars' => variable_get('fivestar_stars_'. $node_type, 5),
'allow_clear' => variable_get('fivestar_unvote_'. $node_type, FALSE),
// If the user has setup this content type to use smart stars, display
// the smart version instead of just the average.
'style' => variable_get('fivestar_style_'. $node_type, 'average') != 'smart' ? 'average' : 'smart',
'text' => $summary ? variable_get('fivestar_text_'. $node_type, 'dual') : 'none',
'content_type' => 'node',
'content_id' => $node->nid,
'tag' => $tag,
'autosubmit' => TRUE,
'title' => FALSE,
'feedback_enable' => $summary ? variable_get('fivestar_feedback_'. $node_type, 1) : FALSE,
'labels_enable' => $summary ? variable_get('fivestar_labels_enable_'. $node_type, 1) : FALSE,
'labels' => $summary ? variable_get('fivestar_labels_'. $node_type, array()) : array(),
);

print drupal_get_form('fivestar_custom_widget', $values, $settings);
?>

source

hook_form_alter for views exposed filter form

/**
* Implementation of hook_form_FORM_ID_alter() for 'views exposed form'
* Change the first option label '<All>' to 'Filter by Identifier Name' for exposed filters
* The filter identifier needs set up with underscores for multiple word filter names
* e.g. 'document_type' becomes 'Document Type'
*/

function mymodule_form_views_exposed_form_alter (&$form, $form_state) {
foreach($form as $key => &$value) {
if(isset($value['#options']['All'])) {
$label = ucwords(strtolower(str_replace('_', ' ', $key)));
$value['#options']['All'] = t('Filter by !label', array('!label' => $label));
}
}
}

source

Use drush sql dump to dump the database to the file system.

drush sql dump > /path/to/BACKUPS/site_name-$(date +%Y-%m-%d-%H.%M.%S).sql

source