6

In a custom formatter that I'm writing, I have a problem with default_value for an image style.

function MYMODULE_field_formatter_info() {
  return array(
    'MYMODULE_formatter' => array(
      'label' => t('My Custom Formatter'),
      'field types' => array('image'),
      'multiple values' => FIELD_BEHAVIOR_DEFAULT,
      'settings' => array(
        'trigger_image_style' => '',
      )
    ),
  );
}


function MYMODULE_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state){
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];

  $element = array();

  $image_styles = image_style_options(FALSE);
  $element['trigger_image_style'] =  array(
    '#type' => 'select',
    '#title' => t('Trigger image style:'),
    '#description' => t('Image style to be used for trigger'),
    '#default_value' => $settings['trigger_image_style'],
    '#options' => $image_styles,
  );

  return $element;
}

function MYMODULE_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $elements = array();
  $html = "";
  $settings = $display['settings'];

  // ensure deaults?
  if (!isset($settings['trigger_image_style'])) $settings['trigger_image_style'] = '';
  //.....
}

Seems that Drupal will return incorrect path:

image_style_url($settings['trigger_image_style'], $item['uri']);

until I explicitly set a style of my choice. The incorrect path will have double slash, as in:

http://example.com/sites/default/files/styles//public/trigger-images/test1.jpg

I thought that empty string simply means 'use original image'... How should I tell Drupal to use original image if the style is not set or is an empty string?

1
  • Why you left Drupal ?
    – Yuseferi
    Jun 30 '16 at 11:46
1

It is have simple solution when you want specified image_style use something like

  image_style_url('850x450',$file->uri);

and when you don't want use any image style and want use origin image use

 file_create_url($file->uri);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.