Hello,

I'm using time period to save preparation and resting times for a recipes website.

The problem is that when the recipe have no resting times, the field register 0 seconds. I don't find a way to not display the field if it's empty.

Can you point me the solution ?

Thank you

Alex

CommentFileSizeAuthor
#3 timeperiod-2092835-3.patch2.08 KBDavid Lesieur
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

riccardino’s picture

Issue summary: View changes

I had the same problem. My solution was to modify the timeperiod_field_formatter_view function in the timeperiod.module file like this:

/**
 * Implements hook_field_formatter_view().
 */
function timeperiod_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];

  switch ($display['type']) {

    case 'timeperiod_default':
      foreach ($items as $delta => $item) {
        // ADDED IF (do not show 0 sec values)
        if(!empty($item['value']) && (string) $item['value'] !== '0') { 
          $output = format_interval($item['value'], $settings['granularity']);
          // Prefix/Suffix handling taken from number_field_formatter_view(),
          if ($settings['prefix_suffix']) {
            $prefixes = isset($instance['settings']['prefix']) ? array_map('field_filter_xss', explode('|', $instance['settings']['prefix'])) : array('');
            $suffixes = isset($instance['settings']['suffix']) ? array_map('field_filter_xss', explode('|', $instance['settings']['suffix'])) : array('');
            $prefix = (count($prefixes) > 1) ? format_plural($item['value'], $prefixes[0], $prefixes[1]) : $prefixes[0];
            $suffix = (count($suffixes) > 1) ? format_plural($item['value'], $suffixes[0], $suffixes[1]) : $suffixes[0];
            $output = $prefix . $output . $suffix;
          }
          $element[$delta] = array('#markup' => $output);
        }
      }
      break;
  }
  return $element;
}
zmove’s picture

Thank you for the answer, I would prefer to not modify the core of a module to make it works as I want.

If I make a patch, does the maintainer will implement it, it is a correct default behavior the module want to support ?

David Lesieur’s picture

Status: Active » Needs review
FileSize
2.08 KB

Here's a patch to hide the field when the time period is empty.

fakir22’s picture

Patch works like a charm, thanks !