function backgroundfield_field_formatter_view in BackgroundField 7
Immplementation of hook_field_formatter_view()
File
- ./
backgroundfield.module, line 262
Code
function backgroundfield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
static $previous_selectors = array();
if (in_array($instance['settings']['css_selector'], $previous_selectors)) {
return;
}
// if this field instance has more than one image, randomly select one
if (count($items) > 1) {
$num = mt_rand(0, count($items) - 1);
$file = file_load($items[$num]['fid']);
}
else {
if (isset($items[0])) {
$file = file_load($items[0]['fid']);
}
else {
if (is_numeric($field['settings']['default_image'])) {
$file = file_load($field['settings']['default_image']);
}
else {
return;
}
}
}
// check if there's a file
if (!$file) {
return;
}
$important = isset($instance['settings']['css_important']) && $instance['settings']['css_important'] ? ' !important' : '';
if (!isset($instance['settings']['css_size']) || empty($instance['settings']['css_size'])) {
$instance['settings']['css_size'] = 'auto';
}
if (isset($instance['settings']['image_style']) && !empty($instance['settings']['image_style']) && $instance['settings']['image_style'] != 'no_style') {
$file = file_create_url(image_style_path($instance['settings']['image_style'], $file->uri));
}
else {
$file = file_create_url($file->uri);
}
// $instance['settings'] can be modified by calling hook_backgroundfield_alter(&$settings, $entity)
drupal_alter('backgroundfield', $instance['settings'], $entity);
// add token support for the css selector.
$data = array(
$entity_type => $entity,
);
$instance['settings']['css_selector'] = token_replace($instance['settings']['css_selector'], $data);
$css = $instance['settings']['css_selector'] . ' {
background-image: url("' . $file . '")' . $important . ';
background-repeat: ' . $instance['settings']['css_repeat'] . $important . ';
background-position: ' . $instance['settings']['css_h_position'] . ' ' . $instance['settings']['css_v_position'] . $important . ';
background-attachment: ' . $instance['settings']['css_attachment'] . $important . ';
background-size: ' . $instance['settings']['css_size'] . $important . ';
}';
$options = array(
'type' => 'inline',
'group' => CSS_DEFAULT,
);
drupal_add_css($css, $options);
}