Проблема с модулем Imagefield marker

Главные вкладки

Аватар пользователя snegin snegin 30 июня 2013 в 12:04

После установки модуля. У меня почему-то все подсказки уходят в верхний левый угол. И последняя подсказка накладывается на предпоследнюю.

http://i47.fastpic.ru/big/2013/0630/c8/6869213a6fa92e361aa8a4bba6dcb7c8.jpg

Что мне делать? Код модуля

<?php

/**
* file
* Create images with one or more annotations on them.
*/

/**
* Implements hook_permission().
*/
function imagemarker_permission() {
return array(
'add markers to images' => array(
'title' => t('add markers to images'),
),
);
}

/**
* Implements hook_field_formatter_info().
*/
function imagemarker_field_formatter_info() {
return array(
'image_imagemarker' => array(
'label' => t('Image with markers'),
'field types' => array('image'),
),
);
}

/**
* Implements hook_field_formatter_view().
*/
function imagemarker_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {

$element = array();

switch ($display['type']) {
case 'image_imagemarker':
if (!empty($instance['settings']['imagemarker_style'])) {

if (!isset($entity->{$field['field_name']}[$entity->language][0]['fid'])) {
return;
}
$coordinates = _imagemarker_get_coordinates($entity->{$field['field_name']}[$entity->language][0]['fid'], $language);
$entity->{$field['field_name']}[$entity->language][0]['coordinates'] = $coordinates;
$element[0] = imagemarker_render($entity, $instance['settings']['imagemarker_style'], $field['field_name']);
}
break;
}

return $element;
}

/**
* Implements hook_file_delete().
*/
function imagemarker_file_delete($file) {
db_delete('image_marker')->condition('fid', $file->fid)->execute();
}

/**
* Implements hook_element_info_alter().
*/
function imagemarker_element_info_alter(&$type) {
$type['managed_file']['#after_build'][] = 'imagemarker_process_form_element';
}

/**
* Implements hook_form_FORM_ID_alter().
*/
function imagemarker_form_field_ui_field_edit_form_alter(&$form, $form_state) {
if ($form['#field']['type'] == 'image') {
$styles = array();
foreach (image_styles() as $style) {
$styles[$style['name']] = $style['name'];
}
if (count($styles) > 0) {
$form['instance']['settings']['imagemarker_style'] = array(
'#type' => 'select',
'#options' => array(0 => t('None')) + $styles,
'#title' => t('Image marker style'),
'#description' => t('Choose a style if you want to add markers to this image field. This style will be used on the form and display level.'),
'#weight' => 12,
'#default_value' => isset($form_state['build_info']['args'][0]['settings']['imagemarker_style']) ? $form_state['build_info']['args'][0]['settings']['imagemarker_style'] : array(),
);
$form['instance']['settings']['imagemarker_language'] = array(
'#type' => 'checkbox',
'#title' => t('Different image markers per language'),
'#description' => t('Tick this checkbox if you want different markers/annotations per node translation. If this checkbox is unchecked the markers will be the same for every language.'),
'#weight' => 13,
'#default_value' => isset($form_state['build_info']['args'][0]['settings']['imagemarker_language']) ? $form_state['build_info']['args'][0]['settings']['imagemarker_language'] : 0,
);
}
}
}

/**
* Implements hook_form_alter().
*/
function imagemarker_form_alter(&$form, $form_state, $form_id) {

if (!empty($form['#entity_type']) && !empty($form_state['field'])) {

$add_submit = FALSE;

// Loop to find an imagemarker field.
foreach ($form_state['field'] as $field_name => $field) {
foreach ($field as $lang_code => $language_field) {
if (!empty($language_field['instance']['settings']['imagemarker_style'])) {
// Get from storage.
$fid = $form[$field_name][$lang_code][0]['#default_value']['fid'];
// Get the language-specific coordinates if that setting is active.
$language = !empty($language_field['instance']['settings']['imagemarker_language']) ? $lang_code : NULL;
$default = _imagemarker_get_coordinates($fid, $language);
$add_submit = TRUE;
// Build field coordinates textarea.
if (!isset($form['coordinates'])) {
$form['coordinates'] = array('#tree' => TRUE);
}
$form['coordinates'][$field_name] = array(
'#type' => 'textarea',
'#title' => t('Coordinates'),
'#prefix' => '

',
'#suffix' => '

',
'#default_value' => $default,
'#attributes' => array('class' => array('imagemap-coordinates'))
);
}
}
}

// Add a submit handler to this element.
if ($add_submit) {
$form['#submit'][] = 'imagemarker_widget_submit';
}
}
}

/**
* After build function to process the imagemaps widget on forms.
*/
function imagemarker_process_form_element($element, $form_state) {

// Leave alone if we are on the field ui settings.
if (!isset($element['#field_name'])) {
return $element;
}

// Leave element when user doesn't have access.
if (!user_access('add markers to images')) {
return $element;
}

$image_style = _imagemarker_get_fields_enabled_style($element['#entity_type'], $element['#field_name'], $element['#bundle']);

// Leave element when no image style is found for current field.
if (empty($image_style)) {
return $element;
}

$element['imagemarker'] = array(
'#type' => 'markup',
'#weight' => 10,
'#suffix' => '

',
);

if ($element['fid']['#value']) {
$element['imagemarker']['#prefix'] = '

';
$element['imagemarker']['#markup'] = imagemarker_widget($element);
$form['#submit'][] = 'imagemarker_widget_submit';
}
else {
$element['imagemarker']['#prefix'] = '
';
$element['imagemarker']['#markup'] = t('Once uploaded an image you will be able to add markers.');
}

return $element;
}

