Restored guide

Removing Column View Unauthenticated Users Using Hook Views Pre Build

Archived 2008–2011 · 2 min read · Eric London's Drupal Blog

Use this as a field note: the path is the historical one, and the steps are a clean-room explanation of the same Drupal problem. The note is titled “Removing Column View Unauthenticated Users Using Hook Views Pre Build” and is written for Drupal 6.x unless a line below says otherwise.

What you are solving

Views is the display layer. Prefer a display plugin (page, block, or attachment) over copying a query into a template. Pass arguments explicitly, and theme the display only after the query returns the rows you expect. The moving parts in the historical title are: removing, column, view, unauthenticated, users, using, hook, views, pre, build.

APIs and modules in play

  • Views 2 displays and views_embed_view()
  • Views arguments, filters, and theme suggestions

A practical approach

  1. Step 1. Name the outcome in one sentence: Removing Column View Unauthenticated Users Using Hook Views Pre Build.
  2. Step 2. Reproduce it on a development copy with the same modules enabled.
  3. Step 3. Keep the change in a custom module or in the theme, and leave contributed code untouched.
  4. Step 4. Build the display in the Views UI until the preview is right, then embed or theme that display.
  5. Step 5. Pass contextual arguments in order, and test the empty case as well as a matching case.

Example

The sample is illustrative. Rename the function, swap in your machine names, and run it on a development copy.

// Removing Column View Unauthenticated Users Using Hook Views Pre Build
function notes_removing_column_view_unauthenticated_users_using_embed($argument = NULL) {
  $view = views_get_view('related_nodes');
  if (!$view) {
    return '';
  }
  $view->set_display('block_1');
  if ($argument !== NULL) {
    $view->set_arguments(array($argument));
  }
  return $view->preview();
}

Check before you deploy

  • Confirm the change on a database copy, then again with the theme registry and page cache cleared.
  • Watch the PHP error log and the watchdog table for the first real request.
  • If the change alters markup, recheck the path with a keyboard and without JavaScript.

Restored guide. The original post was published by Eric London on this domain. This text is a new explanation of that subject, not a reproduction of the archived wording.