I'm looking to output panes within panels as JSON as well, instead of HTML strings. Is there any general way to recursively output panes as JSON? Or can this only be done in an ad-hoc way (like with a overridden render_pane_content() function with custom code to handle each type of pane)?

Comments

colinmccabe created an issue. See original summary.

hussainweb’s picture

I'm sorry for the late response here.

If you have built the pane, then there is a much easier way to do this. Otherwise, there is no straight-forward way to do this.

For plugins/panes you control, you can check for a 'restful_panels' context in the render callback. For example, this is a 'render callback' for my ctools content type plugin:


/**
 * Callback to render a custom pane.
 */
function example_pane_render($subtype, $conf, $args, $contexts) {
  if (!empty($contexts['restful_panels'])) {
    return example_pane_json_render($subtype, $conf, $args, $contexts);
  }
  else {
    // Just pass it on the admin info which is all we need to show if the node
    // is viewed directly.
    return example_pane_admin_info($subtype, $conf, $contexts);
  }
}

The example_pane_json_render just returns a stdClass object. This structure is sent back as is from the RESTful endpoint. The example_pane_admin_info generates HTML and is not really relevant in a RESTful call. It is only called if the panel is "viewed" normally.

For other types of panels, there are two ways I can think of:

  1. Handle this in your RESTful controller itself - parsing/massaging the data depending on your needs.
  2. Handle this in the structured panel renderer. This is much more work and I wouldn't recommend it, unless you can't reuse the parsing/massaging logic in your controllers easily.

Do you have any other ideas?

colinmccabe’s picture

Thanks for the advice! I agree, the general approach seems to be to write custom CTools content types. I was hoping for a silver bullet, but it doesn't look like there is one. Drupal wants to render everything itself, and to go against that requires custom code.

colinmccabe’s picture

Status: Active » Closed (works as designed)