HeartbeatFeedForm.php in Heartbeat 8
File
src/Form/HeartbeatFeedForm.php
View source
<?php
namespace Drupal\heartbeat\Form;
use Drupal\Core\Form\FormAjaxException;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Ajax;
use Drupal\heartbeat\Ajax\SelectFeedCommand;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\heartbeat\HeartbeatService;
use Drupal\heartbeat\HeartbeatTypeService;
use Drupal\heartbeat\HeartbeatStreamServices;
use Drupal\Core\Ajax\AjaxResponse;
class HeartbeatFeedForm extends FormBase {
protected $heartbeatService;
protected $typeService;
protected $streamService;
private $stream;
private $streams;
public function __construct(HeartbeatService $heartbeat, HeartbeatTypeService $heartbeat_heartbeattype, HeartbeatStreamServices $heartbeatstream) {
$this->heartbeatService = $heartbeat;
$this->typeService = $heartbeat_heartbeattype;
$this->streamService = $heartbeatstream;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('heartbeat'), $container
->get('heartbeat.heartbeattype'), $container
->get('heartbeatstream'));
}
public function getFormId() {
return 'heartbeat_feed_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
if ($this->stream === null) {
$this->stream = $form_state
->getValue('feedtabs');
}
if ($this->streams === null) {
foreach ($this->streamService
->getAllStreams() as $heartbeatStream) {
$this->streams[$heartbeatStream
->getName()] = t($heartbeatStream
->getName());
}
}
$form['feedtabs'] = [
'#type' => 'radios',
'#title' => $this
->t('Choose a feed'),
'#options' => $this->streams,
'#ajax' => [
'callback' => '::updateFeed',
'progress' => array(
'type' => 'none',
),
'#default' => $this->streams['Public'],
],
];
$form['#attached']['library'][] = 'heartbeat/heartbeat';
return $form;
}
public function updateFeed(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response
->addCommand(new SelectFeedCommand($form_state
->getValue('feedtabs')));
return $response;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$stophere = null;
$stopthere = null;
foreach ($form_state
->getValues() as $key => $value) {
drupal_set_message($key . ': ' . $value);
}
}
}