Drupal7: Override a View Row Field Result Programmatically

I have a large project with I believe hundreds of modules … I’m too afraid to check! haha. And the client has asked me to allow a blog entry submitted locally as a Drupal piece of content or remotely from an external url. Listing this content in Views should show the correct html link to the content — local or remote. Beyond reading further I recommend you look at Display Suite, or Contextual View Modes. I did this because I can hardly afford to install more modules on this site.

Beyond here lies madness! (this approach is documented, yet I find very fragile, full of obscure gotchas …)

As I said I have a content type blogentry it has the following fields:

Crucial blog fields for remote / local support of blog entries (some non-critical fields omitted).

Crucial blog fields for remote / local support of blog entries (some non-critical fields omitted).

If the user creates the blog post locally I want to link to the internal path of the Node using Views of published blog entries. If the user creates a blog entry remotely I want to link to the supplied remote url.

To do this I created a View with: the local/remote status, the blog title, author name, local path and remote path — and additionally an extra view field that will be the true destination url.

Then using the Views API I inform my View that the extra field has a custom preprocess function and custom template file. I followed the documentation on this page under the section titled Themeing Views From Your Module.

Note, this documentation clearly states if you miss any steps of the override process listed — it will break in obscure and vague ways (good luck).

I also referenced this blog post by Appnovation while creating this site feature, it is similar in content to the official Views documentation above (I like to read more than 1 source of material implementing a technique — as both this blog entries had errors in their syntax in filename and function name usage with Views conventions).

Custom code to implement this site feature includes:

mymodule.module file

// Provide custom theme pre-processor functions for Views.
// http://www.appnovation.com/blog/theming-view-within-your-module
// http://views-help.doc.logrus.com/help/views/api-default-views
// note: the directions on both pages confuse _ and __ in the function names.

function student_blogs_theme($existing, $type, $theme, $path) {
  $themes['views_view_fields__student_blogs_testtest2__page_1'] = array(
    'template' => 'templates/views-view-fields--student-blogs-testtest2--page-1',
    'original hook' => 'views_view_fields',
    'preprocess functions' => array(
    'arguments' => array('view' => NULL, 'options' => NULL, 'row' => NULL),
    'path' => drupal_get_path('module', 'student_blogs'),
  return $themes;

function student_blog_preprocess_views_view_fields__student_blogs_testtest2__page_1(&$vars) {
#  dpm($vars, __FUNCTION__);
#  dpm($vars['fields']);

  if ($vars['fields']['field_blog_is_hosted']->content === 'remote site') {
#    dpm($vars['fields']['field_blog_destination_url'], 'desturl');
    $title = $vars['fields']['title_1']->content;
    $user = $vars['fields']['name_2']->content;
    $link = $vars['fields']['field_blog_remote_url']->content;
    $vars['fields']['title']->content = <<<EOFHTML
<span class="field-content"><a href="$link" target="_blank" class="active eapextblogurl">$title</a></span>




 * @file
 * Default simple view template to all the fields as a row.
 * - $view: The view in use.
 * - $fields: an array of $field objects. Each one contains:
 *   - $field->content: The output of the field.
 *   - $field->raw: The raw data for the field, if it exists. This is NOT output safe.
 *   - $field->class: The safe class id to use.
 *   - $field->handler: The Views field handler object controlling this field. Do not use
 *     var_export to dump this object, as it can't handle the recursion.
 *   - $field->inline: Whether or not the field should be inline.
 *   - $field->inline_html: either div or span based on the above flag.
 *   - $field->wrapper_prefix: A complete wrapper containing the inline_html to use.
 *   - $field->wrapper_suffix: The closing tag for the wrapper.
 *   - $field->separator: an optional separator that may appear before a field.
 *   - $field->label: The wrap label text to use.
 *   - $field->label_html: The full HTML of the label to use including
 *     configured element type.
 * - $row: The raw result object from the query, with all data it fetched.
 * @ingroup views_templates
<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>

  <?php print $field->wrapper_prefix; ?>
    <?php print $field->label_html; ?>
    <?php print $field->content; ?>
  <?php print $field->wrapper_suffix; ?>
<?php endforeach; ?>


a.eapextblogurl {
    background: url(/profiles/eap_base/modules/custom/eap_student_views/student_blogs/assets/external_link_icon.png) center right no-repeat;
    padding-right: 18px;

All this madness allows a Views row content to change display (link destination) based upon the content of the row. Again, trying to leverage other modules to perform this logic — planning this as a feature in your site from the getgo is more advised than this approach. This is cumbersome and fragile!

This image shows that a remote url goes to a remote site, and the local blog entry goes to a local content url.

This image shows that a remote url goes to a remote site, and the local blog entry goes to a local content url.

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *