BTMash

Blob of contradictions

Powering multiple gallery blocks through one via the use of CCK and Views alter

Written

A couple of months ago, I presented at a LA User Group meetup on getting rid of 60 - 70 odd gallery blocks on calarts.edu and getting them all powered by one block. I hadn't had a chance to properly present all the material at that time (and I didn't get a chance to share some/most of the code we'd used to get it all up and running) - I've decided to type out the whole history behind how the idea and the implementation that came about. Its a long read (and forgive me for the formatting of the coded portions - I need to figure out how to get the filters to account for this).

History

Soon after the site had launched, we had gone back to the drawing board to see where things stuck out like a sore thumb and realized that, based on the way we were powering image galleries as blocks, we had a few issues. To start with the simple problem, it starts with creating a new gallery which, while somewhat complicated, was powerful and easy enough for content editors to get through. However, if they wanted to show the gallery as a block on a series of pages:

  • Go to the views edit area and create a new view display
  • Go to the block edit area and adjust the block to show up on certain pages

What should have been relatively simple to set up upon creation of a gallery became a nightmare as:

  1. We use features to manage our views and content editors would have to deal with code push from our testbed -> staging -> production in such a scenario
  2. We wanted to close down access on views to site administrators
  3. Having so many blocks and so many views displays really bogged down the browser to a crawl

Proposal

In short, it just wasn't sustainable in the long run (imagine larger numbers like 100, 200...500, or 1000 galleries - you are now dealing with 1000 blocks and 1000 views displays). So upon thinking long and hard about the issue, our idea involved a few points:

  1. Create a view block display which takes a bunch of gallery IDs as the arguments.
  2. Create a new cck field for galleries where the content editor could then type out (in the same way as for a block) which pages or paths the content would appear (or not appear - we used the methodology offered by the Context module).
  3. Alter the view so that for a given page, check which galleries fit the current path, add it into the view as arguments.
  4. Pray?

This proposal solved a number of issues:

  • Close down on access to views ui.
  • Possibly close down on access for a content editor to manipulate the blocks.
  • For users with access to views ui or the block list page, it would allow for the page load to be much more manageable.
  • Content editors deal with one screen for modifying gallery information as opposed to 3 or more.
  • Inadvertently, it also allowed for the gallery block to be on a consistent area of the site.
  • Inadvertently, it also allowed for easily chaining up multiple galleries together on one path.

While the main site designer felt that the last point may be a possible issue, we decided that in the event that users want to have the gallery available in a different region, we can do so via the old method. So with that, I was given the task of making it all work. The other possible point of contention may be that the block cannot be cached (except on a per-page basis). Since the majority of the users that visit our site are anonymous, this was not an issue.

Implementation

I won't really explain the implementation for the image gallery since I basically use the same recipe as what Jeff Eaton wrote about a few years ago. The only difference this time is that I added a CCK text-area field called gallery_display_paths:

  1. // Exported field: field_gallery_display_paths
  2. $fields['gallery-field_gallery_display_paths'] = array(
  3. 'field_name' => 'field_gallery_display_paths',
  4. 'type_name' => 'gallery',
  5. 'display_settings' => array(
  6. 'weight' => '-1',
  7. 'parent' => '',
  8. '4' => array(
  9. 'format' => 'default',
  10. 'exclude' => 1,
  11. ),
  12. '5' => array(
  13. 'format' => 'default',
  14. 'exclude' => 1,
  15. ),
  16. 'label' => array(
  17. 'format' => 'above',
  18. ),
  19. 'teaser' => array(
  20. 'format' => 'default',
  21. 'exclude' => 1,
  22. ),
  23. 'full' => array(
  24. 'format' => 'default',
  25. 'exclude' => 1,
  26. ),
  27. '2' => array(
  28. 'format' => 'default',
  29. 'exclude' => 0,
  30. ),
  31. '3' => array(
  32. 'format' => 'default',
  33. 'exclude' => 0,
  34. ),
  35. 'token' => array(
  36. 'format' => 'default',
  37. 'exclude' => 0,
  38. ),
  39. ),
  40. 'widget_active' => '1',
  41. 'type' => 'text',
  42. 'required' => '0',
  43. 'multiple' => '0',
  44. 'module' => 'text',
  45. 'active' => '1',
  46. 'text_processing' => '0',
  47. 'max_length' => '',
  48. 'allowed_values' => '',
  49. 'allowed_values_php' => '',
  50. 'widget' => array(
  51. 'rows' => '10',
  52. 'size' => 60,
  53. 'default_value' => array(
  54. '0' => array(
  55. 'value' => '',
  56. '_error_element' => 'default_value_widget][field_gallery_display_paths][0][value',
  57. ),
  58. ),
  59. 'default_value_php' => NULL,
  60. 'label' => 'Path visibility settings',
  61. 'weight' => 0,
  62. 'description' => 'Enter one page/path per line as Drupal paths. The \'*\' character is a wildcard. Example paths are blog for the blog page and blog/* for every personal blog. Start a line with \'~\' if you do not wish for this to appear on a certain set of paths. Example path would be ~node and ~node/* to not appear on any node page). <front> is the front page.',
  63. 'type' => 'text_textarea',
  64. 'module' => 'text',
  65. ),
  66. );

