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:

<?php
/**
 * 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'] = '<![CDATA[' . $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.