Hi, How can I add <![CDATA[ at the start of some of the item elements?

Comments

maciej.zgadzaj’s picture

Status:Active» Fixed

You would need to create relevant preprocess functions for those elements, and add CDATA wrapper there. See views_rss_core_views_rss_item_elements() and content of views_rss_core.inc for some examples.

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary:View changes

Updated issue summary.

truantology’s picture

Issue summary:View changes

Did you ever fix this, ermelandus? If so, care to share your code?

Thanks!

wonder95’s picture

For what it's worth, here's how I was able to do it. I needed to add a custom 'content' element, and then add the CDATA tags to the description and content elements.

/**
 * Implementation of hook_views_rss_item_elements()
 */
function nb_alters_views_rss_item_elements() {
	$elements['alertID'] = array(
		'title' => t('Alert ID'),
    'description' => t('Used to identify the node in the Cyber Alert.'),
  );

	$elements['content'] = array(
		'title' => t('Content'),
		'description' => t('The body of the blog post.'),
	);

  return $elements;
}

/**
 * Implementation of views_rss_preprocess_item()
 */
function nb_alters_views_rss_preprocess_item($vars) {
	// Add CDATA tags to the standard description and custom content item elements.
	$vars['item']['views_rss_core']['description'] = '<![CDATA[' . $vars['item']['views_rss_core']['description'] . ']]>';
	$vars['item']['nb_alters']['content'] = '<![CDATA[' . $vars['item']['nb_alters']['content'] .  ']]>';
}

The only weird problem is that my '<' is being converted to the HTML entity (&lt;), so I need to figure out how to get around that, but this will at least give you an idea how to add the tags to the item.

mpv’s picture

In #2393461: format_xml_elements() does not allow unencoded values there's patch for format_xml_elements that allows to avoid the encoding by setting an encoded property in the xml element.

wonder95’s picture

@mpv, I saw that, but I'm not seeing where the "encoding" property gets set on the element so that patch works.

mpv’s picture

@wonder95 you can set the encoded property in the same preprocess functions you are adding the CDATA wrapper. I am using something like this:

/**
 * Implements hook_views_rss_item_elements_alter().
 */
function MY_MODULE_views_rss_item_elements_alter(&$elements) {
  $elements['views_rss_core']['title']['preprocess functions'][] = 'MY_MODULE_cdata_wrapper';
  $elements['views_rss_core']['description']['preprocess functions'][] = 'MY_MODULE_cdata_wrapper';
}

/**
 * Preprocess function for wrapping element with CDATA.
 */
function MY_MODULE_cdata_wrapper(&$variables) {
  $value = $variables['elements'][0]['value'];
  if (!empty($value)) {
    $variables['elements'][0]['value'] = '';
    $variables['elements'][0]['encoded'] = TRUE;
  }
}

This together with the patch in #2393461: format_xml_elements() does not allow unencoded values is working for me.

wonder95’s picture

Awesome!! That did the trick. I was close with what I had, but this works perfectly.

aedwards88’s picture

Could the same be accomplished by just creating a module with a respective Field Formatter for the cdata representation. Thereafter setting the field formatter on the field which is to display the cdata representation of the text.

SnackyStacky’s picture

@wonder95 and/or @mpv: I have been struggling with this for a week now.

I am able to get what @wonder95 had originally that creates the

Should I only be using the code shown by @mpv? Or do they both go into a single .module file? I notice in @mpv's code, there are empty quotes and brackets. Should something be going in there?

Any help would really be appreciated. :)

wonder95’s picture

@SnackyStacky; you need @mpv's code (his was an update to mine) and the core update he referenced. His code will not work without the update to common.inc. That issue has yet to be committed (due to some documentation for the change missing), so I've been having to manually re-apply that patch after every D7 core update. However, @mpv just submitted a patch with the required documentation, so hopefully it will be committed to core soon.

FWIW, here's my final code:

/**
 * Implements hook_views_rss_item_elements_alter().
 */
function nb_alters_views_rss_item_elements_alter(&$elements) {
	// Define custom preprocess function to use for wrapping content in CDATA tags and removing tokens.
	$elements['nb_alters']['content']['preprocess functions'][] = 'nb_alters_cdata_wrapper';
	$elements['views_rss_core']['description']['preprocess functions'][] = 'nb_alters_cdata_wrapper';
}

/**
 * Preprocess function for wrapping element with CDATA.
 */
function nb_alters_cdata_wrapper(&$vars) {
	$value = $vars['elements'][0]['value'];
	if (!empty($value)) {
		$vars['elements'][0]['value'] = '<![CDATA[' . htmlentities($value, ENT_QUOTES, 'UTF-8') . ']]>';
		$vars['elements'][0]['encoded'] = TRUE;
	}
}
SnackyStacky’s picture

@wonder95 - THANK YOU!!!!! SO MUCH!!!!!

I did apply the common.inc patch, so it's been driving me absolutely bonkers that I cannot get this to work. I really appreciate your follow up! I'm out of the office now, but I will be working with this as soon as I get back.

Thank you, again!

SnackyStacky’s picture

It worked!!!!

Thank you!!!!