Nov 05 2013
Nov 05

Today we will look at creating a module to allow you to export your views to code. While this concept has been explored on other blogs previously, several important end steps that I find particularly useful have been left out of those discussions. I would like to remedy that here, detailing an entire process for setting up the module, displaying use scenarios, and also showing the changes that this module will make to your views administration process.

The first thing to discuss is why you would even want to export your views to code. There are a few good reasons to do this, but the most obvious one (and the one I personally find the most important) is "protection." Protection of your views and your site.

Standard views don't work until after they're saved on the site. However, since there is no such thing currently as "View Revisions," if the view ends up not functioning (or worse yet, breaks the entire site!) after saving the view, you cannot just go back to a previous version. You typically have to scrap the entire view and start from scratch. If you're just trying to make a list of content, that's not a huge deal, but if you have extremely complicated views with contextual filters, relationships, and accessing multiple parts of your site, this can at best be a headache, and at worse cost you many hours of work.

Storing your views in code allows you to have a safe place to keep a working "backup" of the view. Obviously you can just export your view and save it in a notepad file if you choose, but the method we're going to use not only backs it up, but it uses the backup as a module to display the view.

The Drupal 7 and Drupal 6 methods for this are both extremely similar. I will note the one difference below, but to be clear, this blog is speaking as though you are using Drupal 7 and Views 3.

This blog assumes you know how to create a basic custom module to use as a foundation. If you do not know how to create a basic custom module, check out our quick tutorial on the subject.

1. Hooking into views

Hooks in Drupal are just ways of modifying the website page's results.
Let's create one in our new module.

There are two hooks we will be using in this exercise: hook_views_api and hook_views_default_views. These links will take you to the drupal api pages that describe each hook.

Now let's edit the custom_example.module, and make its contents the following: <?php /* * Implementation of hook_views_api() */ function custom_example_views_api() { return array('api' => 3.0); }

This is all you need to do inside the module file. Note the api of "3.0". Here is where you will make the one change I mentioned above if you are using Drupal 6. Since Views 3.0 was never backported to Drupal 6, you will instead name "2.0" as the api. Here is the code: <?php /* * Implementation of hook_views_api() */ function custom_example_views_api() { return array('api' => 2.0); }

That's it. Now comes the fun part.

2. Providing our views

Now we will hook into 'hook_views_default_views' - basically, the purposes of this hook is to allow a module to provide views that can be read and understood by drupal. This is the main part of the blog that diverges from most other blogs on this subject. Typically, descriptions of how to export views to code end with this section, and the view is input into the hook function. However, that leaves this function very large (with a lot of view information in the function), and also makes it much more difficult to have multiple views. So the steps I will take here will be for the purpose of leaving the function clean, and providing us with a method for having as many views as you want.

