Pair view fields content on a Drupal 7 view quick howto

Valid for: Drupal 7, Views 3.

I have two multiple text fields on a Drupal 7 content type.

multiple fields

I want to display those fields paired by delta in a view. Here the code I used in my module named mymodule.module.

function mymodule_views_pre_render(&$view) {
  // pair field contents
  if ($view->name == 'my_view_name' && $view->current_display == 'my_view_display') {
    foreach ($view->result as $key => $res) {
      if ($res->field_data_field_youtube_url_delta != $res->field_data_field_youtube_url_description_delta) {
        unset($view->result[$key]);
      }
    }
  }
}

Thanks to hook_views_pre_render().

I use this piece of code to pair Youtube URLs and plain text description  for a JSON output generated via Views Datasource.

Remember to disallow multiple values when adding these fields to your view or you’ll get incoherent results.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s