You are here

dynamic_background_views.module in Dynamic Background 7

This module implements the views extension for dynamic background.

File

modules/dynamic_background_views/dynamic_background_views.module
View source
<?php

/**
 * @file
 * This module implements the views extension for dynamic background.
 */

/**
 * Implementation of hook_perm().
 */

/**
 * Implementation of hook_perm().
 */
function dynamic_background_views_permission() {
  return array(
    'configure views dynamic background' => array(
      'title' => t('Configure views dynamic background'),
    ),
    'views selection of background' => array(
      'title' => t('Allow selection of backgrounds in views'),
    ),
  );
}

/**
 * Implementation of hook_menu(). Hooks into the profile with a "My background"
 * tab, where users can select one of the backgrounds.
 *
 * @return array menu items
 */
function dynamic_background_views_menu() {
  $items = array();
  $items['admin/config/user-interface/backgrounds/views'] = array(
    'title' => 'Views',
    'description' => t('Configure views dynamic background'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_background_views_admin_form',
    ),
    'access arguments' => array(
      'configure views dynamic background',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => -10,
  );
  return $items;
}

/**
 * Build the administration interface for dynamic background nodes and enables
 * administrators to select which content types have enable background selection.
 *
 * @return array $form
 */
function dynamic_background_views_admin_form() {
  $form = array(
    '#tree' => TRUE,
  );

  // Add image style to the form.
  $form += dynamic_background_image_style_form('dynamic_background_views_image_style');

  // Add css behaviour form to the form.
  $form += dynamic_background_css_behaviour_form('dynamic_background_views_css');
  return system_settings_form($form);
}

/**
 * Implementation of hook_views_api().
 */
function dynamic_background_views_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'dynamic_background_views') . '/views',
  );
}

/**
 * Helper function that loads a dynamic background image base on a view id. The
 * id is a combination of the view name underscore view display name.
 *
 * @param string $view_id
 * @return string image id
 */
function dynamic_background_views_get_image_id($view_id) {
  $query = db_select('dynamic_background_views', 'dbv');
  $data = $query
    ->fields('dbv', array(
    'data',
  ))
    ->condition('view_id', $view_id)
    ->execute()
    ->fetchField();
  if ($data === FALSE) {
    return NULL;
  }
  return $data;
}

/**
 * Implements hook_dynamic_background_css().
 */
function dynamic_background_views_dynamic_background_css($vars) {

  // If the current page is a view, try to use dynamic background.
  $image_id = NULL;
  if ($view = views_get_page_view()) {
    $view_id = $view->name . '_' . $view->current_display;
    $image_id = dynamic_background_views_get_image_id($view_id);
  }

  // Generate the css based in the image id.
  if (!is_null($image_id)) {
    $backgrounds = variable_get('dynamic_background_images', array());
    if (isset($backgrounds[$image_id])) {

      // Load image style settings.
      $image_style = variable_get('dynamic_background_views_image_style', FALSE);
      return array(
        'image' => $backgrounds[$image_id]['picture'],
        'configuration' => variable_get('dynamic_background_views_css', array()),
        'image_style' => $image_style ? $image_style['style'] : FALSE,
      );
    }
  }
}

/**
 * Implements hook_dynamic_background_weight for the main module.
 */
function dynamic_background_views_dynamic_background_weight() {
  return array(
    'weight' => -40,
  );
}

Functions

Namesort descending Description
dynamic_background_views_admin_form Build the administration interface for dynamic background nodes and enables administrators to select which content types have enable background selection.
dynamic_background_views_dynamic_background_css Implements hook_dynamic_background_css().
dynamic_background_views_dynamic_background_weight Implements hook_dynamic_background_weight for the main module.
dynamic_background_views_get_image_id Helper function that loads a dynamic background image base on a view id. The id is a combination of the view name underscore view display name.
dynamic_background_views_menu Implementation of hook_menu(). Hooks into the profile with a "My background" tab, where users can select one of the backgrounds.
dynamic_background_views_permission Implementation of hook_perm().
dynamic_background_views_views_api Implementation of hook_views_api().