Drupal: Recipes

by Gisle Hannemyr

This segment of site is going to be a collection of small recipes explaining how to perform common tasks..

Table of contents

Introduction

Drupal recipes to go into a searchable database.

Get the vocabulary name of a taxonomy term knowing its tid

$term = taxonomy_term_load($tid);
$vocabulary_name = $term->vocabulary_machine_name;

Using the Taxonomy API is preferable to going into the database directly as you can take advantage of caching properly, and keep a more structured code base at the same time.

Source: DA SE.

Redirect for node add

How you add the submit handler matters.

To also edit the edit use case, use hook_form_BASE_FORM_ID_alter().

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * FORM_ID: my_node_form
 */
function rbg_form_my_node_form_alter(&$form, &$form_state, $form_id) {
  …
  $form['actions']['submit']['#submit'][] = '_my_node_form_submit';
  …
}

/**
 * Submit for my node form.
 */
function _my_node_form_submit(&$form, &$form_state) {

  // You may also want to unset destination.
  //unset($_GET['destination']);
  //unset($_REQUEST['edit']['destination']);

  $form_state['redirect'] = 'rbg';
}

Source: Drupal SE.

Search box for a single content type

Source: D.org. I use this on CC.no for dictionary search.

Do not truncate username

Currently usernames are trimmed when dispalyed as links to the user if they are over 20 characters. They end up as 15 characters + 3 periods.

function mytheme_preprocess_username(&$variables) {
  $variables['name'] = check_plain($variables['name_raw']);
}
https://drupal.stackexchange.com/questions/254955/prevent-comment-author-name-from-truncating

Final word

[TBA]


Last update: 2014-02-20 [gh].