[РЕШЕНО] Title и Alt для картинок - как менять?

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

Аватар пользователя Обухов Никита Обухов Никита 11 ноября 2008 в 21:24

Здравствуйте,
Есть материал, в котором содержится поле CCk imagefield. Пользователь загружает картинку, картинка ложится в нужную директорию. Нужно на этом этапе формировать для картинки $alt и $title с которыми она будет выводиться <img src="image" alt="$alt" title="$title">

В $title будут записываться термины одного словаря, а в $alt термин другого словаря и название ноды. Воть...

Подскажите, пожалуйста, как быть.

[b]upd.[/b] Решение для imagefield и imagecache+imagefield в камментсах.

Комментарии

Аватар пользователя rodman1980 rodman1980 12 ноября 2008 в 11:07

В imagefield есть функция

function theme_imagefield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
 
  $file = (array)$file;
  if (!$getsize || (is_file($file['filepath']) && (list($width, $height, $type, $image_attributes) = [user=getimagesize]getimagesize[/user]($file['filepath'])))) {
    $attributes = drupal_attributes($attributes);
   
    $path = $file['fid'] == 'upload' ? $file['preview'] : $file['filepath'];
    $alt = empty($alt) ? $file['alt'] : $alt;
    $title = empty($title) ? $file['title'] : $title;
   
    $url = file_create_url($path);
    return '<img src="'. check_url($url) .'" alt="'.
        check_plain($alt) .'" title="'. check_plain($title) .'" '. $image_attributes . $attributes .' />';
  }
}

Ее вам и надо использовать создайте ее копию в файле template.tpl.php расположенном в папке вашей темы, только измените ее название на имя_вашей_темы_imagefield_image или на:

function phptemplate_imagefield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
 
  $file = (array)$file;
  if (!$getsize || (is_file($file['filepath']) && (list($width, $height, $type, $image_attributes) = [user=getimagesize]getimagesize[/user]($file['filepath'])))) {
    $attributes = drupal_attributes($attributes);
   
    $path = $file['fid'] == 'upload' ? $file['preview'] : $file['filepath'];
    $alt = empty($alt) ? $file['alt'] : $alt;
    $title = empty($title) ? $file['title'] : $title;
   
    $url = file_create_url($path);
    return '<img src="'. check_url($url) .'" alt="'.
        check_plain($alt) .'" title="'. check_plain($title) .'" '. $image_attributes . $attributes .' />';
  }
}

соответственно внутри нее вы и измените переменный $alt и $title

Аватар пользователя Обухов Никита Обухов Никита 13 ноября 2008 в 16:57

Ой, прошу прощения - не сказал что еще и imagecache есть для урезания размеров.

Решил так:

function phptemplate_imagecache($namespace, $path, $alt = '', $title = '', $attributes = NULL) {
    $node = node_load(array('nid' => arg(1)));

  if (is_null($attributes)) {
    $attributes['class'] = 'imagecache imagecache-'. $namespace;
  }
  $attributes = drupal_attributes($attributes);
  $imagecache_url = imagecache_create_url($namespace, $path);
 
  if($node) {
  $alt = /*Изменяем альт*/
  $title = /*Изменяем заголовок*/
  }
  return '<img src="'. $imagecache_url .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $attribut

es .' />';
}

Аватар пользователя Nodachi Nodachi 22 января 2009 в 19:22

Попробовал способ из последнего каммента - работает. Но только лишь при отображении ноды целиком. А когда на экран выводятся тизеры с картинкой в каждом из них - не пашет (((
Как и раньше, выводит имя файла. В пхп не силен, шаманство мое не помогло. Максимум, чего добивался - в отображении тизера были пустые теги alt и tittle...

Аватар пользователя Макс74 Макс74 9 февраля 2009 в 12:55

Для себя решил так (drupal 6.9):
в файле imagecache.module находим функцию
function imagecache_field_formatter($field, $item, $formatter, $node) {

и после кода
$style = array_pop($parts);
$presetname = implode('_', $parts);

добавляем
if ($node) {
$alt=$node->title;
$title=$node->title;
}

Аватар пользователя Обухов Никита Обухов Никита 2 марта 2009 в 18:24

Nodachi, там та же функция должна использоваться.

После этого какой результат?

function phptemplate_imagecache($namespace, $path, $alt = '', $title = '', $attributes = NULL) {
   if (is_null($attributes)) {
    $attributes['class'] = 'imagecache imagecache-'. $namespace;
  }
  $attributes = drupal_attributes($attributes);
  $imagecache_url = imagecache_create_url($namespace, $path);
 
  $alt = 'hello';
  $title = 'world';

  return '<img src="'. $imagecache_url .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $attributes .' />';
}