When you create an image field, you loose the ability to display the url of the image style wanted. This formatter exists for file field but isn't inherited for image field.

To correct this bug, we have several options:

1 - rewrite a formatter for image field, allowing us to output the url of a styled image.
2 - using an inheritance mecanism that tells every fields extending File field for instance, can use their formatters and widgets.

Comments

sylvain lecoy’s picture

if you hack the file.fields.inc file, by changing the hook_field_formatter_info(), it work out of the box. Instead of having the image, you have the url of the original file. That mean we have to handle the style and output the right link.

<?php
/**
 * Implements hook_field_formatter_info().
 */
function file_field_formatter_info() {
  return array(
    'file_default' => array(
      'label' => t('Generic file'),
      'field types' => array('file'),
    ),
    'file_table' => array(
      'label' => t('Table of files'),
      'field types' => array('file'),
    ),
    'file_url_plain' => array(
      'label' => t('URL to file'),
      'field types' => array('file', 'image'), // Added image.
    ),
  );
}
?>
sylvain lecoy’s picture

Here is the code to implements in a module if you want a quick work around:

<?php
/**
 * Implements hook_field_info().
 */
function MODULE_field_formatter_info() {
  return array(
    'file_url_plain' => array(
      'label' => t('URL to image'),
      'field types' => array('image'),
      'settings' => array('image_style' => '', 'image_link' => ''),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function MODULE_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  foreach ($items as $delta => $item) {
    $style_path = image_style_path($display['settings']['image_style'], $item['uri']);
    if (!file_exists($style_path)) {
      $style_path = image_style_url($display['settings']['image_style'], $item['uri']);
    }

    $element[$delta] = array(
      '#markup' => empty($style_path) ? '' : file_create_url($style_path),
    );
  }

  return $element;
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function MODULE_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  return image_field_formatter_settings_form($field, $instance, $view_mode, $form, $form_state);
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function MODULE_field_formatter_settings_summary($field, $instance, $view_mode) {
  return image_field_formatter_settings_summary($field, $instance, $view_mode);
}

?>

I think this need some review, but it is pretty straightforward to integrate in the image field.

sylvain lecoy’s picture

Status: Active » Needs review
StatusFileSize
new1.07 KB

Attached a simplier solution

sylvain lecoy’s picture

Patch is ignored.

sylvain lecoy’s picture

Let's submit again..

sylvain lecoy’s picture

damien tournoud’s picture

Don't use a -D7 suffix. It's a weird syntax that causes a patch *not* to be tested :)

sylvain lecoy’s picture

Status: Needs review » Needs work

Ha thanks, anyway I think I missed something in the patch, the correct handling of images styles; here it just gives url to the original file, but the goal was to give url to a styled image.

sylvain lecoy’s picture

Title: Image field should inherits formatters and widgets of File field » Image field should implements image_url_plain view mode
StatusFileSize
new3.91 KB

Attached a patch adding the feature for image field to display image url as plain text.

(Mainly useful for AMF or JSON serialization where you need a specific type of image but not the original URL).

sylvain lecoy’s picture

Status: Needs work » Needs review
StatusFileSize
new3.91 KB

Need review, and with the good patch extension this time.

Status: Needs review » Needs work
sylvain lecoy’s picture

Status: Needs work » Needs review
StatusFileSize
new3.91 KB

Status: Needs review » Needs work
sylvain lecoy’s picture

Status: Needs work » Needs review
StatusFileSize
new3.44 KB

Status: Needs review » Needs work
sylvain lecoy’s picture

Status: Needs work » Needs review
StatusFileSize
new3.44 KB

Status: Needs review » Needs work

The last submitted patch, 992792_Issue.patch, failed testing.

sylvain lecoy’s picture

Status: Needs work » Needs review
StatusFileSize
new3.33 KB

Status: Needs review » Needs work

The last submitted patch, 992792_Issue.patch, failed testing.

sylvain lecoy’s picture

What the hell with that ?!

sylvain lecoy’s picture

Status: Needs work » Needs review
StatusFileSize
new3.44 KB

Tricking the Test Engine with a fake index...

sylvain lecoy’s picture

StatusFileSize
new3.33 KB

In case of

Status: Needs review » Needs work

The last submitted patch, 992792_Image_Feature.patch, failed testing.

sylvain lecoy’s picture

Status: Needs work » Needs review

This image.field.inc changed since 7.0 ?

I can't navigate through git repository neither clone it (i'm on 3G bandwidth with my phone and limits download to 8MB).

sylvain lecoy’s picture

StatusFileSize
new3.78 KB

Let's try this

sylvain lecoy’s picture

StatusFileSize
new3.59 KB

The previous patch actually introduce a tab instead of a space, so use this one instead.

(If it passes obviously..)

sylvain lecoy’s picture

Component: field system » image.module

Changed component to image module.

sylvain lecoy’s picture

This problem can be fixed more generally by: #1133984: Formatting fields values not only for html output.

kenorb’s picture

Issue summary: View changes
Status: Needs review » Needs work

Original image:

<img src="http://localhost/properties/feed/sites/default/files/images/ShowImageXML.asp_173.jpeg" width="683" height="909" /></image>

Generated plain text URL:

http://localhost/properties/feed/sites/default/files/styles//public/images/ShowImageXML.asp_173.jpeg?itok=YI8bqE07

so it seems //public path hasn't been translated into proper URL.

It seems image_style_path($display['settings']['image_style'], $item['uri']); returns "public://styles//public/images/ShowImageXML.asp_173.jpeg", $item['uri'] = "public://images/ShowImageXML.asp_173.jpeg".

kenorb’s picture

Status: Needs work » Needs review
StatusFileSize
new408 bytes

I think suggestion from #1 is not so bad idea, so uploading patch against file.field.inc.

My use case is to print URLs of images for the XML output of view (#903528: Allow for XML fragments), so I need to get rid of the html, however I want to use my own XML tags in TPL, e.g.

$ cat views-view-field--xml2u-feed--page--field-feed-image.tpl.php 
<?= "<image>$output</image>" ?>

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.