0

I am attempting to define a CTools content pane based on the tutorial here. However, when I add my pane to the panel, no matter what region (left sidebar, header, footer) I add it to, it is printed at the top of the page without any of the region or pane CSS classes.

This is the function I am using to render the pane:

function MYMODULE_messagelimits_content_type_render($subtype, $conf, $args, $context) {
  if (!empty($conf)) {
    $block = new stdClass();
    $block->title = check_plain("");
    // Show message limits
    $block->content = MYMODULE_show_message_limits();
    return $block;
  }
}

MYMODULE_show_message_limits() is a function that prints out some information to show users what their messaging limits are, for example:

if ($sent_total > 1) {
  print '<div class="messagesng">';
  print t('You have already started conversations with 2 people this week.') . '</div>';    }
else if ($sent_total == 1) {
  print '<div class="messagesok">' . t('You can start a conversation with 1 more new person this week.') . '</div>';
}
else {
  print '<div class="messagesok">' . t('You can start conversations with 2 new people this week.') . '</div>';
}

If I make a change such as $block->content = t("my sample content"), then the pane is printed with the correct classes in the proper place on the page.

So how can I set the output of $block-content to be the output of my custom module's function while retaining the panels CSS classes? Please help me stop abusing the PHP filter within Panels!

  • @Letharion Actually, I don't know what's going on. Originally I just put this PHP function into a New custom content in the Panels UI and used the PHP filter to evaluate it. But now I'm trying to stop using the PHP filter by writing the whole thing in code, so I don't know what I need to change to get it to work. – Patrick Kenny Aug 24 '13 at 11:07
  • Answer posted, cleaned up my previous comments. :) Glad to hear of an effort to move away from PHP filters ;) – Letharion Aug 24 '13 at 11:17
2

In the following function, the content is provided by t(). t(), when called, returns the text "my sample text". Because that text gets assigned to $block->content, the pane is rendered with "my sample text" as the content.

function MYMODULE_messagelimits_content_type_render($subtype, $conf, $args, $context) {
  if (!empty($conf)) {
    $block = new stdClass();
    $block->title = check_plain("");
    // Show message limits.
    $block->content = t("my sample content")
    return $block;
  }
}

By contrast, this function:

function MYMODULE_messagelimits_content_type_render($subtype, $conf, $args, $context) {
  if (!empty($conf)) {
    $block = new stdClass();
    $block->title = check_plain("");
    // Show message limits
    $block->content = MYMODULE_show_message_limits();
    return $block;
  }
}

Assigns the return value of MYMODULE_show_message_limits() to $block->content, rendering the pane with that function's return value. To understand why that doesn't work as expected, we must ask, what does MYMODULE_show_message_limits() return?

if ($sent_total > 1) {
  print '<div class="messagesng">';
  print t('You have already started conversations with 2 people this week.') . '</div>';
}
else if ($sent_total == 1) {
  print '<div class="messagesok">' . t('You can start a conversation with 1 more new person this week.') . '</div>';
}
else {
  print '<div class="messagesok">' . t('You can start conversations with 2 new people this week.') . '</div>';
}

The function above, lacks a return statement, so in pratice, it returns NULL. Hence the pane output is empty, not even an empty string.

MYMODULE_show_message_limits() does, on the other hand, execute several print calls. Since that happens at a stage when content is still being assembled, remember, this is not a template, the prints are thrown right out onto the page, in the same way it would have if it was executed in any hook invocation for example.

The solution then, is to do:

$output = '';
if ($sent_total > 1) {
  $output .= '<div class="messagesng">';
  $output .= t('You have already started conversations with 2 people this week.') . '</div>';
}
else if ($sent_total == 1) {
  $output .= '<div class="messagesok">' . t('You can start a conversation with 1 more new person this week.') . '</div>';
}
else {
  $output .= '<div class="messagesok">' . t('You can start conversations with 2 new people this week.') . '</div>';
}
return $output;
  • Thanks, this is really helpful. It's these kinds of explanations that will help me catch my own errors in the future, since they allow me to understand what is actually going on. – Patrick Kenny Aug 24 '13 at 11:33

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.