Change a title of a view block programmatically!

Just in case you are like me and find yourself wanting to change the title of a view programmatically, and can't figure out how, the (somewhat unintuitive) way is to hit the build_info, so you do this in your module:

<?php
function hook_views_pre_render(&$view) {
 
// Some code to make sure you are changing the right title, say an if statement perhaps!
 
$view->build_info['title'] = t("Your Brand New Title!");
}
?>

Soon this will not be necessary, as there will be a much easier method for accomplishing this included in Views sometime in the near future.

<?php
function hook_views_pre_render(&$view) {
 
// Some code to make sure you are changing the right title, say an if statement perhaps!
 
$view->set_title($fancy_new_title);
}
?>

Hope this helps!

Going back to Vim

Well, I have been all over the place on IDE’s and editors, and I am coming back full circle to Vim. It’s been around forever but seems to keep getting the love. After spending a good part of the morning ramping up my Vim setup after reading http://nvie.com/posts/how-i-boosted-my-vim/ I am all kinds of excited to try some of the new plugins I have installed.

Also, though I have had a good time working with Komodo Edit, and NetBeans I find I am still a lot more comfortable, and faster, in Vim. Also, used in conjunction with code snippet software, and some other speed up tips found in http://net.tutsplus.com/articles/general/9-ways-to-instantly-code-faster/ I am virtually flying when I actually do get to the point where I am coding.

The Story of Postfix and the Non-Local Domain Email Host

So I have been scratching my head for a little while over how to get Postfix to deliver mail from my www.example.com site off to an external mail host, when the email address has the same domain, @example.com.

What I discovered is that I needed to do 3 things:

  1. Set my machine hostname to be distinct, ie www.example.com (vim up your /etc/hostname and then hostname -F /etc/hostname)
  2. Set mydomain and myhostname to both be the same as the machine hostname, www.example.com (this is in /etc/postfix/main.cf)
  3. Restarted postfix

The key here for me was to ensure that the hostname (and Postfixes mydomain setting) needed to be different from the actual domain of the email addresses I was trying to get mail delivered to. At that point, I didn't need to deal with local host files, or luser settings, etc. The Postfix program just sees it as an external mail address and pushes it out like any other mail. Yay!

There are a bunch of other things you should probably set as well if you are just setting up your Postfix, run /etc/postfix/post-install first-install-reminder for a fun reminder of what you need to do. http://postfix.org is also a good resource for getting things setup.

Anybody that knows a better way, please comment!

How to hide a form field and keep the value

Just a quickie. Every so often in Drupal you will come across the need to hide a form field, but can't get rid of it, because it holds a value, or you need to show it to one roll but not another, etc. In cases such as this, it is good to remember '#type' = 'value'

Thanks to Eugen Mayer for refreshing me on that.

An example would be

<?php
/**
* Implementation of hook_form_alter()
*
*/
function cool_module_form_alter(&$form, $form_state, $form_id) {
  switch (
$form_id) {
    case
'wicked_cool_form':
      if (
$form_state['pigs'] == 'fly') {
       
$form['pigs']['field_pigs_can_fly']['#type'] = 'value';
      }
    break;
  }
}
?>

My Vim

Screenshot-views.module (-etc-acquia-drupal-6-sites-all-modules-views-DRUPAL-6--3) - GVIM
This is just an image of my vim setup looking at a Drupal module, bookmarks, and file tree (NERDTree) all visible, with Monaco as the font, and ir_black as the colorscheme. This is on Ubuntu, however MacVim looks pretty identical.

Pretty soon here I will make a blog post about how use Vim, and how I got this setup, along with my .vimrc file and .vim directory.

Adding permissions to a role programatically

I wrote this little bit of code after asking around to find out if there was a way to add permissions by role and came up empty. Ends up there is a nice module for doing this for install profiles and could be used for update functions as well I am sure. I haven't looked at it, but you can check out the Install Profile API.

This apparently has some nifty items that aid in your install profile construction (thanks Boris!), however all I need is to add some checks to some checkboxes on the Permissions page via code, so I am using this:

<?php

/**
* _add_permissions() is a helper function to add permissions by role to the db
*/
function _add_permissions($rid, $permissions) {
  if (!
is_array($permissions)) {
   
$permissions = explode(', ', $permissions);
  }
 
$current_perms = explode(', ', db_result(db_query("SELECT perm FROM {permission} WHERE rid=%d", $rid)));
  foreach(
$permissions as $permission) {
    if (!
in_array($permission, $current_perms)) {
       
$current_perms[] = $permission;
      }
    }
 
$current_perms = implode(', ', $current_perms); 
 
$return = db_query("UPDATE {permission} SET perm= '%s' WHERE rid=%d", $current_perms, $rid);
  return
$return;
}
?>

Draggable Views, a UI sanity saver!

In many instances, I have seen drag and drop (dnd) ordering, rearranging, what have you, used where it perhaps didn't really need to be, but sometimes, there is a real good use for it. I ran into that today.

Views Slideshow Imageflow

Making git a bit easier on me

So lately I have been having all kinds of issues forgetting to do a git pull before doing a git push. Decided to make sure that doesn't happen any more.
#!/bin/bash
# This little script just makes it easier to do all of the git things I do all at once,
# so instead of typing git add file1.fl file2.fl etc, then git pull, then
# git commit -m "blah blah blah", this let's me just type:
# gp "My commit message" all.my files.etc

args=("$@")

if [[ ${args[0]} == '--help' || ${args[0]} == '-h' || ${args[0]} == '/?' ]]; then
  echo "Usage: gp "Your commit message in quotes" filetocommit.ext file2tocommit.ext etcToCommit.ext"
  exit
fi

for (( i=1; i<$#;i++)); do
  git add ${args[i]}
done
git commit -m "${args[0]}"
git pull
git push

CCK field hook_form_alter ARGH!!!!

Ok, there are a couple of things in Drupal that I pound my head against on a regular basis, and overriding CCK fields using hook_form_alter in a custom module is one of them. You write the code, but you don't see any changes. Weird? Oh yeah.
Come to find out from this excellent blog post that I wasn't the only one to experience this. It stems from the module weight of your module as compared to CCK. The fix is to write a simple .install file for your module and set the weight:

Drupal 7 served from the G1 phone using lighttpd web server, php-cgi, and php-sqlite

If that title doesn't get your attention, you aren't a geek, srsly.

drupal on the g1
After hearing a little interest from some people around irc-land, and from one person who I am considering starting a G1-fund for, I decided that I should try to get Drupal 7 working on my T-Mobile G1 phone. After all, it's an Open phone, right? I might have even uttered those words my wife and I always regret; "How hard can it be?"

Actually, it wasn't that hard :)

Powered by Drupal, an open source content management system