drupal planet

Programmatically Adding or Removing a User or Node Reference from a Node (D7 / References)

The References module in Drupal 7 allows for easy creation and removal of user and node references through Drupal's interface. However, programmatically adding and removing these references is a little more difficult.

You basically have to load the node which has the reference in it, edit the reference field (in my example, the reference field can have an unlimited number of references), add or remove the user ID (or node ID if you're chaging a node reference), and save the node.

Let's look at the example of simply adding a user reference to a node:

<?php
 
// Load the node you'd like to edit.
 
$node = node_load($nid);
 
// Add the user ID you'd like to add to this node reference.
 
$node->field_node_user_references[$node->language][] = array('uid' => $uid);
 
// Save the node.
 
node_save($node);
?>

It takes a little more effort to remove a user reference (or node reference) from a node. For this, since I have to do it for a few different fields on a node, I've written a helper function that removes a given $uid from the array of user references on a given node.

Views: Show "Showing X-X of X results" (page and result counter) in Drupal 7

[Update: Views 3.x has a really nifty plugin feature called 'Results summary' that you can simply add to the header or footer of your view, and use your own text with placeholders, to do everything I outline in the post below, without a line of code. Add a results summary instead of using hook_views_pre_render() or a Views PHP field.]

I needed to display a page/item counter on a view on one of my Drupal 7 sites, using Views 7.x-3.x. This counter would display at the bottom of the view, just above the pager, and needed to display the current number of results being displayed, along with the total number of results.

Views provides all this data right inside the $views object, so all I needed to do was add the following PHP snippet (including the <?php ?> delimiters) to a 'Global: PHP' textarea in my view's footer:

Moving Scheduler Module's 'Scheduling Options' Out of the Vertical Tabs in D7

...or, "Always Check Your Module Weights when form_alter'ing"

Scheduling options from Scheduler module

I spent about half an hour today trying to use hook_form_alter() to move the 'Scheduling options' fieldset (provided by the Scheduler module) out of my node form's vertical tabs (down where URL path settings, comment settings, etc. are jumbled together).

I couldn't even see the 'scheduler_settings' form settings when I looked at the form's array, even though I knew it existed (since it was being displayed, and the scheduler.module defined it using its own hook_form_alter().

Declustering Markers in Google Maps with Drupal?

Location MarkerI recently received a question from a friend who's setting up a new site in Drupal 7, and is using the GMap Module, Location, and Views, to set up a map of upcoming events for his website.

My response (posted below) basically gives some pointers for what other people (often creating custom implementations of Google Maps on their sites) are doing to avoid the problem of 'decluttering' or 'declustering' multiple points at the same location (same coordinates). My question is: how do you handle declustering on your Drupal site? Are there any perferred techniques? Luckily for me, this is a problem I have yet to encounter, as I've only had to map locations of stores, parishes, etc., that are already spread out evenly over some area of a map :-)

My response to his question follows:

Scaling the Drupal Community - Notes and Reflections

The sparsely-attended 'Scaling the Drupal Community' session, led by webchick and heyrocker, was one of the few sessions I've attended at DrupalCon Chicago that held my interest throughout. And, since a few people on IRC asked me to post my session notes, I thought I'd do so and put them up on the Planet.

If you, like me, thought there were too many awesome sessions during this timeslot that you decided to go to another one, then this post is for you—I believe that anyone invested in Drupal's future stands to gain something from reflecting on what webchick said at the session.

Now, on to the notes. I will give a summary of a statement by webchick, then my reflection (kind of a Q&A format):

On 'Drupal Answers' vs. a drupal.org solution for Drupal Support

Drupal Answers is a new Stack Exchange site for answering Drupal questions, and it's in public beta right now. The site is hosted off the 'drupal.org' family of sites, and has a few hundred users so far...

Webchick: Webchick basically stated that she's leaning towards what we already have (with slight adjustments). She mentioned that there's no good answer for the question of motivating people to help support drupal newbies without incentives (a karma or reputation system).

Also, she said that moving the support away from drupal.org is potentially very dangerous; this could lead the 'newbie' community away from the 'expert' community (which tends to discuss things in drupal.org issue queues. Right now, 'newbies' typically hang around in drupal.org forums, and 'experts' typically stick to their trackers and issue queues.

Git through an NTLM Proxy (Corporate Firewall) for drupal.org

Borrowing from answers in this Stack Overflow question, here's how you can get through a corporate (Microsoft) NTLM Proxy to clone git repositories from drupal.org:

cd into your drupal contrib directory (or wherever you want to put the repository).

$ export http_proxy="http://username:password@proxy:port/"

$ git clone http://git.drupal.org/project/[projectname]

Basically, you're first setting an environment variable to tell your shell to use an HTTP proxy, with your username/password combo. This variable will be used when making connections to git.drupal.org (and other services, like github). You can also set this in your ~/.profile, .bash_rc, or .bash_profile so it will be saved for future Terminal sessions.

Gitting Started with New Drupal.org VCS

This is half for my own reference, because I have a few other computers I still need to set up, and I don't want to keep referring back to drupal.org docs to get everything ready for Git.

In case you've been under a rock lately, drupal.org was down for a while yesterday, while a team of dedicated Drupal peeps spent a few hours migrating everything in drupal.org's version control system (which was running CVS) to Git. Git is an excellent tool for version control, and I've been using it for a few months for my personal projects (most recently, I've started using Tower on my Mac to make Git easier).

Without further ado, here are the steps/links you need to 'git' started with Git on drupal.org:

Drupal 7 Front-End Performance - Shared Hosting Recommendations

Speedometer - BoostedI've spent a lot of time working on making sure my smaller Drupal sites (mostly run on shared hosts or very small VPSes) run lean and mean. This helps the pages load faster, users are happier, and my hosting providers don't have to shut down any of my sites, even when they're under pretty heavy load.

Here are my three recommendations for making your Drupal 7 website run great on a shared (or low-end VPS) host:

Prepping for Git on drupal.org - Need a Git client? Try Tower

A few weeks ago, I heard rumors of a new Git client for the Mac that was supposed to be even easier/prettier than my past favorite, GitX (specifically, brotherbard's fork of GitX). GitX is simple, and very usable, but just feels rough sometimes. But... it's free, so it is very nice in that way.

Tower for St. Louis Review
Yes, I often work on the server. "Do what I say, not what I do."

This new Git client is Tower, and it is awesome. I paid for two licenses for my two Macs, and I highly recommend it for other Mac users deeply invested in Git. The interface is simple, it has searchable commit logs, it lets you stage and unstage (and modify) changes and commits with efficiency and ease, and it is $50 well-spent. Check out the Tower demonstration video on YouTube.

Pages

Subscribe to RSS - drupal planet