View source
<?php
namespace Drupal\heartbeat\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\Annotation\EntityType;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\heartbeat\Entity;
use Symfony\Component\DependencyInjection\ContainerInterface;
class HeartbeatForm extends ContentEntityForm {
protected $nodeManager;
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function __construct(EntityTypeManager $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
$this->nodeManager = $entity_type_manager
->getStorage('node');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$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(),
'#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;
}
public function save(array $form, FormStateInterface $form_state) {
$entity =& $this->entity;
if (!$form_state
->isValueEmpty('new_revision') && $form_state
->getValue('new_revision') != FALSE) {
$entity
->setNewRevision();
$entity
->setRevisionCreationTime(REQUEST_TIME);
$entity
->setRevisionUserId(\Drupal::currentUser()
->id());
}
else {
$entity
->setNewRevision(FALSE);
}
$status = parent::save($form, $form_state);
switch ($status) {
case SAVED_NEW:
drupal_set_message($this
->t('Created the %label Heartbeat.', [
'%label' => $entity
->label(),
]));
break;
default:
drupal_set_message($this
->t('Saved the %label Heartbeat.', [
'%label' => $entity
->label(),
]));
}
$form_state
->setRedirect('entity.heartbeat.canonical', [
'heartbeat' => $entity
->id(),
]);
}
}