You are here

function radioactivity_field_widget_form in Radioactivity 7.2

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

Implement hook_field_widget().

This widget displays the radioactive energy field.

File

./radioactivity.field.inc, line 448

Code

function radioactivity_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta = 0, $element) {
  if (isset($items[$delta][RADIOACTIVITY_FIELD_TIMESTAMP]) && $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.");
  }
  $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,
    ),
  );
  if ($instance['settings']['history']) {
    drupal_add_js(drupal_get_path('module', 'radioactivity') . '/js/radioactivity-history.js');
    $entity =& $form['#entity'];
    $info = entity_get_info($instance['entity_type']);
    $id = $info['entity keys']['id'];
    if (isset($entity->{$id})) {
      $entity_id = $entity->{$id};
      $data = _radioactivity_get_history_for_field_and_entity($instance['id'], $entity_id);
      $element += array(
        RADIOACTIVITY_HISTORY_GRAPH => array(
          '#theme' => 'radioactivity_history',
          '#dataset' => $data,
        ),
      );
    }
  }
  return $element;
}