/**
* Create a widget to create and manage imagemarker.
*
* param $element
* Form element of the image.
*/
function imagemarker_widget(&$element) {

$settings = field_read_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
$image_style = $settings['settings']['imagemarker_style'];
$langcode = $element['#language'];
$output = '';

if (isset($element['#default_value'], $element['#default_value']['uri'])) {
$coordinates = _imagemarker_get_coordinates($element['#default_value']['fid']);
$output .= _imagemarker_render($element['#default_value']['uri'], $coordinates, $image_style);
$properties = _imagemarker_load_imagestyle($image_style);
$output .= '

';

// Attach the javascript and css files for the imagemap UI.
$element['#attached'] = array(
'js' => array(
drupal_get_path('module', 'imagemarker') . '/imagemarker.js',
drupal_get_path('module', 'imagemarker') . '/imagemarker.admin.js'
),
'css' => array(
drupal_get_path('module', 'imagemarker') . '/imagemarker.css'
)
);
}

return $output;

}

/**
* Submit handler to save coordinates for an image with markers.
*/
function imagemarker_widget_submit(&$form, &$form_state) {
if (isset($form_state['values']['coordinates'])) {
foreach ($form_state['values']['coordinates'] as $field_name => $coordinates) {
$fid = $form_state['values'][$field_name][$form_state['values']['language']][0]['fid'];
$language = $form_state['build_info']['args'][0]->language;
$per_language = $form_state['field'][$field_name][$language]['instance']['settings']['imagemarker_language'];
if (!empty($fid)) {
$record = new stdClass();
$record->fid = $fid;
$record->language = $per_language ? $language : NULL;
$record->coordinates = $coordinates;
db_delete('image_marker')->condition('fid', $record->fid)->condition('language', $record->language)->execute();
drupal_write_record('image_marker', $record);
}
}
}
}

/**
* Render the imagemap by entity and imagemap configuration.
*
* param $entity
* The entity object.
* param $image_style
* The name of the image style.
* param $image_field
* The name of the image field.
* return
* Image field.
*/
function imagemarker_render($entity, $image_style, $image_field) {

$language = isset($entity->language) ? $entity->language : LANGUAGE_NONE;
$coordinates = (isset($entity->{$image_field}[$language][0]['coordinates'])) ? $entity->{$image_field}[$language][0]['coordinates'] : '';

$file = file_load($entity->{$image_field}[$language][0]['fid']);
$output = _imagemarker_render($file->uri, $coordinates, $image_style, $language);

$build = array(
'#markup' => $output,
);

if (!empty($coordinates)) {
$build['#attached'] = array(
'js' => array(drupal_get_path('module', 'imagemarker') . '/imagemarker.js'),
'css' => array(drupal_get_path('module', 'imagemarker') . '/imagemarker.css')
);
}

return $build;
}

/**
* Helper function to render the imagemap.
*
* param string $image_uri
* The uri of the image
* param string $storage
* The stored coordinates in raw format
* param $image_style
* The name of the image style.
*
* return
* A string with valid html with the rendered imagemap.
*/
function _imagemarker_render($image_uri, $storage, $image_style) {

$output = '';

$properties = _imagemarker_load_imagestyle($image_style);

$variables = array('path' => $image_uri, 'style_name' => $image_style);
$output .= '

';
$output .= theme('image_style', $variables);

// List the coordinates.
if (!empty($storage)) {
foreach (explode(";", $storage) as $marker) {
parse_str($marker);
// available: x, y, id, description.
$output .= '';
}
}
$output .= '

';

return $output;
}

/**
* Helper function to get DOM properties for image styles.
*
* param $image_style
* The name of the image_style.
*/
function _imagemarker_load_imagestyle($image_style) {

static $image_styles = array();

if (!isset($image_styles[$image_style])) {
// Fetch the $image_style image style, using its width and height
// to wrap the image rendered by image style.
$properties = image_style_load($image_style);
foreach ($properties['effects'] as $effect) {
$image_styles[$image_style]['name'] = $image_style;
$image_styles[$image_style]['width'] = $effect['data']['width'];
$image_styles[$image_style]['height'] = $effect['data']['height'];
break;
}
}

return $image_styles[$image_style];
}

/**
* Helper function to get the image style for an image field.
*
* param $entity_type
* The name of the entity
* param $field_name
* The name of the field.
* param $bundle
* The name of the bundle.
* return
* Image style for marker.
*/
function _imagemarker_get_fields_enabled_style($entity_type, $field_name, $bundle) {

$instance = field_info_instance($entity_type, $field_name, $bundle);

if (isset($instance['settings']['imagemarker_style'])) {
return $instance['settings']['imagemarker_style'];
}

return NULL;
}

/**
* Helper function to get coordinates for an image file.
*
* param $fid
* The id of the file.
* return
* Coordinates for a file.
*/
function _imagemarker_get_coordinates($fid, $language = NULL) {
$coordinates = db_select('image_marker', 'i')
->fields('i', array('coordinates'))
->condition('fid', $fid)
->condition('language', $language)
->execute()
->fetchObject();

return ($coordinates !== FALSE) ? $coordinates->coordinates : '';
}