After that, I created two views. One view that would return to me a list of published and accessible galleries on the site; the other was a gallery view block which would show me photos associated with gallery IDs as the argument. The first view was important as this allows for galleries to play with the domain access module (and realistically, it could follow any additional rules we add down the line (so we don't need to maintain the sql queries - hooray!). Once these were set up, we implemented hook_views_pre_view. This is important as this hook is called prior to the view being built out; it lets you add arguments and any other tables/whatnot to join in an easy manner.

  1. /**
  2.  * Implementation of hook_views_pre_view().
  3.  */
  4. function my_media_gallery_views_pre_view(&$view, $display_id, &$args) {
  5. if ($view->name == 'gallery_carousel_side') {
  6. // Let me find out some info...
  7. $path = drupal_get_path_alias($_GET['q']);
  8. $nids = my_media_gallery_get_matching_references($path);
  9. if (count($nids) < 1 && $_GET['q'] !== $path) {
  10. $nids = my_media_gallery_get_matching_references($_GET['q']);
  11. }
  12. if (count($nids) > 0 && count($args) < 1) {
  13. $args[] = implode('+', $nids);
  14. }
  15. }
  16. }

Barring a unhelpful comment, what the above does is figure out matching references based on the actual and aliased paths and the function looks like:

  1. /**
  2.  * Get all matching galleries based on a given path
  3.  */
  4. function my_media_gallery_get_matching_references($requested_path) {
  5. $gallery_paths = views_get_view_result('gallery_paths');
  6. $nids = array();
  7. foreach ($gallery_paths as $gallery_path) {
  8. // The view provides the name of the field and I directly use that.
  9. $temp_paths = $gallery_path->node_data_field_gallery_display_paths_field_gallery_display_paths_value;
  10. $temp_paths = explode("\n", $temp_paths);
  11. $positives = FALSE;
  12. $negatives = FALSE;
  13. foreach ($temp_paths as $temp_path) {
  14. $check_path = trim($temp_path);
  15. if (strpos($check_path, '~') === 0) {
  16. $temp_check_path = trim(substr($check_path, 1));
  17. if (!empty($temp_check_path) && drupal_match_path($requested_path, $temp_check_path)) {
  18. $negatives = TRUE;
  19. }
  20. }
  21. else if (!empty($check_path) && drupal_match_path($requested_path, $check_path)) {
  22. $positives = TRUE;
  23. }
  24. if ($positives) {
  25. $nids[$gallery_path->nid] = $gallery_path->nid;
  26. }
  27. }
  28. if ($negatives) {
  29. unset($nids[$gallery_path->nid]);
  30. }
  31. }
  32. return $nids;
  33. }

Where view gallery_path is the list of galleries that can be seen by the current user on the website. I'll mark down what all the above means at some point but I don't its not particularly difficult to follow (its consists of checking for matching paths and paths that say 'DO NOT PUT THIS GALLERY ON THE CURRENT PAGE!'). There is some level of hardcoded assumptions (name of the view, name of the particular field); however, I also packaged all of this up with features so the view is in the code and I can breathe with a sigh of relief for it all to work well and with minimal changes (other than the filters or argument that may power getting the data).

Conclusion

Like any story (would this count as a short story?), we need to reach a conclusion. Honestly speaking, the content editors and co-workers are very happy with the solution. We are on the verge of shutting down views ui (only to remain enabled on our dev and test servers), we have a bunch of fairly interesting image selections for pages (by chaining together galleries for pages), and enabling new galleries do not require code pushes. And we won't have to worry about 1000+ blocks coming from this functionality (other views and their setup are an entirely different story!)

I have been trying to figure out how to expand on what I wrote so that it can be part of a module users could use to power such blocks on their site. Any ideas on where to go about for such an implementation are very much welcome :)