0

I've created a custom block, and I'm trying to get this custom block to output a specific piece of HTML. The HTML needs to be different on every page, because it needs to include the page's URL in it. For instance, if the page is http://www.businesskorea.co.kr/article/7789/content-business-marginalized-samsung-reorganizes-media-solution-center-owing-poor, then the block should output:

<img src="http://cp.news.search.daum.net/api/publish.json?nurl={http%3A%2F%2Fwww.businesskorea.co.kr%2Farticle%2F7789%2Fcontent-business-marginalized-samsung-reorganizes-media-solution-center-owing-poor}&xml_url={http%3A%2F%2Fwww.businesskorea.co.kr%2Farticle%2F7789%2Fcontent-business-marginalized-samsung-reorganizes-media-solution-center-owing-poor%3Fformat%3Dxml}&channel=business" style="display:none;">

This is to be compatible with daum.net's news publishing functionality. The code that I've written up for this custom block looks like this:

<?php

/**
* Implements hook_block_info().
*/

function bkcustom_block_info() {
  $blocks = array();
  $blocks['bkcustom_daum_json_block'] = array(
    'info' => t('BKCustom Daum JSON'),
  );
  return $blocks;
}

/**
* Implements hook_block_view().
*/

function bkcustom_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'bkcustom_daum_json_block':
      $block['subject'] = '';
      $block['content'] = BKCUSTOM_DAUM_JSON_BLOCK_CONTENT();
      break;
  }
  return $block;
}

function BKCUSTOM_DAUM_JSON_BLOCK_CONTENT() {
  $path_alias = drupal_get_path_alias($_GET["q"]);

  $encoded_path_alias = urlencode($path_alias);

  $output = t("<img src=\"http://cp.news.search.daum.net/api/publish.json?nurl={http%3A%2F%2Fwww.businesskorea.co.kr%2F" . $encoded_path_alias . "}&xml_url={http%3A%2F%2Fwww.businesskorea.co.kr%2F" . $encoded_path_alias . "%3Fformat%3Dxml}&channel=business\" style=\"display:none;\" />");

  return $output;
}

Which outputs almost, but not quite, what I need. The output changes ampersands to &amp;, and somehow deletes the final forward slash (/) at the end of the IMG tag. The output looks like this:

<img src="http://cp.news.search.daum.net/api/publish.json?nurl={http%3A%2F%2Fwww.businesskorea.co.kr%2Farticle%2F7789%2Fcontent-business-marginalized-samsung-reorganizes-media-solution-center-owing-poor}&amp;xml_url={http%3A%2F%2Fwww.businesskorea.co.kr%2Farticle%2F7789%2Fcontent-business-marginalized-samsung-reorganizes-media-solution-center-owing-poor%3Fformat%3Dxml}&amp;channel=business" style="display:none;">

The most frustrating part of this exercise is that if I delete the opening and closing brackets, <>, the code works perfectly and prints out each character exactly as I need it to. That's how I tested to see if it would work. But when I introduce opening and closing brackets, the code changes and the JSON query or whatever the crap this is fails.

How can I preserve the & and / characters?

2 Answers 2

0

t function is for texts, not for HTML, so it is perfectly allowed to fail. Also, you should not use dynamic strings in t's first argument at all, it is meant to be static.

Whenever possible, you should use renderable arrays. In your specific case this seems a bit difficult, so first thing to, if you are willing to settle for quick and dirty, do would be just forget t and output tag directly.

1
  • I haven't had a chance to get back to this until now. When I remove the t, the output remains the same. ampersands are converted into &amp;. I'm going to look into renderable arrays. Dec 16, 2014 at 8:49
0

I ended up solving this problem by abandoning the custom module idea and doing the work in the theme layer - in node.tpl.php. I should have done that from the beginning I guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.