You are here

public function HeartbeatForm::buildForm in Heartbeat 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/HeartbeatForm.php, line 39

Class

HeartbeatForm
Form controller for Heartbeat edit forms.

Namespace

Drupal\heartbeat\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  //    $this->nodeManager = \Drupal::service('entity_type.manager')->getStorage('node');

  /* @var $entity \Drupal\heartbeat\Entity\Heartbeat */
  $form = parent::buildForm($form, $form_state);
  $entity =& $this->entity;
  if ($entity
    ->isNew()) {
    $form['new_revision'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Create new revision'),
      '#default_value' => FALSE,
      '#weight' => 10,
    );
  }
  $form['uid'] = array(
    '#type' => 'entity_autocomplete',
    '#target_type' => 'user',
    '#default_value' => $entity
      ->getOwner(),
    // A comment can be made anonymous by leaving this field empty therefore
    // there is no need to list them in the autocomplete.
    '#selection_settings' => [
      'include_anonymous' => FALSE,
    ],
    '#title' => $this
      ->t('Authored by'),
    '#description' => $this
      ->t('The owner of the heartbeat'),
  );
  $form['message'] = array(
    '#type' => 'text_format',
    '#description' => t('The Heartbeat message'),
    '#title' => 'Message',
    '#default' => $entity
      ->getMessage()
      ->getValue()[0]['value'],
    '#value' => $entity
      ->getMessage()
      ->getValue()[0]['value'],
  );
  $nodeId = $entity
    ->getNid()
    ->getValue()[0]['target_id'];
  $node = $this->nodeManager
    ->load($nodeId);
  $form['nid'] = array(
    '#type' => 'entity_autocomplete',
    '#entity_type' => 'node',
    '#target_type' => 'node',
    '#selection_handler' => 'default',
    '#default_value' => $node,
    '#title' => 'Node',
    '#description' => t('The node referenced by this Heartbeat'),
  );
  $form['status'] = array(
    '#type' => 'checkbox',
    '#title' => 'Status',
    '#description' => t('Published'),
    '#default_value' => $entity
      ->isPublished(),
  );
  $entity = $this->entity;
  return $form;
}