You are here

background_image_formatter.module in Simple Background image formatter 7

Same filename and directory in other branches
  1. 8 background_image_formatter.module

File

background_image_formatter.module
View source
<?php

/**
 * @file
 * Background image formatter module.
 */
require_once 'background_image_formatter.helpers.inc';

/**
 * Implements hook_field_formatter_info().
 */
function background_image_formatter_field_formatter_info() {
  return array(
    'background_image_formatter' => array(
      'label' => t('Background Image'),
      'field types' => array(
        'image',
      ),
      'settings' => array(
        'image_style' => '',
        'background_image_selector' => 'background-image-formatter',
        'background_image_output_type' => 'inline',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function background_image_formatter_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $image_styles = image_style_options();
  $element['image_style'] = array(
    '#title' => t('Image style'),
    '#type' => 'select',
    '#options' => $image_styles,
    '#default_value' => $settings['image_style'],
    '#empty_option' => t('None (original image)'),
    '#description' => t('Select the image style to use.'),
  );
  $element['background_image_output_type'] = array(
    '#title' => t('Output To'),
    '#type' => 'select',
    '#options' => array(
      'inline' => t('Write background-image to inline style attribute'),
      'css' => t('Write background-image to CSS selector'),
    ),
    '#default_value' => $settings['background_image_output_type'],
    '#required' => TRUE,
    '#description' => t('Define how background-image will be printed to the dom.'),
  );
  $element['background_image_selector'] = array(
    '#title' => t('CSS Selector'),
    '#type' => 'textfield',
    '#default_value' => $settings['background_image_selector'],
    '#required' => FALSE,
    '#description' => t('CSS selector that image(s) are attached to.'),
  );
  if (module_exists('token') && !empty($field)) {
    background_image_formatter_token_settings_form($field, $element);
  }
  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function background_image_formatter_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = array();
  $image_styles = image_style_options(FALSE);

  // Unset possible 'No defined styles' option.
  unset($image_styles['']);
  $select_style = $settings['image_style'];
  if (isset($image_styles[$select_style])) {
    $summary[] = t('URL for image style: @style', array(
      '@style' => $image_styles[$select_style],
    ));
  }
  else {
    $summary[] = t('Original image URL');
  }
  $summary[] = t('The CSS selector <code>@background_image_selector</code> will be created with the image set to the background-image property.', array(
    '@background_image_selector' => $settings['background_image_selector'],
  ));
  return implode('<br />', $summary);
}

/**
 * Implements hook_field_formatter_view().
 */
function background_image_formatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  if (!is_background_image_formatter_display_type($display)) {
    return $element;
  }
  foreach ($items as $delta => $item) {
    $image_url = file_create_url($item['uri']);
    if (!empty($settings['image_style'])) {
      $image_url = image_style_url($settings['image_style'], $item['uri']);
    }
    $selector = $settings['background_image_selector'];
    if (module_exists('token')) {
      $tokens_mapping = array_flip(token_get_entity_mapping());
      $selector = token_replace($selector, array(
        $tokens_mapping[$entity_type] => $entity,
      ));
    }
    $theme_data = array(
      'background_image_selector' => strip_tags($selector),
      'image_url' => $image_url,
    );
    switch ($settings['background_image_output_type']) {
      case 'css':
        theme('background_image_formatter_css', $theme_data);
        break;
      case 'inline':
        $element[$delta]['#markup'] = theme('background_image_formatter_inline', $theme_data);
        break;
    }
  }
  return $element;
}

/**
 * Implements hook_theme().
 */
function background_image_formatter_theme() {
  return array(
    'background_image_formatter_css' => array(
      'variables' => array(
        'background_image_selector' => NULL,
        'image' => array(),
      ),
    ),
    'background_image_formatter_inline' => array(
      'template' => 'templates/background-image-formatter-inline',
      'arguments' => array(
        'background_image_selector' => NULL,
        'image_url' => NULL,
      ),
    ),
  );
}