fp_render_content

5.x render.inc fp_render_content($render_array = array())

This is very similar to fp_get_form / fp_render_form, except in this case we are being passed the completed "render_array", which already contains all of our elements. We will call hooks on it, sort by weights, and then return the rendered HTML.

6 calls to fp_render_content()
admin_display_courses in modules/admin/admin.courses.inc
This function displays all of our courses for us to edit.
admin_display_degrees in modules/admin/admin.degrees.inc
admin_display_groups in modules/admin/admin.groups.inc
This function will display a list of all our groups.
advise_display_history in modules/advise/advise.history.inc
Displays the history tab on screen.
course_search_display_courses in modules/course_search/course_search.module
Show the user their select of courses.

... See full list

File

includes/render.inc, line 13

Code

function fp_render_content($render_array = array()) {

  $rtn = "";

  if (!isset($render_array["#id"])) {
    // An #id wasn't set, which is required, so we're going to 
    // create one based on the function that called this function.
    $x = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
    $render_array['#id'] = trim(@$x[1]['class'] . "_" . @$x[1]['function']);
    // If nothing was discovered, just ditch it...
    if ($render_array['#id'] == "_") {
      unset($render_array["#id"]);
    }
  }

  // First, check to see if this render array has an "#id" field defined.  This is required.
  $render_id = fp_get_machine_readable(trim(@$render_array["#id"]));
  if ($render_id == "") {
    fp_add_message(t("The render array supplied does not have an #id field set.  This must be
                     machine-readable and unique.<br>Ex:  \$arr['#id'] = 'advise_course_description_popup';
                     <br>It may be easiest to simply name it after the function which is creating the render array."), "error");
    return "";
  }

  // Any hooks to alter this render array?  
  $modules = modules_implement_hook("content_alter");
  foreach ($modules as $module) {
    call_user_func_array($module . '_content_alter', array(&$render_array, $render_id));
  }

  // Okay, now the fun part.  Re-order the elements by weight.  Lighter weights
  // should float to the top.  Elements w/o a weight listed are assumed to have a weight of 0.
  // Unfortunately we cannot use uasort, as it re-orders our indexes when weights are identical.

  // The first the I want to do is find out, what are the defined weights in this form, if any.
  $defined_weights = array();
  foreach ($render_array as $element) {

    if (!is_array($element)) {
      continue;
    }

    if (!isset($element["weight"])) {
      $element["weight"] = 0;
    }

    $weight = (int) $element["weight"];
    if (!in_array($weight, $defined_weights)) {
      $defined_weights[] = $weight;
    }
  }

  // Okay, now sort our weights.
  sort($defined_weights);


  // Before we get to assigning weights, we need to make sure
  // that none of our form elements have a name which might cause us trouble.
  // Namely, no element can be named "submit" (like a button) because it will
  // interfere with our javascript functions.
  $form2 = array();
  foreach ($render_array as $key => $element) {
    $name = $key;
    if ($name == "submit") {
      $name = "btn_submit";
    }

    $form2[$name] = $element;
  }

  $form = $form2;


  // Okay, now go through the weights and create a new form in THAT order.
  $new_form = array();
  foreach ($defined_weights as $dw) {
    foreach ($form as $key => $element) {

      if (!is_array($element)) {
        $new_form[$key] = $element;
        continue;
      }

      if (!isset($element["weight"])) {
        $element["weight"] = 0;
      }
      $weight = (int) $element["weight"];
      if ($weight == $dw) {
        $new_form[$key] = $element;
      }
    }
  }

  // Okay, we should now be good to go!  
  $render_array = $new_form;

  // We can now proceed with rendering this render_array.  It will be similar to fp_render_form.
  $rtn .= "<div class='renderapi-content' id='render-$render_id'>";
  $rtn .= fp_render_array($render_array);
  $rtn .= "</div>";

  return $rtn;

}