You are here

bootstrap_carousel_if.module in Bootstrap Carousel Image Formatter 7.3

Same filename and directory in other branches
  1. 8.3 bootstrap_carousel_if.module

Bootstrap Carousel Image Field.

File

bootstrap_carousel_if.module
View source
<?php

/**
 * @file
 * Bootstrap Carousel Image Field.
 */

/**
 * Implements hook_field_formatter_info().
 */
function bootstrap_carousel_if_field_formatter_info() {
  $info['bootstrap_carousel_if'] = array(
    'label' => t('Bootstrap Carousel'),
    'field types' => array(
      'image',
    ),
    'module' => 'bootstrap_carousel_if',
    'settings' => array(
      'interval' => '5000',
      'pause' => 0,
      'wrap' => 0,
      'keyboard' => 0,
      'indicators' => 1,
      'image_style' => 0,
      'background' => 0,
      'background_pos' => 'center center',
      'width' => '100%',
      'height' => '100px',
    ),
  );
  return $info;
}

/**
 * Implements hook_theme().
 */
function bootstrap_carousel_if_theme() {
  return array(
    'bootstrap_carousel_if' => array(
      'variables' => array(
        'interval' => '5000',
        'pause' => 0,
        'wrap' => 0,
        'keyboard' => 0,
        'indicators' => 1,
        'image_style' => 0,
        'background' => 0,
        'background_pos' => 'center center',
        'width' => '100%',
        'height' => '100px',
        'id' => '',
        'items' => array(),
      ),
    ),
  );
}

/**
 * Theme function.
 */
function theme_bootstrap_carousel_if($vars) {
  $files = array();
  foreach ($vars['items'] as $file) {
    if ($vars['background']) {
      $files[] = '<div style="width: ' . check_plain($vars['width']) . '; height: ' . $vars['height'] . '; background-position: ' . $vars['background_pos'] . '; background-size: cover; background-image: url(' . ($vars['image_style'] ? image_style_url($vars['image_style'], $file['uri']) : file_create_url($file['uri'])) . ');"></div>';
    }
    else {
      $type = $vars['image_style'] ? 'image_style' : 'image';
      $file['style_name'] = $vars['image_style'] ? $vars['image_style'] : '';
      $file['path'] = $file['uri'];
      $files[] = theme($type, $file);
    }
  }
  $filecount = count($files);
  $output = '
      <div id="' . $vars['id'] . '" class="carousel slide" data-ride="carousel" data-interval="' . $vars['interval'] . '" data-pause="' . $vars['pause'] . '" data-wrap="' . $vars['wrap'] . '" data-keyboard="' . $vars['keyboard'] . '">';
  if ($vars['indicators'] && $filecount > 1) {
    $output .= '<!-- Indicators -->
        <ol class="carousel-indicators">';
    for ($i = 0; $i < $filecount; $i++) {
      if (!$i) {
        $output .= '<li data-target="#' . $vars['id'] . '" data-slide-to="0" class="active"></li>';
      }
      else {
        $output .= '<li data-target="#' . $vars['id'] . '" data-slide-to="' . $i . '"></li>';
      }
    }
    $output .= '</ol>';
  }
  $output .= '<div class="carousel-inner" role="listbox">';

  // Files/slides
  for ($i = 0; $i < $filecount; $i++) {
    $output .= !$i ? '<div class="item active">' : '<div class="item">';
    $output .= $files[$i];
    $output .= '</div>';
  }
  $output .= '</div>';
  if ($filecount > 1) {
    $output .= '<!-- Controls -->
          <a class="left carousel-control" href="#' . $vars['id'] . '" role="button" data-slide="prev">
            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
          </a>
          <a class="right carousel-control" href="#' . $vars['id'] . '" role="button" data-slide="next">
            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
          </a>
    ';
  }
  $output .= '</div>';
  return $output;
}

/**
 * Implements hook_field_formatter_view().
 */
