0

Let's say $node->field_description[LANGUAGE_NONE][0]['value'] contains 'We're in this together'.

This code doesn't output what I expect.

$node = menu_get_object();
if (isset($node->field_description[LANGUAGE_NONE][0]['value'])) {
  $text = strip_tags($node->field_description[LANGUAGE_NONE][0]['value']);
  $text = decode_entities($text);
}

instead of 'We're in this together', I get 'We're in this together'.

The full hook implementation is the following.

function MY_MODULE_page_alter($page) {
  // Provide list description.
  $node = menu_get_object();
  if (isset($node->field_description[LANGUAGE_NONE][0]['value'])) {
    $text = strip_tags($node->field_description[LANGUAGE_NONE][0]['value']);
    $text = decode_entities($text);
  }

  // Add metadata to describe site's content.
  $description = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array(
      'name' => 'description',
      'content' => $text,
    ),
  );
  drupal_add_html_head($description, 'desciption');
}
1

The function decode_entities works as you expect. It converts the HTML entity (') into a single quote and returns the decoded string.

However, that single quote will be expanded into an entity again at some point downstream, when the page is rendered. This is what inserts (provided you spell "description" correct) the following into your page's <head>:

<meta name="description" content="We&#039;re in this together" />

I would not worry about this. The above is a perfectly valid HTML meta-tag and will be interpreted correctly for SEO purposes, etc.

| improve this answer | |
  • This is how I had it originally. I changed it back above, but still the same result? – dbj44 Mar 14 '14 at 15:29
  • I tested the contents of the function: html_entity_decode($text, ENT_QUOTES, 'UTF-8') on codepad.viper-7.com and it worked fine. It's not my version of PHP either, which is 5.4. Caches cleared. Browser cache cleared. Hmm? – dbj44 Mar 14 '14 at 15:41
  • I'm using the code in an implementation of hook_page_alter(). maybe that is changing it back before rendering? – dbj44 Mar 14 '14 at 15:47
  • Debug: 'we\'re' is displayed in a message, and when I look at the source code in my Chrome browser the output is: We&#039;re. – dbj44 Mar 14 '14 at 15:57
  • 1
    Furthermore, realize that decode_entities($text) is, in Drupal 7 anyway, just a wrapper for returning html_entity_decode($text, ENT_QUOTES, 'UTF-8') so check out us3.php.net/html_entity_decode for lots of further information... – Jimajamma Mar 14 '14 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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