You are here

bootstrap_carousel.module in bootstrap_carousel 7

Bootstrap carousel module hooks.

File

bootstrap_carousel.module
View source
<?php

/**
 * @file
 * Bootstrap carousel module hooks.
 */

/**
 * Implements hook_libraries_info().
 */
function bootstrap_carousel_libraries_info() {
  $libraries['bootstrap'] = array(
    'name' => 'Bootstrap',
    'vendor url' => 'http://twitter.github.io/bootstrap',
    'download url' => 'http://twitter.github.io/bootstrap/customize.html',
    'version arguments' => array(
      'file' => 'js/bootstrap.js',
      'pattern' => '@v([0-9\\.a-z]+)@',
      'lines' => 2,
    ),
    'files' => array(
      'js' => array(
        'js/bootstrap.js',
      ),
      'css' => array(
        'css/bootstrap.css',
      ),
    ),
  );
  return $libraries;
}

/**
 * Implements hook_node_info().
 */
function bootstrap_carousel_node_info() {
  return array(
    'bootstrap_carousel' => array(
      'name' => t('Carousel'),
      'base' => 'node_content',
      'has_title' => TRUE,
      'title_label' => t('Carousel Title'),
      'description' => t('Create a new Carousel using Bootstrap Javascript.'),
    ),
  );
}

/**
 * Implements hook_field_info().
 */
function bootstrap_carousel_field_info() {
  return array(
    'bootstrap_carousel' => array(
      'label' => t('Bootstrap carousel'),
      'description' => t('The slides of the carousel'),
      'default_widget' => 'bootstrap_carousel_widget',
      'default_formatter' => 'bootstrap_carousel_formatter',
      'property_type' => 'bootstrap_carousel',
      'property_callbacks' => array(
        'bootstrap_carousel_property_info_callback',
      ),
      'no_ui' => TRUE,
    ),
  );
}

/**
 * Implements hook_property_info_callback().
 */
function bootstrap_carousel_property_info_callback(&$info, $entity_type, $field, $instance, $field_type) {
  $property =& $info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  $property['setter callback'] = 'entity_metadata_field_verbatim_set';
}

/**
 * Implements hook_field_widget_info().
 */
