You are here

function heartbeat_activity_form in Heartbeat 7

Generate the heartbeat activity add/edit form. This entity form is always referenced to a stream.

1 string reference to 'heartbeat_activity_form'
heartbeat_forms in ./heartbeat.module
Implements hook_forms(). All heartbeat template forms share the same form handler.

File

./heartbeat.entity.inc, line 211

Code

function heartbeat_activity_form($form, &$form_state, HeartbeatActivity $heartbeatActivity, HeartbeatStream $heartbeatStream) {

  // During initial form build, add the heartbeatActivity entity to the form state for use
  // during form building and processing. During a rebuild, use what is in the form state.
  if (!isset($form_state['heartbeat_activity'])) {
    if (!isset($heartbeatActivity->title)) {
      $heartbeatActivity->title = NULL;
    }
    $form_state['heartbeat_activity'] = $heartbeatActivity;
  }
  else {
    $heartbeatActivity = $form_state['heartbeat_activity'];
  }
  $form['#attributes']['class'][] = 'heartbeat-activity-form';
  if (!empty($heartbeatActivity->type)) {
    $form['#attributes']['class'][] = 'heartbeat-activity-' . $heartbeatActivity->type . '-form';
  }

  // Basic heartbeat activity information.
  // These elements are just values so they are not even sent to the client.
  foreach (array(
    'uaid',
    'uid',
    'uid_target',
    'nid',
    'nid_target',
    'timestamp',
    'message_id',
    'language',
  ) as $key) {
    $form[$key] = array(
      '#type' => 'value',
      '#value' => isset($heartbeatActivity->{$key}) ? $heartbeatActivity->{$key} : 0,
    );
  }

  // @todo D8: Remove. Modules should access the activity using $form_state['heartbeat_activity'].
  $form['#heartbeat_activity'] = $heartbeatActivity;
  $form['#heartbeat_stream'] = $heartbeatStream;

  // Activity author information.
  $form['name'] = array(
    '#type' => 'value',
    '#value' => !empty($heartbeatActivity->actor->name) ? $heartbeatActivity->actor->name : '',
  );

  // Add action buttons.
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#weight' => 5,
    '#ajax' => array(
      'callback' => 'heartbeat_activity_form_callback',
      'wrapper' => 'heartbeat-status-wrapper',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );

  // The complete form as ajax wrapper, validate and submit callbacks.
  $form['#prefix'] = '<div id="heartbeat-status-wrapper">';
  $form['#suffix'] = '</div>';
  $form['#validate'][] = 'heartbeat_activity_form_validate';
  $form['#submit'][] = 'heartbeat_activity_form_submit';
  field_attach_form('heartbeat_activity', $heartbeatActivity, $form, $form_state, $heartbeatActivity->language);
  return $form;
}