Drupal: Add Body Class when Tabs are displayed

I had the urge to style various elements differently in case tabs (the ones generated by menu_local_tasks) are present on the page. Out of the box $tabs array isn't available in html.tpl.php so I had to implement ' hook_preprocess_html' and add my desired class to my array of body classes. Yay!

<?php
function THEMENAME_preprocess_html(&$vars, $hook) {
  // Make tabs available on html tpl
  $vars['tabs'] = menu_local_tabs();
  // if we have primary tabs, add an accoring body class
  if (!empty($vars['tabs']['#primary'])) {
    $vars['classes_array'][] = 'has-tabs';
  }
}
Tags: 

Unmaintainable Block Admin Page in Drupal

When you have more than 100 Blocks things get messy on Drupal’s Block Admin Interface. If you ever went crazy because of weird issues when dragging and dropping blocks to change their weight and/or region you probably have hit Drupal’s internal limit of handling weights from -50 to +50 or otherwise said, 100 items.

It can be fixed by setting the variable drupal_weight_select_max to a higher value. This can be done in various ways, here’s how its implemented in a deploy script.

function dgm_deploy_update_7011() {
  variable_set('drupal_weight_select_max', 250);
}
Tags: 

Custom Drupal Views Template breaks AJAX (updated)

I often create custom Views templates for my Drupal sites, for example to freely reposition exposed filters. Unfortunately, AJAX exposed filters in Views works only with a very strict markup hierarchy, and if your markup doesn't match that scheme (.view-[view:id] > .view-filters > filters) it won't work anymore

diff --git a/js/ajax_view.js b/js/ajax_view.js
index 7d54c67..f7f7ba9 100644
--- a/js/ajax_view.js
+++ b/js/ajax_view.js
@@ -57,7 +57,7 @@ Drupal.views.ajaxView = function(settings) {
   this.settings = settings;

   // Add the ajax to exposed forms.
-  this.$exposed_form = this.$view.children('.view-filters').children('form');
+  this.$exposed_form = this.$view.find('.view-filters form');
   this.$exposed_form.once(jQuery.proxy(this.attachExposedFormAjax, this));

   // Add the ajax to pagers.

Source: https://www.drupal.org/node/2477307

Update

Thanks to this patch that removed the selector dependence this is not needed anymore

Remove fields form content type programatically

Say you want to remove the body field from the page content type. Putting this code inside an updatescript will get the job done. Remember not to use field_delete_field() if you're not sure if you still want to use the field anywhere else. It's way safer to rely on field_delete_instance. Setting the second parameter to TRUE means it's automatically going to clean up the field anyway if it is found to be the last instance.

$instance = field_info_instance('node', 'body', 'page');
field_delete_instance($instance, TRUE);
Tags: 

Sync Brackets Extensions & Config

  1. Find your Brackets user folder (usually ~/Library/Application Support/Brackets) and move brackets.json and extensions/ to a shared folder (e.g. Dropbox)

  2. Add a symlinks to the moved file and folder:
    cd ~/Library/Application Support/Brackets
    ln -s ~/Library/Mobile\ Documents/com~apple~CloudDocs/config/Brackets/extensions/
    ln -s ~/Library/Mobile\ Documents/com~apple~CloudDocs/config/Brackets/brackets.json

  3. On your other machine make sure your config folder is done syncing, delete brackets.json and extensions/ from your Brackets user folder and setup your symlinks in the same way. Done \o/

PS: If you think: but why not syncing the entire Brackets user folder: I did that but I was bugged by the fact that Brackets was constantly complaining about not finding recently opened files and folders, which are saved in state.json. So I made sure that this file won’t be synced. 

Translate Drupal Strings Programatically

One of the many issues one has to face when trying to do some proper deployment with Drupal (only up to version 7, that is) is custom localized strings. Fear no more! Instead of translating strings in the admin interface (/admin/config/regional/translate/translate) we can make use of the great Update Script Processor, thus put all our custom translations into code and happily version and deploy them. Yay!

  1. Install Update Script Processor
  2. A update Script that contains your localizations, e.G. $this->setLanguageWording('de', 'Telephone number', 'Telefonnummer');
  3. run drush updatescripts

Obviously, you should put that under version control and make sure your deployment process runs drush updatescripts after deploying to another environment.

Here's a full example that translate the original strings Telephone number and Fax number to their German counterparts:

Pages