Hello,
hope i'm writing in the right space. I'm new in the community and i'm learning some developing.
I bought the book DRUPAL 7 DEVELOPMENT BY EXAMPLE BEGINNER'S GUIDE and I'm using the version 7.x23
At the chapter 2 (Custom Content Types and an Introduction to Module Development) there is how to make a custom module that will allow us to format a Recipe content duration fields, integers converted to hours and fractions of hours. I followed everything and this is my code for the custom module:

<?php
/**
* Implements hook_field_formatter_info().
*/
function d7d_field_formatter_info() {
return array(
'd7d_integer_duration' => array(
'label' => t('Duration'),
'field types' => array('number_integer'),
)
);
}
/**
* Implements hook_field_formatter_view().
*/
function d7d_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];

switch ($display['type']) {
case 'd7d_integer_duration':
foreach ($items as $delta => $item) {
//some simple math to covert the duration minutes to hours and the //remainder as minutes
$hours = floor($item['value']/60); //divide by minutes in 1 hour //and get floor
$minutes = $item['value']%60; //use the modulus to get the //remainder of minutes
//simple helper function to get gcd of minutes
function gcd($a, $b) {
$b = ( $a == 0 )? 0 : $b;
return ( $a % $b )? gcd($b, abs($a - $b)) : $b;
}
//get greatest common denominator of minutes to convert to //fraction of hours
$minutes_gcd = gcd($minutes, 60);
//⁄ is the html entity for the fraction separator, and we //use the sup and sub html element to give
//the appearance of a fraction
$minutes_fraction = '' . $minutes/$minutes_gcd . '' . 60/$minutes_gcd . ''; $markup = $hours > 0 ? $hours . ' and ' .
$minutes_fraction . ' hours' : $minutes_fraction . ' hours';
//finally, return our formatted value as the markup for this field //formatter
$element[$delta] = array('#markup' => $markup);
}
break;
}
return $element;
}

But when I go MANAGE DYSPLAY in CONTENT TYPES there is not DURATION in the FORMAT list.
Hope i wrote everything that everybody can understand and that somebody can help me.

Thank you

Comments

nichess’s picture

Hi,
did you clear the Cashes at "d7dev/#overlay=admin/config/development/performance"?

Cheers

nichess’s picture

Also I got some error about "Cannot redeclare function gcd()".

Therefore I just changed the code like this:


/**
* Implements hook_field_formatter_info().
*/
function d7dev_field_formatter_info() {
  return array(
    'd7dev_integer_duration' => array(
      'label' => t('Duration'),
      'field types' => array('number_integer'),
    )
  );
}

//simple helper function to get gcd of minutes
		function gcd($a, $b) {
			$b = ( $a == 0 )? 0 : $b;
				return ( $a % $b )? gcd($b, abs($a - $b)) : $b;
			}
		

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

  switch ($display['type']) {
    case 'd7dev_integer_duration':

      foreach ($items as $delta => $item) {
      	//some simple math to covert the duration minutes to hours
		//and the remainder as minutes
		
		//divide by minutes in 1 hour and get floor
		$hours = floor($item['value']/60);
		
		//use the modulus to get the remainder of minutes
		$minutes = $item['value']%60;
		
		
		//get greatest common denominator of minutes to convert to fraction of hours
		$minutes_gcd = gcd($minutes, 60);		
		
		//&frasl; is the html entity for the fraction separator, and we 
		//use the sup and sub html element to give the appearance of a 
		//fraction
		$minutes_fraction = '<sup>' . $minutes/$minutes_gcd . '</sup>&frasl;<sub>' . 60/$minutes_gcd . '</sub>';
		
		$markup = $hours > 0 ? $hours . ' and ' . $minutes_fraction . ' hours' : $minutes_fraction . ' hours';
		//finally, return our formatted value as the markup for this field
		//formatter
		$element[$delta] = array('#markup' => $markup);
		}
		break;
	}

	return $element;
	}
Deciphered’s picture

Status: Active » Fixed

Sorry, this is more of a generic Drupal module development question rather than a question about the Custom Formatters module.

I recommend you try the #drupal-support IRC channel.

Status: Fixed » Closed (fixed)

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