Posts tagged Drupal
Default Location for CCK field on Drupal Node Add Form
May 4th
We wanted to automatically fill in the Location fields on a node add form based upon the author’s location. Since most of these items that are posted would share the same location as the author, it seemed like a nice thing to do to make it a little easier for users.
In order to accomplish this, we used Drupal’s after_build. This allows you to call your own custom function. In this case it calls add_user_location which takes the user’s location and fills in the CCK location fields.
function digitalstax_customizations_form_alter(&$form, $form_state, $form_id) {
if($form_id == 'widget_node_form') {
$form['#after_build'][] = 'add_user_location';
}
function add_user_location(&$form) {
global $user;
$viewinguser = user_load(array(‘uid’ => $user->uid));
$form['locations'][0]['street']['#value'] = $viewinguser->locations[0]['street'];
$form['locations'][0]['city']['#value'] = $viewinguser->locations[0]['city'];
$form['locations'][0]['province']['#value'] = $viewinguser->locations[0]['province'];
$form['locations'][0]['postal_code']['#value'] = $viewinguser->locations[0]['postal_code'];
return $form;
}
Setting Values in Drupal Webform Additional Processing
Apr 18th
Sometimes when using the Drupal Webform module, you may want to do some calculations and then fill in a hidden field after the form has been submitted. For example, if we needed to calculate a total amount from the values of a few numerical fields, it might look something like this.
$total_amount = $form_values['submitted_tree']['pizza_price'] + $form_values['submitted_tree']['donut_price'];
//Then we have a hidden field with a component ID of 8 and we set it's value like this
$form_values['submitted'][8] = $total_amount;
$form_values['submitted_tree'][8] = $total_amount;
To get the component id, hover over “Edit” next to the component in your list on the webform and the id will be in the url after “components/.”
Using multiple databases with Drupal
Apr 18th
Using an external database with Drupal is very simple.
First, you must add the database to your settings.php file like so:
$db_url['default'] = 'mysqli://user:password@localhost/database_name';
$db_url['external'] = 'mysqli://user:password@localhost/database_name';
So, “default” is our regular Drupal database and “external” is an additional database. To switch to the “external” database, you would use this code:
db_set_active('external');
//Your code here
//*****then remember to SWITCH BACK TO DEFAULT DATABASE when done
db_set_active('default');
Easy as Pie!
Drupal Views PHP Filter
Apr 18th
Sometimes it requires a little PHP to get the job done in a Views filter. That’s where the Views PHP Filter module comes in. The goal when using a PHP snippet is to return an array of nids.
In this example we’re returning an array of nids where a field in a CCK content type is equal to the user’s name. This is a rare instance that we won’t go into. But the concept is the same for many other use cases.
global $user;
$nids = array();
$result = db_query("SELECT nid FROM content_type_property WHERE field_propmanager_value = '%s'", $user->name);
while ( $row = db_fetch_array($result) ) {
$nids[] = $row['nid'];
}
return $nids;
Remove “preview” button in Drupal
Apr 18th
Sometimes the preview button is unnecessary or unwanted in your Drupal installation. To remove the preview button, add this function to a custom module.
In this case, we wanted to remove the button from the node form for a “property” content type.
function digitalstax_customizations_form_alter(&$form, $form_state, $form_id) {
if($form_id=='property_node_form') {
unset ($form['buttons']['preview']);
}
}
Change “apply” button text in Drupal Views exposed filters
Apr 18th
When you want the user to search, the “apply” text can be confusing in a Drupal views exposed filter button.
To change this, create your own custom module and add the function below. Note that our module is named “digitalstax_customizations” and we wanted the change applied to a view named “admin_dashboard.”
function digitalstax_customizations_form_alter(&$form, $form_state, $form_id) {
if($form_state['view']->name == 'admin_dashboard') {
$form['submit']['#value'] = t('Search');
}
}
Dynamic meta descriptions and keywords in Drupal Views
Apr 18th
We created a view with exposed filters and arguments in Drupal so users could search for a particular product by city and state. For the purposes of this, we’ll call them widgets. We wanted to have custom meta descriptions and keywords for each of these city and state pages.
This requires the Nodewords module and you’ll need to create a custom module to take advantage of the functionality of the Nodewords API.
The arguments we used in the url for the view are as follows:
- arg 0 is “widgets” or the custom content type.
- arg 1 is the city.
- arg 2 is the state.
So a path would look like this: widgets/springfield/mo
We also included the ability to search all cities in a state by using: widgets/search/mo
Or all of the United States with: widgets/search/us
(This is done simply by replacing the “ALL” wildcard term that Views uses)
Using these arguments from the url, we were able to create meta descriptions and keywords on the fly:
function digitalstax_customizations_nodewords_tags_alter(&$tags, $parameters) {
if(arg(0) == 'widgets') {
if(arg(1) != 'search' && arg(2) != 'us'){
$city = ucwords(str_replace( "-", " ", arg(1)));
$state = strtoupper(str_replace( "-", " ",arg(2)));
$tags['description'] = "Find widgets for sale in " . $city.", ". $state . " at The Widget Place.";
$tags['keywords'] = "". $city .", " . $state . ", widgets, what-nots, the widget place";
}
if(arg(1) == 'search'){
$state = strtoupper(str_replace( "-", " ",arg(2)));
$tags['description'] = "Find widgets for sale in ". $state . " at The Widget Place.";
$tags['keywords'] = "". $state . ", widgets, what-nots, the widget place";
}
if(arg(2) == 'us'){
$tags['description'] = "Find widgets for sale in the United States at The Widget Place.";
$tags['keywords'] = "united states, us, widgets, what-nots, the widget place";
}
}
}
Remove “Split Summary At Cursor” button in Drupal
Apr 17th
We were developing a website that required use by novice users and wanted to eliminate anything that could be confusing. The “Split Summary At Cursor” button on the body field in Drupal served no purpose and could possibly confuse users.
In order to remove it, you must create a module with the following function. Note that our module was named “digitalstax_customizations.”
function digitalstax_customizations_form_alter(&$form, $form_state, $form_id) {
if(isset($form['body_field']['teaser_include'])) {
$form['body_field']['teaser_include']['#access'] = FALSE;
}
}