You are here

function bg_image_add_background_image_from_node in Background Images 6

Same name and namespace in other branches
  1. 7 bg_image.module \bg_image_add_background_image_from_node()

Adds a background image to the page using an image from a node. The node must have an image (or media) field and the field must be configured on the bg_image configuration page for this to work.

Parameters

$nid: The nid of the node that has the image to use for the background. Use 0 to generate a random image from the configured node type.

$css_settings: An array of css settings to use. See bg_image_add_background_image() for more detailed description of possible values.

$imagecache_preset: Optionally add an imagecache preset to the image before applying it to the background

1 call to bg_image_add_background_image_from_node()
bg_image_context_reaction_bg_image::execute in plugins/bg_image_context_reaction_bg_image.inc
Organizes the background image data into an array keyed by the selector, then applies the background image with the highest weight for each selector

File

./bg_image.module, line 487
Allows for customizable background images per page

Code

function bg_image_add_background_image_from_node($nid, $css_settings = array(), $imagecache_preset = NULL) {

  // We need to generate a random nid if 0 is passed in
  if ($nid == 0) {
    $type = variable_get('bg_image_node_type', '');
    if ($type) {
      $sql = "SELECT nid FROM {node}\n              WHERE type = '%s'\n              AND status = 1\n              ORDER BY RAND()\n              LIMIT 1";
      $query = db_query($sql, array(
        $type,
      ));
      $nid = db_result($query);
    }
    else {
      return FALSE;
    }
  }
  if (is_numeric($nid)) {

    // Load the node so we can check it's status
    $node = node_load($nid);

    // Check node access permissions
    if (node_access('view', $node)) {

      // Get the path of the image on the node
      $image_path = bg_image_get_image_path_from_node($nid);

      // Add the background image
      return bg_image_add_background_image($image_path, $css_settings, $imagecache_preset, $nid);
    }
  }

  // If all else fails
  return FALSE;
}