I'm using the wysiwyg module, wysiwyg filter module, and tinymce. When my rendered node content finally gets passed to ad_embed for it to insert ads automatically after X paragraphs, the content is already translated into HTML. Because of this the search you are doing for "\n" on line 311 of ad_embed.module does not return the correct position, and my ads do not get inserted where I'd like them.

I think this module either needs to be aware of input formats, which filters are assigned to which format (the "Line break converter" will replace line breaks with BR or P tags), which format is assigned to which node, and if wysiwyg editors are present which might also strip out line-breaks.

I was able to rewrite this function to test for a P tag as well, but there may be other tags we need to check for as well.

/**
 * Automatically embed advertisement into content.
 */
function ad_embed_auto($text, $ad, $count, $force = FALSE) {
  if (!$text) { return; }

  if ($count == 0) {
    $text = $ad . $text;
  }
  else if ($count == -1) {
    $text = $text . $ad;
  }

  $pos = $paragraph = $ptag = $char = 0;
  while ($pos !== FALSE) {
    $ptag = strpos($text, "</p>", $pos + 1);
    $char = strpos($text, "\n", $pos + 1);
    if (($char == FALSE && $ptag !== FALSE) || (($ptag !== FALSE) && ($char !== FALSE) && $ptag < $char)){
      $len = 4;
      $pos = $ptag;
    }
    else {
      $len = 0;
      $pos = $char;
    }

    if ($pos) {
      $paragraph++;
      if ($paragraph == $count) {
        $part1 = substr($text, 0, $pos + $len);
        $part2 = substr($text, $pos + $len + 1, strlen($text));
        $text = $part1 . $ad . $part2;
        break;
      }
    }
  }
  // Not enough paragraphs to display ad, unless forced.
  if (($paragraph < $count) && $force) {
    $text = $text . $ad;
  }

  return $text;
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

interestingaftermath’s picture

subscribe

LynnS’s picture

This would explain why on some pages Ad *insists* on putting the ad at the top of the page unless I specify bottom of the content. Gonna try the code.

Subscribe.

John Franklin’s picture

The embed should probably be turned into an input filter put at the end of the chain. The filter should drop an "insert ad here" div tag that gets replaced via JS with the actual ad. That way a search engine crawling doesn't index the ad.

I'll have to investigate how screen readers would react to such a tag. Will they run the JS or see an empty tag and skip over it?

jenlampton’s picture

Patch still applies cleanly to latest version.

jenlampton’s picture

I found a bug in the old patch, this version corrects it.