View source
<?php
function backgroundfield_init() {
module_load_include('inc', 'filefield', 'filefield_widget');
module_load_include('inc', 'imagefield', 'imagefield_field');
module_load_include('inc', 'imagefield', 'imagefield_widget');
module_load_include('inc', 'backgroundfield', 'backgroundfield_field');
module_load_include('inc', 'backgroundfield', 'backgroundfield_widget');
}
function backgroundfield_field_info() {
return array(
'backgroundfield' => array(
'label' => t('Background Field'),
'description' => t('Allows users to apply a background image to a field defined css selector'),
),
);
}
function backgroundfield_field_settings($op, $field) {
switch ($op) {
case 'form':
return backgroundfield_field_settings_form($field);
case 'save':
return backgroundfield_field_settings_save($field);
default:
return filefield_field_settings($op, $field);
}
}
function backgroundfield_field($op, $node, $field, &$items, $teaser, $page) {
return filefield_field($op, $node, $field, $items, $teaser, $page);
}
function backgroundfield_content_is_empty($item, $field) {
return filefield_content_is_empty($item, $field);
}
function backgroundfield_default_value(&$form, &$form_state, $field, $delta) {
return filefield_default_value($form, $form_state, $field, $delta);
}
function backgroundfield_widget_info() {
return array(
'theme_background_widget' => array(
'label' => t('Background Field'),
'field types' => array(
'backgroundfield',
),
'multiple values' => CONTENT_HANDLE_CORE,
'callbacks' => array(
'default value' => CONTENT_CALLBACK_DEFAULT,
),
'description' => t('Allows users to apply a background image to a field defined css selector'),
),
);
}
function backgroundfield_widget_settings($op, $widget) {
switch ($op) {
case 'form':
return backgroundfield_widget_settings_form($widget);
case 'validate':
return imagefield_widget_settings_validate($widget);
case 'save':
return backgroundfield_widget_settings_save($widget);
}
}
function backgroundfield_elements() {
return array(
'backgroundfield_widget' => array(
'#input' => TRUE,
'#process' => array(
'imagefield_widget_process',
),
'#element_validate' => array(
'imagefield_widget_validate',
),
),
);
}
function backgroundfield_widget(&$form, &$form_state, $field, $items, $delta = 0) {
if (!is_array($items[$delta]['data'])) {
$items[$delta]['data'] = unserialize($items[$delta]['data']);
}
$element = filefield_widget($form, $form_state, $field, $items, $delta);
$element['#type'] = 'imagefield_widget';
return $element;
}
function backgroundfield_file_references($file) {
return array(
'backgroundfield' => 1,
);
}
function backgroundfield_theme() {
return array(
'backgroundfield_formatter_default' => array(
'arguments' => array(
'element' => NULL,
),
),
);
}
function backgroundfield_field_formatter_info() {
return array(
'default' => array(
'label' => t('Background Field'),
'field types' => array(
'backgroundfield',
),
'multiple values' => CONTENT_HANDLE_CORE,
),
);
}
function theme_backgroundfield_formatter_default($element = NULL) {
global $base_url;
static $previous_selectors = array();
$fields = content_fields();
$cssInfo = $fields[$element['#field_name']];
if (in_array($cssInfo['css_selector'], $previous_selectors)) {
return;
}
if (!empty($element['#item']['filepath'])) {
$css = $cssInfo['css_selector'] . ' {
background-image: url("' . $base_url . '/' . $element['#item']['filepath'] . '");
background-repeat: ' . $cssInfo['repeat'] . ';
background-position: ' . $cssInfo['h_position'] . ' ' . $cssInfo['v_position'] . ';
background-attachment: ' . $cssInfo['attachment'] . ';
}';
drupal_set_html_head('<style type="text/css" rel="stylesheet" media="all">' . $css . '</style>');
$previous_selectors[] = $cssInfo['css_selector'];
}
}