You are here

function bg_image_add_background_image_from_node in Background Images 7

Same name and namespace in other branches
  1. 6 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.

$image_style: Optionally add an image style to the image before applying it to the background

2 calls to bg_image_add_background_image_from_node()
bg_image_context_reaction_bg_image::execute in bg_image_context/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
bg_image_ui_init in bg_image_ui/bg_image_ui.module
Implements hook_init()

File

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

Code

function bg_image_add_background_image_from_node($nid, $css_settings = array(), $image_style = 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) {

      // Generate a random nid of the configured node type
      $query = db_select('node');

      // Add the node_access tag if user doesn't have permission to view all
      // background images
      if (!user_access('view all background images')) {
        $query
          ->addTag('node_access');
      }
      $nid = $query
        ->fields('node', array(
        'nid',
      ))
        ->condition('node.type', $type)
        ->orderRandom()
        ->execute()
        ->fetchField();
    }
    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 (user_access('view all background images') || node_access('view', $node)) {

      // Get the path of the image on the node
      $image_uri = bg_image_get_image_path_from_node($nid, FALSE);

      // Add the background image
      return bg_image_add_background_image($image_uri, $css_settings);
    }
  }

  // If all else fails
  return FALSE;
}