Drupal 7 examples and resources

All these examples on this page are for Drupal 7 if not otherwise specified

Drush commands examples

Download module globalredirect for the site example.com

( option -l = URI of the drupal site to use - only needed in multisite environments or when running on an alternate port)

drush -l example.com dl globalredirect

Enable module globalredirect for the site example.com having document root /var/www/drupal

drush -r /var/www/drupal -l example.com en globalredirect

Clear cache for site example.com

drush -l example.com  cache-clear all

or

drush -l example.com cc all

http://www.drush.org/examples/example.aliases.drushrc.php
 

Add the content of a module block to a page (page should be in php format)

<?php
$block = module_invoke('module_name', 'block_view', 'module_name');
print render($block);
?>

Add the content of a block to a page/other block

$block = block_load($module, $block_id);
$block_content = _block_render_blocks(array($block));
$build = _block_get_renderable_array($block_content);
$block_rendered = drupal_render($build);
print $block_rendered;

Add the content of a view to a page

<?php

print views_embed_view('name_of_the_view', 'name_of_the_display');

?>

Hide language switcher links for untranslated languages (add code in template.php)

function myThemeName_links__locale_block($variables) {
    foreach($variables['links'] as $key => $value) {
        if (in_array('locale-untranslated', $value['attributes']['class'])) {
            unset($variables['links'][$key]);
        }
    }
    return theme('links', $variables);
}


Unset meta variable in the header that are part of the core (add code in template.php)

function myThemeName_html_head_alter(&$head_elements) {

    // remove meta generator tag
    unset($head_elements['system_meta_generator']);

    // the list of tags to remove
    $remove = array('shortlink', 'canonical');
    foreach ($head_elements as $key => $element) {
        if (isset($element['#attributes']['rel']) && in_array($element['#attributes']['rel'], $remove)) {
            unset($head_elements[$key]);
        }
    }
}

 

Useful Drupal links

Module creation

http://drupal.org/node/206761

http://drupal.org/node/206753

http://drupal.org/node/1525472

Working with table and pagination

http://w3shaman.com/comment/196

Adding WYSIWYG to textarea of block translation form

https://drupal.org/node/1252144#comment-6736460

function myModule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'i18n_string_translate_page_form') {

    // Loop all strings.
    foreach ($form['strings']['all'] as $name => $field) {
      // The field for [ block title / vocabulary name / vocabulary description / term name ] are textfields in ori language,
      // but textareas when translating: change these to textfields.
      if (
        preg_match('/blocks:block:[0-9]+:title/i' , $name) ||
        preg_match('/taxonomy:(vocabulary|term):[0-9]+:name/i' , $name) ||
        preg_match('/taxonomy:vocabulary:[0-9]+:description/i' , $name)
      ) {
        $form['strings']['all'][$name]['#type'] = 'textfield';
      }
      // Change textarea to text_format and overwrite description which is already auto included in text_format fields.
      elseif (
        preg_match('/blocks:block:[0-9]+:body/i' , $name) ||
        preg_match('/taxonomy:term:[0-9]+:description/i' , $name)
      ) {
        $form['strings']['all'][$name]['#type'] = 'text_format';
        $form['strings']['all'][$name]['#description'] = '<br />';
      }
    }

    // Add submit function.
    $form['#submit'] = array_merge(array('_myModule_form_submit') , $form['#submit']);
  }
}

function _myModule_form_submit($form, &$form_state) {
  // Remove wysiwyg format because i18n cant handle it.
  foreach ($form_state['values']['strings'] as $name => $field) {
    if (
      preg_match('/blocks:block:[0-9]+:body/i' , $name) ||
      preg_match('/taxonomy:term:[0-9]+:description/i' , $name)
    ){
      unset($form_state['values']['strings'][$name]['format']);
    }
  }
}