function bootstrap_carousel_field_widget_info() {
  return array(
    'bootstrap_carousel_widget' => array(
      'label' => t('Default'),
      'field types' => array(
        'bootstrap_carousel',
      ),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
        'default value' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 */
function bootstrap_carousel_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $required = $element['#required'];
  $item =& $items[$delta];
  $settings = $instance['widget']['settings'];
  $element['carousel_image'] = array(
    '#title' => t('Image File'),
    '#description' => t('Allowed extensions: gif png jpg jpeg'),
    '#type' => 'managed_file',
    '#required' => $required,
    '#default_value' => isset($item['carousel_image']) ? $item['carousel_image'] : '',
    '#upload_location' => 'public://carousel_images',
    '#upload_validators' => array(
      'file_validate_extensions' => array(
        'gif png jpg jpeg',
      ),
    ),
  );
  $element['image_alt_text'] = array(
    '#title' => t('Image Description'),
    '#description' => t('This field will be used as ALT attribute.'),
    '#type' => 'textfield',
    '#required' => $required,
    '#default_value' => isset($item['image_alt_text']) ? $item['image_alt_text'] : '',
  );
  $element['carousel_video'] = array(
    '#title' => t('Youtube Video Code'),
    '#description' => t('Youtube URL. (e.g "szXbSlV_n40" in case of Youtube URL => http://www.youtube.com/watch?v=szXbSlV_n40)'),
    '#type' => 'textfield',
    '#required' => $required,
    '#default_value' => isset($item['carousel_video']) ? $item['carousel_video'] : '',
  );
  $element['carousel_caption'] = array(
    '#type' => 'text_format',
    '#base_type' => 'textarea',
    '#title' => t('Slide Caption'),
    '#rows' => isset($settings['textarea_rows']) ? $settings['textarea_rows'] : 3,
    '#format' => isset($item['format']) ? $item['format'] : filter_default_format(),
    '#required' => $required,
    '#default_value' => isset($item['carousel_caption']) ? $item['carousel_caption'] : NULL,
  );
  return $element;
}

/**
 * Implements hook_field_presave().
 */
function bootstrap_carousel_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  foreach ($items as $delta => $value) {
    _bootstrap_carousel_process($items[$delta], $delta, $field, $entity);
  }
}

/**
 * Prepares the item description and format for storage.
 */
function _bootstrap_carousel_process(&$item, $delta, $field, $entity) {
  $item['format'] = $item['carousel_caption']['format'];
  $item['carousel_caption'] = $item['carousel_caption']['value'];
  if (!empty($item['carousel_image'])) {

    // Load the file via file.fid.
    $file = file_load($item['carousel_image']);

    // Change status to permanent.
    $file->status = FILE_STATUS_PERMANENT;

    // Save.
    file_save($file);

    // Record that the module (in this example, user module) is using the file.
    file_usage_add($file, 'bootstrap_carousel', 'bootstrap_carousel_image', $item['carousel_image']);
  }
}

/**
 * Implements hook_field_is_empty().
 */
function bootstrap_carousel_field_is_empty($item, $field) {

  // Image or Video are required for each valid slide item.
  if (empty($item['carousel_image']) && empty($item['carousel_video'])) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implements hook_field_validate().
 */
function bootstrap_carousel_field_validate($obj_type, $object, $field, $instance, $langcode, &$items, &$errors) {
  foreach ($items as $delta => $item) {
    if ((!empty($item['image_alt_text']) || !empty($item['carousel_caption']['value'])) && (empty($item['carousel_image']) && empty($item['carousel_video']))) {
      $errors[$field['field_name']][$langcode][$delta][] = array(
        'error' => 'image_invalid',
        'message' => t('A slide should has at least a image or a youtube video. To remove a slide, all of its fields must be empty.'),
      );
    }
  }
}

/**
 * Implements hook_field_formatter_info().
 */
function bootstrap_carousel_field_formatter_info() {
  return array(
    'bootstrap_carousel_default' => array(
      'label' => t('Default'),
      'field types' => array(
        'bootstrap_carousel',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function bootstrap_carousel_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {
    case 'bootstrap_carousel_default':
      foreach ($items as $delta => $item) {
        $element[$delta]['#markup'] = theme('bootstrap_carousel_field_formatter_default', $item);
      }
      break;
  }
  return $element;
}

/**
 * Implements hook_theme().
 */
function bootstrap_carousel_theme() {
  return array(
    'bootstrap_carousel_field_formatter_default' => array(
      'variables' => array(
        'item' => NULL,
      ),
    ),
  );
}

/**
 * Renders the bootstrap_carousel formatter.
 */
function theme_bootstrap_carousel_field_formatter_default($item) {
  return "{$item['carousel_text']}: {$item['carousel_image']}";
}

/**
 * Implements hook_theme_registry_alter().
 */
function bootstrap_carousel_theme_registry_alter(&$theme_registry) {
  $mod_path = drupal_get_path('module', 'bootstrap_carousel') . '/theme';
  $theme_registry_copy = $theme_registry;
  _theme_process_registry($theme_registry_copy, 'phptemplate', 'theme_engine', NULL, $mod_path);
  $theme_registry += array_diff_key($theme_registry_copy, $theme_registry);
  _bootstrap_carousel_insert_after_first_element($theme_registry['node']['theme paths'], $mod_path);
}

/**
 * Helper function for re-ordering arrays (needed by theme_registry_alter).
 */
function _bootstrap_carousel_insert_after_first_element(&$a, $element) {
  if (is_array($a)) {
    $first_element = array_shift($a);
    array_unshift($a, $first_element, $element);
  }
}

/**
 * Implements hook_node_view().
 */
function bootstrap_carousel_node_view($node, $view_mode, $langcode) {
  if ($node->type === 'bootstrap_carousel') {
    _bootstrap_carousel_load_javascript($node);
    _bootstrap_carousel_load_css();
  }
}

/**
 * Implements hook_ctools_render_alter().
 */
function bootstrap_carousel_ctools_render_alter(&$info, &$page, &$context) {
  if (!$page) {
    return FALSE;
  }
  if (!isset($context['handler']->conf['display']->content)) {
    return FALSE;
  }
  foreach ($context['handler']->conf['display']->content as $id => $panel_content) {
    if ($panel_content->type == 'node') {
      $node = node_load($panel_content->configuration['nid']);
      if ($node->type == 'bootstrap_carousel') {
        _bootstrap_carousel_load_javascript($node);
        _bootstrap_carousel_load_css();
      }
    }
  }
}

/**
 * Add bootstrap javascript and library.
 */
function _bootstrap_carousel_load_javascript($node) {
  $carousel_settings = array(
    'interval' => FALSE,
    'pause' => '',
  );
  $node = entity_metadata_wrapper('node', $node);
  $interval = $node->field_carousel_interval
    ->value();
  if (!empty($interval)) {
    $carousel_settings['interval'] = $interval;
  }
  $control_options = $node->field_control_options
    ->value();
  if (in_array(3, $control_options)) {
    $carousel_settings['pause'] = 'hover';
  }
  foreach ($node->field_slides
    ->value() as $slide) {
    if (isset($slide['carousel_video'])) {
      drupal_add_js(variable_get('bootstrap_carousel_youtube_player_api', 'https://www.youtube.com/player_api'), 'external');
    }
  }
  libraries_load('bootstrap');
  drupal_add_js(drupal_get_path('module', 'bootstrap_carousel') . '/scripts/bootstrap_carousel.js');
  drupal_add_js(array(
    'bootstrap_carousel' => $carousel_settings,
  ), 'setting');
}

/**
 * Add stylesheet to customize carousel.
 */
function _bootstrap_carousel_load_css() {
  drupal_add_css(drupal_get_path('module', 'bootstrap_carousel') . '/styles/bootstrap_carousel.css');
}

Functions