You are here

function radioactivity_field_widget_form in Radioactivity 7

Same name and namespace in other branches
  1. 7.2 radioactivity.field.inc \radioactivity_field_widget_form()

Implement hook_field_widget().

This widget displayed three text fields\, one each for red, green, and blue. However, the field type defines a single text column, rgb, which needs an HTML color spec. Define an element validate handler that converts our r, g, and b fields into a simulaed single 'rgb' form element.

File

./radioactivity.field.inc, line 312

Code

function radioactivity_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta = 0, $element) {
  if (isset($items[$delta]) && $items[$delta][RADIOACTIVITY_FIELD_TIMESTAMP] > 0) {
    $seconds = time() - $items[$delta][RADIOACTIVITY_FIELD_TIMESTAMP];
    $minutes = floor($seconds / 60);
    $hours = floor($minutes / 60);
    $seconds = $seconds - $minutes * 60;
    $minutes = $minutes - $hours * 60;
    $time_ago = t("Radioactivity energy. Last emission @hour hours @min minutes and @sec seconds ago", array(
      "@hour" => $hours,
      "@min" => $minutes,
      "@sec" => $seconds,
    ));
  }
  else {
    $time_ago = t("Radioactivity energy.");
  }

  /*
  Do a cool hotness meter and add a nice UI to tweak the value
  for example ... make hotter, make hottest and so on
  Raw energy editing is kind of lame.
  */
  $title = $instance['label'];
  $energy = isset($items[$delta][RADIOACTIVITY_FIELD_ENERGY]) ? $items[$delta][RADIOACTIVITY_FIELD_ENERGY] : 0;
  $timestamp = isset($items[$delta][RADIOACTIVITY_FIELD_TIMESTAMP]) ? $items[$delta][RADIOACTIVITY_FIELD_TIMESTAMP] : time();
  $element += array(
    '#type' => 'fieldset',
    '#group' => 'Radioactivity',
    '#title' => $title,
    RADIOACTIVITY_FIELD_ENERGY => array(
      '#type' => 'textfield',
      '#title' => t("Energy"),
      '#default_value' => $energy,
      '#description' => $time_ago,
    ),
    RADIOACTIVITY_FIELD_TIMESTAMP => array(
      '#type' => 'hidden',
      '#title' => t("Last emission"),
      '#default_value' => $timestamp,
    ),
  );
  return $element;
}