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);

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:

Generate new Drupal 7 Password Hash

If you find yourself locked out of your own Drupal installation and are unlucky enough to not receive any password-restore emails (or your server not being able to send them) you need to set a new password directly in the database. As Drupal stores a salted sha512 hash (and no plain passwords, of course) you need to encrypt your new password before you can store it in your database.

To do this you can either use the Shell script called provided in the scripts folder of any Drupal installation. In case you don't have access to a shell you can also use the user_hash_password function in PHP:

This could be put in your index.php temporarily after bootstrapping Drupal. After visiting your site a string will be displayed. Replace your admin's password in the users table in the database with that string and you can now log in with YourNewPassword.

require_once 'includes/';
echo user_hash_password('YourNewPassword'); 

Override Superfish's Mobile Menu Trigger in Drupal

The Superfish integration for Drupal works nicely for all kinds of menus from Mega Dropdowns down to Mobile. What bothered me with Superfish's Mobile Menu implementation is that the Button that opens the Mobile Accordion (.sf-accordion-toggle) is always named after the name of the actual menu and normally can't be overridden.

One possible approach to overcome this is using plain CSS to hide the span and place something different inside the link tag using the content property. I don't like that idea too much so instead I tried figuring out how to change this with JavaScript. Turned out it's not possible to simply selecting the DOM element and changing its value because the original value gets re-assigned anytime you resize you browser windows. What does work is overriding the Superfish behavior. I just copied it over from superfish.js form the Superfish Module into my theme's JS and there I set my desire value, like so: options.plugins.smallscreen.title = '<i class="glyphicon glyphicon-menu-hamburger "/><span>Menü</span>';

Here's the entire function:

Let Drupal Editors Pick a Color and use it in Template

I'm currently working on an image slider where each slide needs to have its own background color.

Letting editors choose a color is easy with the Color Field Module. Just install it, throw the Spectrum Color Picker into your libraries folder and add and configure a Color Field where ever it's needed. You can then simply access the user-entered color as a hex string in your template and use it to your liking (yea, that's one of the rare times where it's ok to use inline style, at least in my book).

Convert Hexadecimal Color Values to RGBa

I needed my color to have alpha transparency, however, the color value is only available as a hexadecimal string. Luckily I found this handy snippet to convert hex values to RGB(a), so thanks a lot to mekshq for sharing.

Just put it into your template.php and you're good to to to use hex2rgba() anywhere in your theme.