Computed Field : audio file duration using getID3

To do this in Drupal 7 first install two modules : getID3() and Computed Field.

In the content type that holds the details for each audio track add a field of type 'Computed'
For this field, in the entry 'Computed Field (PHP)' insert the following code:

Stripping out junk from embed codes (regex between tags)

I had a situation in which users were given the ability to embed content from a third-party provider. The default embed code contained a bunch of junk tags, including wrapping itself in a div with forced styles, and links to the provider's site, the user's profile on the provider's site, and links to pages on the provider's site with listings for every tag on the content.

I used computed field to strip out everything except the content between the <object> tags and it worked great!

Converting Horsepower (float) into Kilowatts (float) and custom format it

As i was building a client site, i had a request like this: when he creates a node (Vehicle content type), among all the fields, he fills in a float value for the horsepower of the engine. He wanted the system to automatically convert that value in Kilowatts to and display it along the horsepower value and add a suffix " KW".

After creating the computed field, this are my settings:

Computed Code (PHP)

$entity_field[0]['value'] =  $entity->field_mjet_kuajfuqi['und'][0]['value'] * 0.7456;

Where the field_mjet_kuajfuqi is the Horsepower field (i have named it in albanian) that i referenced to get the value, and multiplied it with the appropriate conversion value for 1 kilowatt.

Then on the Display Code (PHP) i did this:

$mycustomdisplay = number_format($entity_field_item['value'], 1, '.', '');
$display_output = $mycustomdisplay  . " KW";

I applied the number_format php function to force the output to have only 1 value after the decimal point. And after that i concatenated it with the suffix String namely " KW".

In the database storage settings i used:
Data type: float
Data size: normal
Decimal Precision: 10
Decimal Scale: 1 // which did not work and made me use the number format in the display code above.

Subscribe with RSS Subscribe to RSS - computed field