There's another module called Ranges in thepearson's sandbox with the same purpose which I mentioned here: #1912804: Merge with thepearson's Ranges sandbox module?.
There's a really useful feature that thepearson managed to implement in his own module, which is here: #1846952: Provide separate prefix and suffix values for the start and end values.
So separate prefixes and suffixes are provided for the field's start and end values, which can be really useful in some cases.
Here's an example, which was mentioned in the linked issue:

testprefix 1234 testsuffix — another testprefix 234132 another testsuffix

Couldn't you also implement this or merge your projects somehow?

Thanks in advance!

Comments

taran2l’s picture

Project: » Range
Version: » 7.x-1.0
Sk8erPeter’s picture

Version: 7.x-1.0 » 7.x-1.x-dev

Do you have any plans for this? :)
thepearson solved it in the ex-Ranges module (mentioned in #1912804: Merge with thepearson's Ranges sandbox module?) with providing separate prefix and suffix instance settings for the separate start and end values.

Here are some relevant code snippets taken from the file ranges.module, see the ['start']['prefix'], ['start']['suffix'], ['end']['prefix'] and ['end']['suffix'] array indices:

line 55-95:

/**
 * Implements hook_field_info().
 */
function ranges_field_info() {
  return array(
    'ranges' => array(
      'label' => t('Range'),
      'description' => t('Provides ability to specify numerical ranges.'),
      'instance_settings' => array(
        'start' => array(
          'min_value' => '',
          'max_value' => '',
          'prefix' => '',
          'suffix' => '',
          'size' => 12,
          'title' => t('Start'),
        ),
        'end' => array(
          'min_value' => '',
          'max_value' => '',
          'prefix' => '',
          'suffix' => '',
          'size' => 12,
          'title' => t('End'),
        ),
        'formatting' => array(
          'prefix' => '',
          'suffix' => '',
          'indicator' => '-',
        ),
      ),
      'settings' => array(
        'type' => 'integer',
      ),
      'default_widget' => 'ranges_textfield',
      'default_formatter' => 'default',
      'property_type' => 'field_ranges_field',
      'property_callbacks' => array('ranges_field_property_info_callback'),
    ),
  );
}

line 200-353 :

/**
 * Implements hookd_field_instance_settings_form().
 */
function ranges_field_instance_settings_form($field, $instance) {
  $settings = $instance['settings'];

  $form['start'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#title' => t('Start value limits'),
  );

  $form['start']['min_value'] = array(
    '#type' => 'textfield',
    '#title' => t('Min'),
    '#size' => 12,
    '#description' => t('Specify a minimum value for the start value. Leave blank for none.'),
    '#default_value' => isset($settings['start']['min_value']) ? $settings['start']['min_value'] : '',
  );

  $form['start']['max_value'] = array(
    '#type' => 'textfield',
    '#title' => t('Max'),
    '#size' => 12,
    '#description' => t('Specify a maximum value for the start value. Leave blank for none.'),
    '#default_value' => isset($settings['start']['max_value']) ? $settings['start']['max_value'] : '',
  );

  $form['start']['prefix'] = array(
    '#type' => 'textfield',
    '#title' => t('Start prefix'),
    '#size' => 12,
    '#description' => t('Specify a value to place in front of the start field. (eg $)'),
    '#default_value' => isset($settings['start']['prefix']) ? $settings['start']['prefix'] : '',
  );

  $form['start']['suffix'] = array(
    '#type' => 'textfield',
    '#title' => t('Start suffix'),
    '#size' => 12,
    '#description' => t('Specify a value to place after the start field. (eg .00)'),
    '#default_value' => isset($settings['start']['suffix']) ? $settings['start']['suffix'] : '',
  );

  $form['start']['size'] = array(
    '#type' => 'textfield',
    '#title' => t('Size'),
    '#size' => 12,
    '#description' => t('Specify a size for the field.'),
    '#default_value' => isset($settings['start']['size']) ? $settings['start']['size'] : 6
  );

  $form['start']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#size' => 20,
    '#description' => t('Specify a maximum value for the start value. Leave blank for none.'),
    '#default_value' => isset($settings['start']['title']) ? $settings['start']['title'] : t('Start'),
  );

  $form['end'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#title' => t('End value limits'),
  );

  $form['end']['min_value'] = array(
    '#type' => 'textfield',
    '#title' => t('Min'),
    '#size' => 12,
    '#description' => t('Specify a minimum value for the end value. Leave blank for none.'),
    '#default_value' => isset($settings['end']['min_value']) ? $settings['end']['min_value'] : '',
  );

  $form['end']['max_value'] = array(
    '#type' => 'textfield',
    '#title' => t('Max'),
    '#size' => 12,
    '#description' => t('Specify a maximum value for the end value. Leave blank for none.'),
    '#default_value' => isset($settings['end']['max_value']) ? $settings['end']['max_value'] : '',
  );

  $form['end']['prefix'] = array(
    '#type' => 'textfield',
    '#title' => t('End prefix'),
    '#size' => 12,
    '#description' => t('Specify a value to place in front of the end field. (eg $)'),
    '#default_value' => isset($settings['end']['prefix']) ? $settings['end']['prefix'] : '',
  );

  $form['end']['suffix'] = array(
    '#type' => 'textfield',
    '#title' => t('End suffix'),
    '#size' => 12,
    '#description' => t('Specify a value to place after the end field. (eg .00)'),
    '#default_value' => isset($settings['end']['suffix']) ? $settings['end']['suffix'] : '',
  );

  $form['end']['size'] = array(
    '#type' => 'textfield',
    '#title' => t('Size'),
    '#size' => 12,
    '#description' => t('Specify a size for the field.'),
    '#default_value' => isset($settings['end']['size']) ? $settings['end']['size'] : 6,
  );

  $form['end']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#size' => 20,
    '#description' => t('Specify a maximum value for the end value. Leave blank for none.'),
    '#default_value' => isset($settings['end']['title']) ? $settings['end']['title'] : t('End'),
  );

  $form['formatting'] = array(
    '#type' => 'fieldset',
    '#title' => t('More'),
    '#description' => t('These formatting options affect the range as a whole.'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['formatting']['prefix'] = array(
    '#type' => 'textfield',
    '#title' => t('Prefix'),
    '#size' => 12,
    '#description' => t('Specify a value to place in front of the range field.'),
    '#default_value' => isset($settings['formatting']['prefix']) ? $settings['formatting']['prefix'] : '',
  );

  $form['formatting']['suffix'] = array(
    '#type' => 'textfield',
    '#title' => t('Suffix'),
    '#size' => 12,
    '#description' => t('Specify a value to place after the entire range field.'),
    '#default_value' => isset($settings['formatting']['suffix']) ? $settings['formatting']['suffix'] : '',
  );

  $form['formatting']['indicator'] = array(
    '#type' => 'textfield',
    '#title' => t('Sperator'),
    '#size' => 12,
    '#description' => t('Specify an indicator to display between the two range values.'),
    '#default_value' => isset($settings['formatting']['indicator']) ? $settings['formatting']['indicator'] : '-',
  );

  $form['formatting']['force_end_greater'] = array(
    '#type' => 'checkbox',
    '#title' => t('Force end to be greater'),
    '#description' => t('Check this box to force the "end" range value to be greater than the "start" value.'),
    '#default_value' => isset($settings['formatting']['force_end_greater']) ? $settings['formatting']['force_end_greater'] : 0,
  );

  return $form;
}

line 356-389:

/**
 * Implements hook_field_widget_form().
 */
function ranges_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  if ($instance['widget']['type'] == 'ranges_textfield') {

    $element['range'] = array(
      '#type' => 'fieldset',
      '#title' => check_plain($instance['label']),
    );

    $element['range']['range_start'] = array(
      '#type' => 'textfield',
      '#prefix' => isset($instance['settings']['formatting']['prefix']) ? $instance['settings']['formatting']['prefix'] : NULL,
      '#title' => isset($instance['settings']['start']['title']) ? $instance['settings']['start']['title'] : t('Start'),
      '#field_prefix' => isset($instance['settings']['start']['prefix']) ? $instance['settings']['start']['prefix'] : NULL,
      '#field_suffix' => isset($instance['settings']['start']['suffix']) ? $instance['settings']['start']['suffix'] : NULL,
      '#size' => isset($instance['settings']['start']['size']) ? $instance['settings']['start']['size'] : 6,
      '#default_value' => isset($items[$delta]['range_start']) ? $items[$delta]['range_start'] : '',
    );

    $element['range']['range_end'] = array(
      '#type' => 'textfield',
      '#suffix' => isset($instance['settings']['formatting']['suffix']) ? $instance['settings']['formatting']['suffix'] : NULL,
      '#title' => isset($instance['settings']['end']['title']) ? $instance['settings']['end']['title'] : t('End'),
      '#field_prefix' => isset($instance['settings']['end']['prefix']) ? $instance['settings']['end']['prefix'] : NULL,
      '#field_suffix' => isset($instance['settings']['end']['suffix']) ? $instance['settings']['end']['suffix'] : NULL,
      '#size' => isset($instance['settings']['end']['size']) ? $instance['settings']['end']['size'] : 6,
      '#default_value' => isset($items[$delta]['range_end']) ? $items[$delta]['range_end'] : '',
    );
  }
  return $element;
}

