php

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
 
<br /
?>
// 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.

Integrate a Webform (3.x) with PayPal

For quite some time, I've wanted to integrate a particular webform with PayPal, since many nonprofits I help use that payment service. With older versions of PayPal, one could add some PHP code into a webform on the site to do this, but it was (a) hackish, and (b) a much less maintainable and secure way of accomplishing the goal I was trying to achieve. So I never did it.

However, after reading Additional processing in Drupal's Webform 3 module (from Drupal Coder), I found that Webform 3 has a hook that runs just after webform has saved a form's data to the database, but before the webform returns the user to a predefined redirected page. hook_webform_submission_insert() is the perfect time to hook into Webform's process and send a user to PayPal, along with that user's data.

Drupal Performance Guide - Drupal and the LAMP/LEMP stack

LAMP Stack with Drupal - Druplicon, Linux, Apache, MySQL, PHP

Drupal is a scalable, flexible, and open source content management system that is built to run on a variety of server architectures. The only real requirement is that PHP runs on your system. You can run Linux, Microsoft, Mac OS X, etc., along with Apache, IIS, nginx, MariaDB, MySQL, PostgreSQL, etc. if you're willing to do a few extra things.

However, the overwhelming majority of Drupal websites use the most popular LAMP stack on the backend: Linux, Apache, MySQL and PHP, or the 'LEMP' variation, with Nginx instead of Apache. This white paper (which is a living document – I'll be updating it as time progresses) provides my thoughts on performance considerations for Drupal on a LAMP stack, but this information can be used for pretty much any system on any server, if you look at the basic principles.

Sections:

Catholic Programmer's T-Shirt and Mousepad

Reposted from Open Source Catholic:

A few weeks back, after finishing a full day of swimming in PHP, HTML and CSS, I was pondering the great mystery of human existence, but probably had a little too much to drink. The result?

Catholic Programming - Design

You can buy the design on the following products (via Zazzle):

Would you like the design on anything else? I could do a mug, or socks, or anything else in Zazzle's catalog...

Alternatively, can you think of a way to code this better? ;-)

Building a Theme for Drupal 7

After having built out many themes for Drupal 6 (and a couple for Drupal 5), I'm going to start from scratch on a couple designs and build a theme in Drupal 7, which will be released sometime in 2010. I'll take you along my journey in this article.

Please note, this article is a work in progress, and I'll be updating it as I go. Hopefully, within a couple weeks, I'll have the article complete, and a nice new theme to release on Drupal.org (maybe), only for Drupal 7.

To get things kicked off, here are a few articles that have good background information on Drupal 7 theming:

Drupal: 2009 Best Open Source PHP CMS!

Packt publishing just announced earlier this morning that Drupal has won the 2009 Open Source PHP CMS award!

Packt Publishing is pleased to announce that Drupal has won the Best Open Source PHP CMS Category in the 2009 Open Source CMS Award. This category featured a very close contest between the top three, Drupal, WordPress, and Joomla! in which Drupal ended up as the overall choice for the judges and the public.

Joomla was the second place winner (kudos to them as well!); read the original release here.

With Drupal being used on Whitehouse.gov, AT&T's mobile apps website, and countless other corporate sites and subsites, it's a good time to be in the Open Source arena!

More award categories will be announced soon!

From OSC: Caching a Page; Saving a Server

I posted a story over on Open Source Catholic today concerning page caching and its importance for saving a server under a heavy load (read: the slashdot effect). You can save a lot of resources on your server by not only using built-in page caching on your favorite CMS, but also exploring further options (for Drupal, there's Boost (read our case study on Boost); for WordPress, there's WP Super Cache). From OSC:

A couple months ago, the Archdiocese of Saint Louis announced that a new Archbishop had been chosen (then-Archbishop-elect Robert J. Carlson). For the announcement, the Archdiocese streamed the press conference online, then posted pictures on the St. Louis Review website of the day's events (updated every hour or two).

Let's Get Through the PHPTemplates!

I just finished rolling a patch for fixing node.tpl.php in Drupal 7 over on the Drupal.org issue queue; hopefully it's ready to be rolled into core, as it's been weeks months since that particular issue was started. Page.tpl.php is already complete. We still have a few more to go, including comment.tpl.php, block.tpl.php and a bunch of little .tpl.php files.

I think Drupal 7 is going to be the best release of Drupal yet, in terms of being able to have a lot of appeal to non-programmers/techies. I set up my first ever full-fledged Wordpress site a few days ago, and it was super-easy to get things going (although also severely limited in what it could do, compared to a base Drupal install with Views and CCK), choose a new theme, change some settings, and hit the ground running.

Current Time - @currenttime on Twitter

A few weeks ago, I was thinking to myself: "It's hard to tell what time certain tweets in my (already busy) timeline were posted... It'd be great if, instead of '18 minutes ago,' I could see that the post fell between 10 p.m. and 10:10 p.m. – it's much easier for me to remember tweets by time than by 'time ago.'

So, instead of trying to find some Twitter app that would let me insert my own timestamps or change the date format (I'm sure there are a few), I wrote a PHP script that would post a new tweet with the time in four time zones to the @currenttime Twitter account. Then, I followed @currenttime, and history was made ;-)

Here's the script I used (with help from Morethanseven's article, "Posting to Twitter using PHP"):