Is there a way to use views field data in Channel core elements? I run an art gallery and I'm trying to customize each artist's gallery feed with their name as the channel title or description, instead of the site name / mission.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dandaman’s picture

This would be nice. For example, I'd like it to create a podcast feed for every item in the taxonomy, so if it could take some fields of the first row and use them to populate the channel fields, that would make this so I don't have to create a separate view display for each feed.

dandaman’s picture

Speaking of which, is it even possible to make the Core elements set from a field within the row results? I was trying to go through the code to see if that's doable, but I'm not sure if I see how this could be done by adding some code even within the framework of this module so far. Maybe I'm missing something, though.

Also, I'd like to do this on 7.x-2.x, not 6.x-2.x, but I think it's mostly the same thing anyways, right?

lzimmerman’s picture

I have been looking at this for the 7.x version, and have the start of a (hacky) solution.

In views_rss_core.inc (Core Elements preprocessing section), I have added this function:

function check_replacement_patterns($variables, $field) {
  // Get tokens from view.
  $row_tokens = $variables['view']->style_plugin->row_tokens[0];
  // Loop through tokens, check for match between token name and channel field text.
  foreach ($row_tokens as $token => $value) {
	// If match is found, return token contents.
	if ($token == $field) {
		// Found token in field text; return token contents.
		return $value;
		}
  }
  // Otherwise, return field text as entered.
  return $field;
}

Then in element preprocessors, we can use the function to check for a token:
check_replacement_patterns($variables, $variables['elements'][0]['value']);

For example, an altered preprocessor:

/**
 * Preprocess function for channel <description> element.
 */
function views_rss_core_preprocess_channel_description(&$variables) {
  if (empty($variables['elements'][0]['value'])) {
    $variables['elements'][0]['value'] = variable_get('site_slogan', '');
  }
  // Added 'else': check for entered tokens if description is not empty
  else {
  // Check replacement patterns
  $variables['elements'][0]['value'] = check_replacement_patterns($variables, $variables['elements'][0]['value']);
  }
}

Other element preprocessors (categories, image) may need more tweaking, but maybe this example can serve as a jumping-off point.

I would guess that a similar function would work for 6.x also.

(N.B.: The end of the URL for a Fields item is likely the token text to use in brackets, e.g. 'body_1' or 'nid'.)

dandaman’s picture

Nice! That might be a good start on a patch to make it a real feature and not a hack.

lzimmerman’s picture

I used this function as a starting point for including replacement patterns for almost all channel elements, and the result looks to be a success so far.

The code feels pretty messy, though - I don't know whether it's patch-worthy at this point. As it is I have several replacements tossed around several files in the Views RSS and Views RSS iTunes modules.

Maybe if a preprocess function could be run on *all* text-field-based Views RSS fields, just running the check_replacement_patterns() function and then moving on to any standard preprocessing.

Such an approach that might need just two short additions - the function itself and an extra field preprocessor running the function.

rtdean93’s picture

@lzimmerman - Can you provide a patch or document what you have done? I need this for my D7 taxonomies.

Thanks

Bobby

lzimmerman’s picture

Hi Bobby,

The code in this thread is what we used, adjusted to suit the different fields that needed to allow views field tokens. Some of ours involved hard-coding site-specific data, which makes the code non-transferrable.

But most fields just used the model already described (example is for the "description" field):
1. Create a function to check for views replacement patterns.
2. Adjust the preprocessor for each field that should require tokens.

You can add any extra cleanup or alteration in the preprocessor function also.

kenheim’s picture

Issue summary: View changes

I'd like to see this feature added. It would make creating multiple feeds so much simpler.

maciej.zgadzaj’s picture

Category: Support request » Feature request
Status: Active » Closed (won't fix)

Using row values for channel elements is not logical to make it into the module, therefore for this part the issue status is "won't fix".

Regarding tokens, I would be happy to apply a patch if anyone submits one (as with any feature request, which this is really). Please feel free to reopen this issue when providing a patch.

pookmish’s picture

Maybe this will work. I'm using 2 contextual filters on a rss feed and i'm able to populate the description & other fields based on a field on the rows. I only got it to pull the data for the last row. If i get time i'll work on it a little more. but it solves this issue for me right now.

ron_s’s picture

Title: Use fields/tokens in Channel core elements? » Token support for channel core elements
Version: 6.x-2.0-rc3 » 7.x-2.x-dev
Component: Documentation » Code
Status: Closed (won't fix) » Needs review
FileSize
6.57 KB

@maciej.zgadzaj, attached is a patch for review only including tokens. This leverages Token's token_tree to define basic options (site, current user, etc.), and then appends tokens for any entities included in the View (node, user, etc.).

I'm using token_tree rather than token_tree_link because of the problems that occur trying to load an Ajax-based window in Views when using live preview.

Also leveraged the regex used by Token to perform matches, and cleaned up the _validate function a bit.

Let me know your feedback, thanks.

ron_s’s picture

After performing some further testing, realized there were a few additional places the token support needed to be added to preprocess functions. We've now created a central views_rss_token_replace function that is used in each location.

Please review the attached files. Thanks.