Create a new file titled 'custom_example.views_default.inc' and place it in the same folder as your module. Then post the following into the file and look over the comments: <?php /** * Implements hook_views_default_views(). **/ function custom_module_views_default_views() { //Finds all files that match a given mask in a given directory //In our case, looks for any files named *.view in the /views directory $files = file_scan_directory(drupal_get_path('module', 'custom_module'). '/views', '/.view/'); foreach ($files as $filepath => $file) { require $filepath; if (isset($view)) { $views[$view->name] = $view; } } //Check that there are views in the directory //This keeps the site from throwing errors if there are no views to return if ($views) { return $views; } }

What this new hook does is it tells drupal to look into this directory that we've named (/views) and see if there are any files inside the directory using the format *.view. If there are, it takes the name of that view and sets it as the default version of that view. This will make more sense once we get a little further along, but just note that basically, this hook is telling drupal to look for your views in code.

3. Create the /views directory and start a template

Now you need to create the /views directory that is shown in the hook above. Inside your module directory, create a new folder called "views." Now create a new file and place in that directory named "view_template.view." This is an optional step, but one I find useful. Inside the template, simply paste the following: <?php //Paste Exported views Code here //Name file according to the "views_name" in exported view //Example: this_is_my_view_name.view

I use this template in a very simple way - anytime I export a new views code, I copy the exported information and paste it over my comments in the template file. Then I save-as the file with the appropriate view name.

4. An example and the "Revert" option

Let's make a quick example. You just need a list of all content on the site (note, your site already does this, but this is an example that you could literally copy from here and paste into your code and it will work, because I am using no custom cck fields, arguments, roles, etc). Open your template file created above (view_template.view), and copy the following code into the file (you can copy over the commented instructions if you like), and save the file as "all_content.view" (note that the file name is identical to value in the key-value pair on the 2nd line - $view-name = 'all_content'): $view = new view(); $view->name = 'all_content'; $view->description = ''; $view->tag = 'default'; $view->base_table = 'node'; $view->human_name = 'all_content'; $view->core = 7; $view->api_version = '3.0'; $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ /* Display: Master */ $handler = $view->new_display('default', 'Master', 'default'); $handler->display->display_options['use_more_always'] = FALSE; $handler->display->display_options['access']['type'] = 'perm'; $handler->display->display_options['cache']['type'] = 'none'; $handler->display->display_options['query']['type'] = 'views_query'; $handler->display->display_options['exposed_form']['type'] = 'basic'; $handler->display->display_options['pager']['type'] = 'full'; $handler->display->display_options['style_plugin'] = 'default'; $handler->display->display_options['row_plugin'] = 'fields'; /* Field: Content: Title */ $handler->display->display_options['fields']['title']['id'] = 'title'; $handler->display->display_options['fields']['title']['table'] = 'node'; $handler->display->display_options['fields']['title']['field'] = 'title'; $handler->display->display_options['fields']['title']['label'] = ''; $handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE; $handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE; /* Sort criterion: Content: Post date */ $handler->display->display_options['sorts']['created']['id'] = 'created'; $handler->display->display_options['sorts']['created']['table'] = 'node'; $handler->display->display_options['sorts']['created']['field'] = 'created'; $handler->display->display_options['sorts']['created']['order'] = 'DESC'; /* Filter criterion: Content: Published */ $handler->display->display_options['filters']['status']['id'] = 'status'; $handler->display->display_options['filters']['status']['table'] = 'node'; $handler->display->display_options['filters']['status']['field'] = 'status'; $handler->display->display_options['filters']['status']['value'] = 1; $handler->display->display_options['filters']['status']['group'] = 1; $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;

Now here is an important step. Your new view will not show up until the views cache on your site has been cleared. In Drupal 7, go to '/admin/structure/views/settings/advanced' and click the "Clear views' cache" button. In Drupal 6, go to '/admin/build/views/tools' and click the "Clear views' cache" button. Now, when you go back to your list of views, you will see your new view. You should notice one new change as well. If you hover over the options for what you can do with that view, you will see a new option, called "Revert." It will have taken the place of the "Delete" option on any views you had the option for deleting. By clicking this button, it will delete any changes that have been made to the view on the site, and will revert the view back to its default position, which will be the view as it is in code (such is the power of the hook_views_default_view hook). This means, once you have a view that is working, you can save it to code, flush your views' cache, and then edit that view to your heart's content, knowing that if you ever mess anything up too terribly, you can always "Revert" the view back to its working glory. Or for that matter, on a production site, you can allow your clients with limited knowledge of views to attempt to create and edit their own, knowing that if something is messed up, you can easily have it fixed.

I have included two images so you can see what the view would have looked like on the views list page before flushing caches, and what it looks like afterward. Note the difference between "Delete" and "Revert":

Websmiths.co Views Tutorial - Delete Option Before Flushing Cache

Websmiths.co Views Tutorial - Delete Option After Flushing Cache

(Note: When you click "Revert," on the confirmation page that follows, it will still say "Are you sure you want to delete this view?" - this is okay, it is just the wording that was used. If on the views list page, it said "Revert" instead of "Delete," you are safe to revert.)

And that's all there is to do.

Jun 21 2012
Jun 21

Increasingly our web pages are being bookmarked on iOS devices.  This is a good thing, however we had traditionally been using long titles on our pages and these were clumsy for end users when trying to store bookmarks.  Like 90,000 other websites we have chosen Page Title to solve this issue.  It works well with Tokens and Views and is probably one of those modules you should just install.

Here’s your 1:30 video http://www.youtube.com/watch?v=DMvXVJ2mbGQ

and a couple of screen shots for the really curious types.  It’s a simple module to install and use. Made by several of Drupal giants robertDouglass, JohnAlbin, and nicholasThompson.

Turn on page titles in the content type area

Turn on page titles in the content type area

Enjoy refreshing page titles independent of your content title

Enjoy refreshing page titles independent of your content title

May 09 2012
May 09

It is possible to override a specific pager for a specific views using a theme function in your template.php file. Here are the steps needed:

Theme Views Pager Step 1

No matter how you want your pager to look, make sure to select mini pager within views. This will allow you to take advantage of the views pager theme hook patterns. I was unable to get this to easily work when using the full pager.

Depending on how you want your pager to look, you can start with either the code that produces the views mini pager or views full pager using the options below.

Views Mini-Pager

  • In the views module, go into the theme.inc file. This is generally located at /sites/all/modules/views/theme/theme.inc.
  • Copy the theme_views_mini_pager function into the template.php file of your theme.
  • Rename the theme_views_mini_pager function to MYTHEME_views_mini_pager.

Views Full Pager

  • In Drupal 6 core code, open the /includes/pager.inc file.
  • Copy the theme_pager function into the template.php file of your theme.
  • Rename the theme_pager function to MYTHEME_views_mini_pager.

Theme Views Pager Step 2

The next step is to determine which pager theme function you want to override in your template.php file. This can easily be determined by using the following code snippet:

print('<pre>'.print_r($pager_theme,1).'</pre>');die();

This line needs to be dropped in the theme.inc file inside the views module. This is most likely located at /sites/all/modules/views/theme/theme.inc. It needs to be added in the template_preprocess_views_view function, after the pager gets set up in the code. I ended up putting this code at line 99, but can go anywhere after you see this line of code:

$pager_theme = views_theme_functions($pager_type, $view, $view->display_handler->display);

Now if you refresh the page when viewing the page that displays your view, you will get a white screen with a list of theme function names that you can use to override your views pager inside your template.php file. Use the function that is a specific as you need it to be for you particular view and use case, and replace the theme_views_mini_pager or theme_pager function with the correct function name. This will end up looking something like (replacing MYTHEME with the name of your theme and MYVIEW with the name of your view:

function MYTHEME_views_mini_pager__MYVIEW($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 9) {
  //the rest of the pager code goes here
}

Theme Views Pager Step 3

This step is entirely up to you. Customize the Drupal views pager to your hearts content.

Hopefully this helps you next time you need to override a specific pager on a view. If you have any other views theme tricks, drop them in the comments.

Special Note

If you are intending to override the views_mini_pager theme function, you should probably make sure your view name does not contain a capital letter. See http://drupal.org/node/1569886 for more information.

Apr 16 2012
Apr 16

The more I use the activity module the more I like it.  It is a simple module that does one thing well = it keeps track of what is happening on the site, and it leverages other modules like core Triggers, Views, and Tokens to create a useful intuitive activity page.  In our use case we wanted to have the ability to track certain roles working with certain nodes – and having the output styled in views is just icing on the cake.

http://www.youtube.com/watch?v=Ual2-oPLIko

To start with you’ll need to go to /admin/build/activity/create and add activity templates.  Because of how this module was designed I believe it would be pretty simple to extend this module to monitor almost anything.. we just needed to know when certain roles updated or created certain node types, so that was pretty simple.

Setting up an activity template

Setting up an activity template

The use of tokens in the message display section makes for readable and useful messaging

Add tokens for readability

Add tokens for readability

After creating your template(s) you now need to go over to views and add an “Activity” type view – here again – it’s just so cool that you’re extending what is already there and working well… it just makes for an easier time + one less thing to learn!

Adding the Activity in Views

Adding the Activity in Views

With the View going the rules are pretty much the same as for all views – it’s worth noting that if you want to filter by User Role you’ll need to add the “Activity: User” relationship – this adds all of the typical user fields to your filters as well as your fields.  Likewise if you need more information on the nodes you may add the Activity: Node relationship – it’s not always that important since many of the information may be stored in the tokens.  It’s nice to know that you may access those fields if you need to though – just add the relationship and fields + filters as desired.  Nice work on this module folks = well thought out and well implemented.  Thanks!

Setting up the activity views

Setting up the activity views

The final outcome = another view

The final outcome = another view

Mar 28 2012
Mar 28

Views field view allows you to pass an argument to a view and return results.

For the video inclined here’s the 2 minute feed http://www.youtube.com/watch?v=dUoL_ARPLcI

Use Case:  we’ve got a lot of variables being tallied in different views.  Views in general seems an awkward tool for tabulating values.  Tools like views calc and group by work ok for getting a sum or count here and there, but try mixing several together in one view and the errors start to fly.  May just be I don’t have my head around them, in any case they are not intuitive.   For a given organic group node I need to know how many posts there are total (not a problem, that field comes with organic group views integration), and how many of these posts have been completed in a certain way.

Views field view added to fields in view

Views field view added to fields in view

Choose a view and add fields from the view as argument tokens

Choose a view and add fields from the view as argument tokens

Creating this separate view is simple, but now I want to link the main view to the small view tally.  Views Field View let’s you pick a view and pass it an argument to filter the results per row.  In this case we load a node id into our main view and pass the argument over to the Views Field View – creating a new field with the correct info on a per row basis = Nice!

The only thing that doesn’t work is that it does not actually load this view in the order the rows are listed.  Because of this it does not work well with Views PHP.  Although the rows are listed in this global php field the actual data is not available because for some reason the field is not loaded in the order one might imagine it to be.   It’d be really handy to be able to use these variables in calculations, however it’s just not obvious at all how to do that.

Views PHP and VIews Field View don't work together

Views PHP and VIews Field View don't work together

 

Feb 16 2012
Feb 16

Autocomplete for apachesolr views

For the folks who need a working copy of apachesolr autocomplete for use with apachesolr views here’s a repo that’ll help https://github.com/alibama/apachesolr-autocomplete-drupal – bear in mind that it’s just the latest version with patches #13 & 14 listed in the autocomplete issue queue applied.  Just figure it’ll save someone a minute of testing against the latest version of solr views…   It’s also attached in a zip at the end of this post.

Also in today’s two minute vid:  exposed form filters in a block = a great way to handle site search.  The exposed filters become available as a separate block that then points to the page.  We then take the newly created block over to our sitewide context and have a search box on every page that connects to the apachesolr search view….

Exposed form in block

Sitewide Contexts put blocks on every page...

 

 

 

 

 

Apachsolr Views Autocomplete Module

Jan 25 2012
Jan 25

It’s been a while since we looked at Apachesolr Drupal integration.  In large part that is because it “just works.”

It's solr, in a view, with exposed filters, facets, and it brews tea!

With the recent release of new code on Apachesolr Views (Big ups to dstuart, Ravi.J and ygerasimov for the recent contributions… everything seems to be aces) it’s time to revisit the subject.

If you have struggled with theming the search-result.tpl.php file and really don’t want to learn any more about getting great faceted search results you are totlally in luck!  Note to all: we’re using views 3.x-dev, apachesolr 3.x-dev, and apachesolr_views 3.x-dev.

For those of you who are video inclined here’s a ~4 minute screencast

For the rest of you Robert Douglass called it over two years ago in his “views 3 + apachesolr + acquia drupal = the future of search” post and for the most part that’s the deal – if you want to see more screen shots go there.

We’re also using ApacheSolr Custom Fields and the Batch Indexing module (thanks anarchivist)  (as mentioned previously).  Note that at this time the custom field module requires this fix to run… but still a handy module.

Learn to not code with the help of nice mods!

Custom Fields is well worth the minor effort in that it is another piece of the no-coding puzzle.  We also enjoyed having views php (a bit of coding is ok – we used it to set up displays per content type in the view)

Also using better exposed filters again to make the UI for the exposed filter better… gotta love naming conventions!

In any case the working view took about 10-15 minutes to produce, with ~5 minutes spent making tea.

Attached below is our working stack in a zip file

if anyone wants to download the entire package that we’re using go ahead, there are a few known issues including taxonomy facets that still need to be patched in this release, however for many use cases it is good to go
 Solr Stack – Views + Apachesolr + Apachesolr Views + Batch Reindex + Custom Fields

Jan 25 2012
Jan 25

A glossary index is a helpful tool for our patrons to find the journals that they are looking for – building your first glossary can be a struggle so here’s a 2 minute video to walk you through the basics

Working in the arguments region in views

Once we had the block built we attached it to our view of titles using contexts.  Another way that would have been acceptable would have been Views Attach.  I wanted to use the attached views, however I wasn’t entirely certain about all possible use cases, so we just went with  a block and joined everything up using contexts.

A path, node type, and taxonomy based context

And yes, we could have added “views” conditions as well, although it is already pretty much overkilled…  in any case it’s another beautiful day with contexts & views

Jan 24 2012
Jan 24

Exposing filters in a view is a great way to add interactivity to lists. Out of the box Drupal Views lets you use CCK select lists as an end user visible filter.  The downside is that the select list is a clunky user interface.  To select multiple options you have to hold down the control button while mousing around – not so bad with a short list, but rapidly unmanageable with a longer list.

To solve this user experience conundrum mikeker provided the new and improved exposed filters… aptly called “better exposed filters“  – one thing I like about drupal is that the naming conventions tend to be fairly obvious.

Here’s a brief ~2 minute walkthrough of setting it up

Before Better Exposed Filters

After Better Exposed Filters

Note: you do of course have to expose the filter in views before being able to use the “better” exposed filter…

The tools are available in a new block within your view

Initially after turning the module on I expected to see the new options available from within the filter section of the view, however the module creates a new menu region within views… afterwards style the output as you see fit with css – such that happiness may reign.  any questions or comments welcome

Dec 21 2011
Dec 21

Sometimes you need to connect several views – perhaps based on unique permissions or perhaps to improve functionality and layout in a way that blocks might not permit.  With Views Attach you are able to link together multiple views – assign permissions per view, as well as pass along arguments and exposed filters… We use these in many places on our site for things like glossaries as well as to link our blocks to our page management dashboards.  This is a brief tutorial on how and when to use attached views…

A look at a view attached to another view

NB from the video – don’t forget to check your permissions – if “Access All Views” is enabled for a given role it will override the permissions within the particular view

Dec 08 2011
Dec 08

This post describes generally how to create ajax paging comments, how to package the created comments into a feature, and how to override default views created by modules.

Ajax Comment Pagination

You know the biggest problem with comments? That's right, getting them to page using ajax so that users watching videos don't have to reload the page. Good answer!

So how does one go about making this happen in Drupal 6? Very easily actually. One simply makes their comments load in a view, and uses Views built in ajax to handle the pagination.

In Drupal 6, using the module CommentBlock will handle all the heavy lifting for you by providing a view for comments as a block, and preventing the output of comments on node pages.

So to start, download and install that module, then place the Comments and Comment Form blocks it provides in the content region of your site.

Next, visit the views page and edit the Comments view with the following settings:

  • Use Ajax: Yes
  • Use Pager: Yes
  • Items to display: 50 (or however many you want)

Save the view and voila! you now have comments that page using ajax.

Wow! you say? That's so awesome I want to use it on all my sites!, you say? No problem.

Enter Features

If you're interested in reusing site aspects you create on other sites, you must learn the Features module. It can package different components of your site into a downloadable and easily deploy-able psuedo-module for you.

Additionally, grab the Strongarm module. It lets you save drupal variables into features, and in this specific case is necessary.

After installing those modules visit your feature's page (Site Building » Features) and create a new feature. Name the feature, describe it and give it a version number, then add the following components.

  • Dependencies: CommentBlock, Views
  • Strongarm: clean_url

The reason we added the clean_url variable is because features that only have dependencies don't show up correctly on the features page.

Click Download, and save the feature you created.

Overriding default views in a Feature using hook_views_default_views_alter()

This is where it gets a little tricky. Since the commentblock module provides the Comments view as a default view within the module, we can't export the modified view into a feature, we're going to have to override that default view with code. Lucky us, one great thing about features is that you can add real module code into them!

First, go back to the comments view you edited earlier and click on 'Export'. Just keep that page open.

In your freshly created and downloaded feature, open the module file in your favorite editor, and add the following at the bottom of the file.

<?php /* * Implementation of hook_views_default_views_alter() * * Override the view provided by commentblock module * http://drupal.org/node/1014774 */ function FEATURENAME_views_default_views_alter(&$views){ // Alter only the 'comments' view. if (array_key_exists('comments', $views)) { /* * How To: * * 1) Install the commentblock module and edit the view provided as desired (comments) * 2) Export the edited view and paste the code below * 3) Cache flushing likely needed */ // paste exported view here. /* * Stop. Do not remove or change */ // Override the existing view with this new definition. $views['comments'] = $view; } }

Now simply go back to the page where you have exported the edited Comments view and copy all of the code provided in the textarea.
Then paste that code in the feature function above where it says 'paste exported view here.

Note: You'll need to replace the FEATURENAME in the function above with the folder name of your feature.

Save that file, and there you have it. Now to have ajax comment pagination on any other drupal 6 websites you need only to have the modules commentblock, views, features, and strongarm, along with your newly created feature.

I know that's a lot of steps crammed into a small blog post, but if you don't need to reuse the feature on other sites you only need to follow the first section.

Happy hunting!

Sep 22 2011
Sep 22

Tomorrow is the last day of Summer but the Drupal training scene is as hot as ever. We’ve scheduled a number of trainings in Los Angeles this Fall that we’re excited to tell you about, and we’re happy to publicly announce our training assistance program.

First, though, we’re sending out discount codes on Twitter and Facebook. Follow @LarksLA on Twitter, like Exaltation of Larks on Facebook or sign up to our training newsletter at http://www.larks.la/training to get a 15% early bird discount* toward all our trainings!

Los Angeles Drupal trainings in October and November, 2011

Here are the trainings we’ve lined up. If you have any questions, visit us at http://www.larks.la/training or contact us at trainings [at] larks [dot] la and we’ll be happy to talk with you. You can also call us at 888-LARKS-LA (888-527-5752) with any questions.

Beginner trainings:

Intermediate training:

Advanced trainings:

All our trainings are $400 a day (1-day trainings are $400, 2-day trainings are $800, etc.). We’re excited about these trainings and hope you are, too. Here are some more details and descriptions.

Training details and descriptions

   Drupal Fundamentals
   October 31, 2011
   http://ex.tl/df7

Drupal Fundamentals is our introductory training that touches on nearly every aspect of the core Drupal framework and covers many must-have modules. By the end of the day, you’ll have created a Drupal site that looks and functions much like any you’ll see on the web today.

This training is for Drupal 7. For more information, visit http://ex.tl/sbd7

   Drupal Scalability and Performance
   October 31, 2011
   http://ex.tl/dsp1

In this advanced Drupal Scalability and Performance training, we’ll show you the best practices for running fast sites for a large volume of users. Starting with a blank Linux virtual server, we’ll work together through the setup, configuration and tuning of Drupal using Varnish, Pressflow, Apache, MySQL, Memcache and Apache Solr.

This training is for both Drupal 6 and Drupal 7. For more information, visit http://ex.tl/dsp1

   Drupal Architecture (Custom Content, Fields and Lists)
   November 1 & 2, 2011
   http://ex.tl/ccfl1

Drupal Architecture (Custom Content, Fields and Lists) is our intermediate training where we explore modules and configurations you can combine to build more customized systems using Drupal. You’ll create many examples of more advanced configurations and content displays using the popular Content Construction Kit (CCK) and Views modules.

This training is for Drupal 6. For more information, visit http://ex.tl/ccfl1

   Developing RESTful Web Services and APIs
   November 3, 4 & 5, 2011
   http://ex.tl/dwsa1

Offered for the first time in Southern California, Developing RESTful Web Services and APIs is an advanced 2-day training (with an optional third day of additional hands-on support) for those developers seeking accelerated understanding of exploiting Services 3.0 to its fullest. This is THE training you need if you’re using Drupal to create a backend for iPad, iPhone or Android applications.

This training covers both Drupal 6 and Drupal 7. For more information, visit
http://ex.tl/dwsa1

Training assistance program

In closing, we’d like to tell you about our training assistance program. For each class, we’re setting aside a limited number of seats for students, unemployed job seekers and people in need.

For more details about the program, contact us at trainings [at] larks [dot] la and we’ll be happy to talk with you. You can also call us at 888-LARKS-LA (888-527-5752) with any questions.

* Our early bird discount is not valid toward the Red Cross First Aid, CPR & AED training and 2-year certification that we’re organizing. It’s already being offered at nearly 33% off, so sign up today. You won’t regret it and you might even save someone’s life. ^

Nov 21 2010
Nov 21

This video tutorial shows you how you can utilize filters to limit the content that is displayed in a view. This includes how to expose a filter so that your site visitors can filter the content as well. 

Note: Click the 'full screen' icon (to the right of the volume control) in order to watch online at full 1280x720 resolution.

Video Links
Jul 24 2010
Jul 24
Printer-friendly versionPDF version

If you're like most people who build using Drupal you want to build your site around nodes, that have titles, descriptions, tags, comments, etc. It's no surprise since those types of content oriented sites are the ones that make the world of the web go round. But what about another type of site that focuses not on nodes but on data contained in the Drupal database? Can you do it? Yes. Should you do it? Only if that sort of thing excites you or perhaps if a client requests it.

I'll give you a little background on where I'm coming from and what made me decide to play around with Drupal as a way to expose data sets. About a decade ago I was part of a group of people who built a first of its kind application to send and receive data from wholesale power markets in North America. The application was built on three tiers: a client, an application server, and a database server. The client and application server were written in Java and the database relied on Oracle technology. It was about as far away from open source as you could get. The product was successful though and we did hundreds of installs all across North America. What the product basically did for its owners was suck in data from a central server then expose that data to the user via tables and charts visible from the user interface. At some point last year I started wondering if I could do something similar with Drupal. So I started investigating, tried out different modules and realized that I could do even more with a Drupal install than we did with that product.

There are four tasks that need to be accomplished if you want to use Drupal as an effective data platform.

  1. Capture data to the Drupal database tables
  2. Setting the data types (text, numeric, etc.)
  3. Expose the data to the user using tables and charts
  4. Offer simplified download format options

 Capturing Data

I have been capturing data using the Table Wizard module. Table Wizard allows you to create a database table by uploading a delimited (like tab or comma separated) file. This works pretty well. I recently uploaded a file with over 300,000 records and it was in the database within a few minutes. After you upload your file you click on the table name to "analyze" the table. Table Wizard will let you know what columns you have in the table, the data types and identify the primary key. When you first upload your data you will want to go switch over to your database to change data types of the appropriate ones and identify a primary key. Once you have done that you can come back and re-analyze the table. 

Setting the Data Types

This is probably the step that will be the least welcome to those who are not familiar with databases. If you want to do extensive work with data in Drupal then you will want to become familiar enough with phpMyAdmin and MySQL data types to be able to navigate to your database and apply the appropriate settings for each type of data. You should also indicate which field contains the primary key for the table. The primary key is a unique value that identifies each record in a table. If you have a dataset that does not have a column with unique values then consider adding a column unique numeric code in each row. You can name the column something like record_id and then add values like 100001, 100002, 100003 and so on.

Expose the Data Using Tables and Charts

If you have been a good boy or girl and added a single field primary key then the Table Wizard module will let you check a box that is labeled, "Provide default view." This option automatically creates a view that you can expand upon as you see fit. Auto created table wizard views will have the tag "tw" added to them. You can also create a view by navigating to /build/views/add and looking for the radio button in the 'View Type' section that starts with Database table. At this point you need to a thing or two about the Views module to go further. There's not enough space to cover that here so I'm going to skip over the details and point you to the key modules and settings.

The standard views module will let you create a table from your data. You just have to choose a Page display and choose the Table style. You can then choose your fields which will appear as columns within the table. Give the page a Path and perhaps add an Exposed Filter or two to allow users to narrow down the data they are looking at. If you want to get a little fancy you can choose use Views Calc to create tables that include calculations like min, max or average on a set of numeric data.

Charts are a little more tricky but it can be done with the assistance of Charts and Graphs and Views Charts. Charts and Graphs allows you to integrate a number of different free charting solutions (including Google Charts) into your site. View Charts makes those features available as a Style (called "Charts") within the Views interface. The chart gets exposed on a page based on the Path that you define. You can also create a chart within the Block display type. There are other charting modules that exist for Drupal. All of them have some drawbacks but so far the Charts and Graphs/Views Charts combo has worked the best for me.

Offering A Download Option

The ability to download data probably won't be a must have feature for every site. After all someone could probably just copy the table and paste it into their favorite spreadsheet program. It's a nice feature to offer though and you can do it pretty easily with the help of the Views Bonus Pack module. With Views Bonus Pack installed you get the option to add a Feed display within the Views interface. With the Feed display selected you then get the option of setting a Style that includes the option for CSV and other popular formats like TXT, DOC, XLS and XML. You can then attach that display to the Table display and there will be an image that appears at the bottom of the table page that will generate a file download when clicked. Be sure to add the same filters to the CSV display that you have added to the Table display so the downloaded file reflects what the user is seeing with any filters selected. 

The usual caveats apply to the ideas and tips that I've offered here. At any time a better module could come along or an existing module could change and alter the process a bit. So tread lightly and do some exploration before you commit to building a data driven site using Drupal. In fact, even though there was a very recent release of Table Wizard the development is being deprecated in favor of the Data module. I'll continue to upload using Table Wizard for now since I think it works well but also plan on giving the Data module a shot in the near future. 

As always I'd love to hear your thoughts and tips on modules and workflows in the comments. If you have built or know of a good data access website built using Drupal feel free to share those links as well.

Video Links

YouTube Version

Flash Version

Quicktime Version

Mar 11 2010
Mar 11

There are several ways you can theme a view on Drupal, and this tutorial will describe a simple and quick way to accomplish just that. I assume that you’re familiar to working with Views and CCK, so I won’t go in much detail on the process of creating the view and content type.

For this example I’m creating a block simple view that will display a teaser for the 3 latest blog posts.

read more

Feb 28 2010
Feb 28

The Nodequeue module is an incredibly useful module that allows you select individual nodes and place them in a list in whatever order you like. This lesson shows you how to create a new nodequeue, add nodes to the queue, arrange the queue and then add the queue to a view.

Note: Click the 'full screen' icon (to the left of the volume control) in order to watch online at full 1280x720 resolution.

Video Links

Flash Version

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web