Posts tagged Views
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;
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";
}
}
}