scald_gallery.module in Scald: Gallery 7.2
Same filename and directory in other branches
Scald Gallery is a Scald Atom Provider for image galleries.
File
scald_gallery.moduleView source
<?php
/**
* @file
* Scald Gallery is a Scald Atom Provider for image galleries.
*/
/**
* Implements hook_theme().
*/
function scald_gallery_theme() {
return array(
'scald_gallery' => array(
'variables' => array(
'atom' => NULL,
'images' => array(),
'options' => array(),
),
'template' => 'scald_gallery',
),
);
}
/**
* Implements hook_library().
*/
function scald_gallery_library() {
$path = drupal_get_path('module', 'scald_gallery');
$libraries = array(
'gallery' => array(
'title' => 'Scald Gallery',
'version' => '1.0',
'js' => array(
$path . '/scald_gallery.js' => array(),
),
'css' => array(
$path . '/scald_gallery.css' => array(
'type' => 'file',
'media' => 'screen',
),
),
),
);
// Add gallery specific modal settings.
// The atom dialog should be as large as possible.
$libraries['gallery']['js'][] = array(
'type' => 'setting',
'data' => array(
'scald_gallery' => array(
'modalSize' => array(
'type' => 'scale',
'width' => 0.9,
'height' => 0.9,
'addWidth' => 20,
'addHeight' => 15,
),
'modalOptions' => array(
'opacity' => 0.5,
'background-color' => '#000',
),
'animation' => 'fadeIn',
'modalTheme' => 'CToolsSampleModal',
'throbber' => theme('image', array(
'path' => ctools_image_path('ajax-loader.gif', 'ctools_ajax_sample'),
'alt' => t('Loading...'),
'title' => t('Loading'),
)),
),
),
);
return $libraries;
}
/**
* Implements hook_library_alter().
*/
function scald_gallery_library_alter(&$libraries, $module) {
if ($module == 'dnd') {
$libraries['library']['dependencies'][] = array(
'scald_gallery',
'gallery',
);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function scald_gallery_form_scald_atom_add_form_options_alter(&$form, &$form_state) {
// Add extra stuffs to Gallery form.
$atom = isset($form_state['scald']['atom']) ? $form_state['scald']['atom'] : $form_state['scald']['atoms'][0];
if ($atom->provider !== 'scald_gallery') {
return;
}
// Add the original submit handlers to the form state for us to use later.
$form_state['original_submit_handlers'] = $form['#submit'];
$form['#submit'] = array(
'scald_gallery_submit_items',
);
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function scald_gallery_field_widget_atom_reference_textfield_form_alter(&$element, &$form_state, $context) {
// Only alter gallery field gallery_items.
if (!isset($context['form']['#entity'])) {
return;
}
if ($context['instance']['entity_type'] !== 'scald_atom') {
return;
}
$atom = $context['form']['#entity'];
if ($atom->type !== 'gallery' || !isset($atom->provider) || $atom->provider !== 'scald_gallery') {
return;
}
if ($context['delta'] >= count($context['items'])) {
// New empty element.
$data = array(
'title_overriden' => 0,
'title' => '',
'description' => '',
);
}
else {
$data = $atom->data['items'][$context['items'][$context['delta']]['sid']];
}
$element['sid']['#weight'] = -99;
$element['title_overriden'] = array(
'#title' => t('Override this atom title'),
'#type' => 'checkbox',
'#default_value' => $data['title_overriden'],
'#weight' => 20,
);
$langcode = !isset($context['langcode']) ? $context['langcode'] : LANGUAGE_NONE;
$element['title'] = array(
'#title' => t('Title'),
'#type' => 'textfield',
'#default_value' => $data['title'],
'#states' => array(
'visible' => array(
':input[name="atom0[gallery_items][' . $langcode . '][' . $context['delta'] . '][title_overriden]"]' => array(
'checked' => TRUE,
),
),
),
'#size' => 90,
'#maxlength' => 255,
'#weight' => 21,
);
$element['description'] = array(
'#title' => t('Description'),
'#type' => 'textarea',
'#default_value' => $data['description'],
'#maxlength' => 1000,
'#rows' => 2,
'#weight' => 22,
);
}
/**
* Special form submit handler to temporarily save a gallery atom.
*
* Save the current form into object cache so that it could be restored later.
* It also updates the gallery data with items title and description.
*/
function scald_gallery_submit_items(&$form, &$form_state) {
if (empty($form_state['scald']['atoms'])) {
return;
}
$atoms = $form_state['scald']['atoms'];
$atom = reset($atoms);
// Save extra gallery items title and description.
$atom->data['items'] = array();
$gallery_items_langcode = $form['atom0']['gallery_items']['#language'];
foreach ($form_state['values']['atom0']['gallery_items'][$gallery_items_langcode] as $input) {
if (empty($input['sid'])) {
continue;
}
$atom->data['items'][$input['sid']] = array(
'title_overriden' => $input['title_overriden'],
'title' => $input['title'],
'description' => $input['description'],
);
}
$form['#submit'] = $form_state['original_submit_handlers'];
form_execute_handlers('submit', $form, $form_state);
}
/**
* Implements hook_scald_atom_providers().
*/
function scald_gallery_scald_atom_providers() {
return array(
'gallery' => 'Gallery items upload',
);
// This code will never be hit, but is necessary to mark the string
// for translation on localize.d.o
// @codingStandardsIgnoreStart
t('Gallery items upload');
// @codingStandardsIgnoreEnd
}
/**
* Implements hook_scald_add_form().
*/
function scald_gallery_scald_add_form(&$form, &$form_state) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Gallery title'),
'#required' => TRUE,
);
}
/**
* Implements hook_scald_add_form_fill().
*/
function scald_gallery_scald_add_form_fill($atom, $form, $form_state) {
$atom->title = $form_state['values']['title'];
}
/**
* Implements hook_scald_register_atom().
*/
function scald_gallery_scald_register_atom($atom, $mode) {
scald_gallery_update_gallery_thumbnail($atom);
}
/**
* Implements hook_scald_update_atom().
*/
function scald_gallery_scald_update_atom($atom, $mode) {
scald_gallery_update_gallery_thumbnail($atom);
}
/**
* Updates the gallery thumbnail.
*
* If the thumbnail field is empty, use the first item thumbnail as the gallery
* thumbnail.
*/
function scald_gallery_update_gallery_thumbnail($atom) {
if (!field_get_items('scald_atom', $atom, 'scald_thumbnail') && ($items = field_get_items('scald_atom', $atom, 'gallery_items'))) {
foreach ($items as $item) {
$item_atom = scald_atom_load($item['sid']);
if (field_get_items('scald_atom', $item_atom, 'scald_thumbnail')) {
$atom->scald_thumbnail = $item_atom->scald_thumbnail;
break;
}
}
}
}
/**
* Implements hook_scald_fetch().
*/
function scald_gallery_scald_fetch($atom, $type) {
$items = field_get_items('scald_atom', $atom, 'scald_thumbnail');
if ($items && file_exists($items[0]['uri'])) {
$atom->file_source = $atom->thumbnail_source = $items[0]['uri'];
}
}
/**
* Implements hook_scald_prerender().
*
* For use with a player other than scald_galleria.
*/
function scald_gallery_scald_prerender($atom, $context, $options, $mode) {
if ($mode == 'atom') {
$config = scald_context_config_load($context);
$player = $config->player['gallery']['*'];
if ('default' != $player) {
return;
}
// Prerender gallery as an image atom.
// Find out which transcoder is in use, and checks if it's
// one of the transcoder provided by Scald Image.
$style_name = NULL;
$mappings = NULL;
$transcoder = $config->transcoder['gallery']['*'];
// Image style support.
if (preg_match('/^style-(.*)$/', $transcoder, $match)) {
$style_name = $match[1];
}
elseif (module_exists('picture') && preg_match('/^group-(.*)$/', $transcoder, $match)) {
$mappings = picture_mapping_load($match[1]);
}
// Default attributes, which can be overridden by field settings.
$attributes = array(
'alt' => $atom->title,
'title' => $atom->title,
);
$items = field_get_items('scald_atom', $atom, 'scald_thumbnail');
$thumbnail = !empty($items) ? $items[0] : array();
foreach (array(
'alt',
'title',
'width',
'height',
) as $attribute_name) {
if (isset($thumbnail[$attribute_name]) && $thumbnail[$attribute_name]) {
$attributes[$attribute_name] = $thumbnail[$attribute_name];
}
}
if (!empty($style_name)) {
$atom->rendered->player = theme('image_style', array(
'path' => $atom->file_source,
'style_name' => $style_name,
) + $attributes);
}
elseif (isset($mappings)) {
foreach ($mappings->mapping as $breakpoint_name => $multipliers) {
if (!empty($multipliers)) {
foreach ($multipliers as $multiplier => $image_style) {
if (!$image_style) {
continue;
}
// $image_style is machine name in Picture 1.x and an array in
// Picture 2.x.
if (is_array($image_style) && $image_style['mapping_type'] === '_none') {
continue;
}
$fallback_image_style = is_array($image_style) ? $image_style['image_style'] : $image_style;
break 2;
}
}
}
// The fallback_image_style is the first image style we find, and so if it
// is empty then we do not have any image style.
if (!empty($fallback_image_style)) {
$atom->rendered->player = theme('picture', array(
'uri' => $atom->file_source,
'style_name' => $fallback_image_style,
'breakpoints' => $mappings->mapping,
) + $attributes);
}
}
else {
$path = empty($atom->rendered->file_transcoded_url) ? $atom->file_source : $atom->rendered->file_transcoded_url;
$atom->rendered->player = theme('image', array(
'path' => $path,
) + $attributes);
}
}
}