line 518-536:

/**
 * Implements hook_field_formatter_view().
 */
function ranges_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  foreach ($items as $delta => $item) {
    $element[$delta] = array('#markup' => implode('', array(
      $instance['settings']['formatting']['prefix'] . ' ',
      $instance['settings']['start']['prefix'],
      $item['range_start'],
      $instance['settings']['start']['suffix'] . ' ',
      $instance['settings']['formatting']['indicator'] . ' ',
      $instance['settings']['end']['prefix'],
      $item['range_end'],
      $instance['settings']['end']['suffix'] . ' ',
      $instance['settings']['formatting']['suffix'],
    )));
  }
  return $element;
}

etc.

Thanks in advance!

taran2l’s picture

Status: Active » Fixed

Thanks, committed and pushed.

Sk8erPeter’s picture

Wow, thank you very much, Taran2L! :) I've just tested it, and it works.

And I'm really sorry that I reopen this issue, but couldn't we ALSO keep the old prefixes and suffixes, like the ex-Ranges module did? (Sorry that I didn't mention it in the original feature request. :( )
I know it may sound silly, but there may be some weird cases it's needed, and it would be much better to keep another additional opportunity to make this module even better.
So what I'm thinking about is the following (see WRAPPERPREFIX and WRAPPERSUFFIX):

WRAPPERPREFIX testprefix 1234 testsuffix — another testprefix 234132 another testsuffix WRAPPERSUFFIX

This is how it looks like now:

Range prefixes and suffixes

What I think would be even better:

Range prefixes and suffixes with wrapper prefixes and suffixes

And this is what the field edit form would look like:

Range field edit form

I hope you won't find this a silly idea, and there's a chance to implement it too (e.g. with putting back some parts of the old code).
I'll try to help with a patch as soon as I'll have time if needed.

Thank you very much in advance again!

oleh chemerys’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new4.95 KB

Here is a patch adding whole field value wrapper.

taran2l’s picture

Looks good, except couple things:

  1. Rename all occurrences of wrapper to field
  2. Put field prefix/suffix into separate fieldset entitles Field
  3. Forgot to add prefix/suffix to field form element
function _range_field_formatter_wrapper_prefix_suffix($output, $instance_settings) {
  if (!empty($instance_settings['wrapper_prefix'])) {
    $output = $instance_settings['wrapper_prefix'] . $output;
  }
  if (!empty($instance_settings['wrapper_suffix'])) {
    $output = $output . $instance_settings['wrapper_suffix'];
  }

  return $output;
}

Can be simplified to

return $instance_settings['field_prefix'] . $output . $instance_settings['field_suffix'];
'#description' => t("Define a string that should be prefixed to the whole field value. Leave blank for none."),

Use single quotes.

taran2l’s picture

Status: Needs review » Needs work
oleh chemerys’s picture

Status: Needs work » Needs review
StatusFileSize
new12.23 KB

Thanks for suggestions, here is an update.

taran2l’s picture

Status: Needs review » Fixed

Thanks, reviewed and committed (with some small changes).

  • Taran2L committed cca30ad on PAReview
    Issue #1912808 by Sk8erPeter: Provide separate prefix and suffix values...
  • Taran2L committed 9e3c688 on PAReview authored by oleg chemerys
    Issue #1912808 by oleg chemerys | Sk8erPeter: Added separate prefix and...

  • Taran2L committed cca30ad on PAReview
    Issue #1912808 by Sk8erPeter: Provide separate prefix and suffix values...
  • Taran2L committed 9e3c688 on PAReview authored by oleg chemerys
    Issue #1912808 by oleg chemerys | Sk8erPeter: Added separate prefix and...

  • Taran2L committed cca30ad on PAReview
    Issue #1912808 by Sk8erPeter: Provide separate prefix and suffix values...
  • Taran2L committed 9e3c688 on PAReview authored by oleg chemerys
    Issue #1912808 by oleg chemerys | Sk8erPeter: Added separate prefix and...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.