Snippets

Snippet: Drupal 7 render view in code and hide if empty results

Use this simple snippet to embed a view in code and only display it if the view has at least one result (prevent rendering empty views markup):

  1. $view = views_get_view('your_view_name');
  2. $view->set_display('your_view_display');
  3. // Only display the view if it has results and the user has access.
  4. if ($view->access('your_view_display')) {        
  5.         // Example argument

Drupal 7 entityreference autocomplete: Improve the ENTER keydown behaviour (select first on enter)

I created this issue and snippet as better entityreference.module autocomplete widget usability solution in: ‎

Define extra fields (display) in code for Drupal Commerce products with auto AJAX handling and product+product node types handling

Today I had the challenge to add some extra (programatically calculated) fields to show in commerce.module product display nodes. In our specific case it was a "base price" that should be shown additionally to calculated SALE prices.

Drupal 7 Snippet: Add first level menu item mlid as body class for sectionizing your pages

If you'd like to separate your website areas by main menu first levels (for example colourize all areas in a different colour) a body class outputting the first level mlid may help you (otherwise you can for example use context.module or things like that!):

Here's the snippet you may use:

  1. function YOUR_THEME_preprocess_html(&$variables, $hook) {

MySQL: Tabelleninhalte nach UTF-8 umwandeln (von latin1 o.Ä.)

Mit folgendem MySQL Befehl könnt ihr relativ simpel ehemalige latin1 Felder zu UTF-8 umwandeln, z.B. nachdem ihr die Collation der gesamten Tabelle umgewandelt habt:


UPDATE tablename SET spaltenname=CONVERT(BINARY CONVERT(spaltenname USING latin1)USING utf8);

Snippet: Drupal Commerce: Hide subtotal (DE: "Zwischensumme") in commerce_price_formatted_components

  1. /**
  2.  * Implements HOOK_preprocess_commerce_price_formatted_components
  3.  */
  4. function YOUR_MODULE_preprocess_commerce_price_formatted_components(&$variables) {
  5.   // Do not show the subtotal (Zwischensumme) in components table
  6.   if (!empty($variables['components']['base_price'])) {
  7.     unset($variables['components']['base_price']);
  8.   }
  9. }

Drupal Commerce: Determine currently selected product variation (or default delta)

Today I'd like to provide you with a little commerce.module snippet to determine the currently selected product variation (using commerce_product_reference.module) of a product display with several variations.

I had to search hard for it so here's the code for you:

  1. // Determine the currently selected product or default product for this product display

Drupal: WYSIWYG + CKEditor + Live-Rechtschreibprüfung via scayt aktivieren und Sprache auf Deutsch einstellen

Heute ein kleines Drupal Modul-Snippet zur Aktivierung der autom. Rechtschreibprüfung via Scayt im CKEditor unter Einsatz des wysiwyg.module inkl. Sprachumschaltung auf Deutsch für die Rechtschreibprüfung (Standard-Sprache)

Snippet: JavaScript: Create css class/id/name identifier cleanly from string

Based on
https://api.drupal.org/api/drupal/includes!common.inc/function/drupal_html_id/7
https://api.drupal.org/api/drupal/includes!common.inc/function/drupal_clean_css_identifier/7
this simple JavaScript snippets turns strings into valid HTML id / class / name identifiers (http://www.w3.org/TR/html4/types.html#type-name)

Code:
 

Using drush and aliases.drushrc.php with non-standard SSH-Port

Drupal Drush aliases.drushrc.php for setting SSH remote port

Today I'd like to give you a little hint how to a execute a remote SSH drush command successfully for a connection defined in aliases.drushrc.php when the SSH port differs from default "22". This is for example the case on Hetzner Hosting.

In my case I set the "remote-port" option, but with no success:

  1. $aliases['live'] = array(
  2.   'remote-host' => '...',