function bootstrap_carousel_if_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $ids = entity_extract_ids($entity_type, $entity);
  $element[0] = array(
    '#markup' => theme('bootstrap_carousel_if', array(
      'interval' => $display['settings']['interval'],
      'pause' => $display['settings']['pause'],
      'wrap' => $display['settings']['wrap'],
      'keyboard' => $display['settings']['keyboard'],
      'indicators' => $display['settings']['indicators'],
      'image_style' => $display['settings']['image_style'],
      'background' => $display['settings']['background'],
      'background_pos' => $display['settings']['background_pos'],
      'width' => $display['settings']['width'],
      'height' => $display['settings']['height'],
      'id' => drupal_clean_css_identifier(implode('-', $ids) . '-' . $instance['field_name']),
      'items' => $items,
    )),
  );
  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function bootstrap_carousel_if_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $summary = '';
  if ($display['type'] == 'bootstrap_carousel_if') {
    $summary = t('Bootstrap carousel');
  }
  return $summary;
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function bootstrap_carousel_if_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $settings = $instance['display'][$view_mode]['settings'];
  $options = array();
  $element['interval'] = array(
    '#title' => t('Interval'),
    '#type' => 'textfield',
    '#default_value' => $settings['interval'],
    '#required' => TRUE,
  );
  $element['pause'] = array(
    '#title' => t('Pause on hover'),
    '#type' => 'select',
    '#options' => array(
      0 => 'no',
      'hover' => 'yes',
    ),
    '#default_value' => $settings['pause'],
  );
  $element['wrap'] = array(
    '#title' => t('Wrap'),
    '#type' => 'select',
    '#options' => array(
      0 => 'no',
      1 => 'yes',
    ),
    '#default_value' => $settings['wrap'],
  );
  $element['keyboard'] = array(
    '#title' => t('Keyboard'),
    '#type' => 'select',
    '#options' => array(
      0 => 'no',
      1 => 'yes',
    ),
    '#default_value' => $settings['keyboard'],
  );
  $element['indicators'] = array(
    '#title' => t('Indicators'),
    '#type' => 'select',
    '#options' => array(
      0 => 'no',
      1 => 'yes',
    ),
    '#default_value' => $settings['indicators'],
  );

  // Get image styles
  $styles = image_styles();
  $options = array(
    0 => 'Original image',
  );
  foreach ($styles as $key => $value) {
    $options[$key] = $value['label'];
  }
  $element['image_style'] = array(
    '#title' => t('Image style'),
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => $settings['image_style'],
  );
  $element['background'] = array(
    '#title' => t('Force all image to appear same size (css background images)'),
    '#type' => 'select',
    '#prefix' => '<div id="bootstrap-carousel-bg-setting">',
    '#suffix' => '</div>',
    '#options' => array(
      0 => 'no',
      1 => 'yes',
    ),
    '#default_value' => $settings['background'],
    '#description' => '<div style="white-space: pre-line">' . t('Setting this to yes makes the images a background image of a div, rather than an image tag. Background position "cover" is set so the image always fills the space. This may cause some parts of the image to not be visible and it will also upscale your images if it is not large enough. You can set the height and width of the div below, or leave them blank and do it in your own CSS. Either way, atleast the height needs to be set on the div so the image is visible.') . '</div>',
  );
  $element['background_pos'] = array(
    '#title' => t('Background position'),
    '#type' => 'select',
    '#options' => array(
      'left top' => 'left top',
      'left center' => 'left top',
      'left bottom' => 'left bottom',
      'right top' => 'right top',
      'right center' => 'right center',
      'right bottom' => 'right bottom',
      'center top' => 'center top',
      'center center' => 'center center',
      'center bottom' => 'center bottom',
    ),
    '#default_value' => $settings['background_pos'],
    '#states' => array(
      'invisible' => array(
        '#bootstrap-carousel-bg-setting select' => array(
          'value' => 0,
        ),
      ),
    ),
  );
  $element['width'] = array(
    '#title' => t('Width'),
    '#type' => 'textfield',
    '#default_value' => $settings['width'],
    '#description' => '<div style="white-space: pre-line">' . t('Only takes effect when background setting is on. expected values are something like "100%" or "100px". Set this value to an empty string to use values from your own CSS.') . '</div>',
    '#states' => array(
      'invisible' => array(
        '#bootstrap-carousel-bg-setting select' => array(
          'value' => 0,
        ),
      ),
    ),
  );
  $element['height'] = array(
    '#title' => t('Height'),
    '#type' => 'textfield',
    '#default_value' => $settings['height'],
    '#description' => '<div style="white-space: pre-line">' . t('Only takes effect when background setting is on. expected values are something like "100%" or "100px". Set this value to an empty string to use values from your own CSS.') . '</div>',
    '#states' => array(
      'invisible' => array(
        '#bootstrap-carousel-bg-setting select' => array(
          'value' => 0,
        ),
      ),
    ),
  );
  return $element;
}