1

I'm a newbie in Drupal but I'm working on a project with drupal 7 and I'm going out of my mind.

I have a block which content must be wraped by different links if is seen on desktop or on mobile devices. I decided to work with a custom block template and php mobile detection, and seems to work fine except that the same code is replicated 2-3 times on the rendered site. I don't understand why this is happening, maybe you can help me...

Here is the template file for the block (block id 27)

<?php 

<div id="<?php print $block_html_id; ?>" class="<?php print $classes; ?>"<?php print    $attributes; ?>>

<?php print render($title_prefix); ?>
<?php if ($title): ?>
   <h2<?php print $title_attributes; ?>><?php print $title; ?></h2>
<?php endif; ?>
<?php print render($title_suffix); ?>


<?php
  $detect = mobile_detect_get_object();
  $linkClass;

  if (! $detect->isMobile() && ! $detect->isTablet()) {
      $linkClass = "fancybox";
  } else {
      $linkClass = "infografica_mobile";
  }

?>
 <p><a class="<?php echo $linkClass; ?>" href="#" target="_blank" rel="gallery"><?php echo $content; ?></a></p>

</div>

Here what i see with dev tools: Duplication of code when template is rendered

UPDATE: I've tried to modify the rendering part, removing the p tag this way

echo "<a class='$linkClass' href='#' target='_blank' rel='gallery'>";
print_r($content);
echo "</a>";

and it seems to work fine and smooth...any clue?

No more code duplication.... Thank you

  • Have you tried clearing the block cache? Not sure if relevant, but just as a starting point – autopoietic Dec 13 '14 at 11:48
  • 1
    The block isn't repeated, and the 'repeated' code does not in fact look the same in each iteration, first anchor tag is empty, second has image, third is not within paragraph. It looks like this is just the block content. Where is block content coming from? – autopoietic Dec 13 '14 at 11:51
  • I agree with @autopoietic You need to examine $content because aside form the duplication, there also seem to be some html error. I suspect it is because $content already contains an anchor tag, and you may not wrap an anchor within an anchor. You don't see it because the browser compensated for it, but I think if you view page source you might see it, or if you were to examine $content you will definately see it. And though not a big issue, I would move the mobile detect code to a preprocess function and keep the template lean. – J. Reynolds Dec 14 '14 at 4:19
  • The content is just a paragraph with an image, no anchor tags inside it. What is really strange is that if in the code above I remove the p tags from the last line (the one that renders the content) no more duplication happen and everything works fine. Any clue? – Kerruba Dec 14 '14 at 20:32

Your Answer

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

Browse other questions tagged or ask your own question.