public function StatusForm::statusAjaxSubmit in Heartbeat 8
File
- modules/statusmessage/src/Form/StatusForm.php, line 204
Class
- StatusForm
- Form controller for Status edit forms.
Namespace
Drupal\statusmessage\Form
Code
public function statusAjaxSubmit(array &$form, FormStateInterface $form_state) {
$message = $form_state
->getValue('message');
$file = $form_state
->getValue('media');
if (strlen(trim($message)) > 1) {
preg_match_all('#\\bhttps?://[^,\\s()<>]+(?:\\([\\w\\d]+\\)|([^,[:punct:]\\s]|/))#', $message, $match);
if ($this->markupgenerator !== NULL && !empty($match) && array_values($match)[0] !== NULL) {
$url = is_array(array_values($match)[0]) ? array_values(array_values($match)[0])[0] : array_values($match)[0];
if (strpos($message, 'twitter')) {
$statusTwitter = new StatusTwitter($url);
$nid = $statusTwitter
->sendRequest();
}
else {
if (strpos($message, 'youtube') || strpos($message, 'youtu.be')) {
$statusYoutube = new StatusYoutube($url, $message);
$nid = $statusYoutube
->generateNode();
}
else {
if ($url !== null) {
$statusHeartPost = new StatusHeartPost($url, $message);
$nid = $statusHeartPost
->sendRequest();
}
}
}
}
if (null === $nid && !empty($this->statusTypeService)) {
$sTypes = $this->statusTypeService
->loadAll();
foreach ($this->statusTypeService
->loadAll() as $type) {
$userViewed = \Drupal::routeMatch()
->getParameters()
->get('user') === NULL ? \Drupal::currentUser()
->id() : \Drupal::routeMatch()
->getParameters()
->get('user')
->id();
if ($userViewed !== NULL) {
$statusEntity = Status::create([
'type' => $type
->id(),
'uid' => \Drupal::currentUser()
->id(),
'recipient' => $userViewed,
'format' => 'basic_html',
]);
StatusHeartPost::parseHashtags($message);
if ($type
->getMedia() && $file !== null) {
$statusEntity
->set('field_image', array_values($file)[0]);
}
$statusEntity
->setMessage($message);
}
if (!empty($statusEntity) && $statusEntity
->save()) {
$statusCreated = TRUE;
}
}
}
if (\Drupal::service('module_handler')
->moduleExists('heartbeat') && ($nid !== NULL || $statusEntity !== NULL)) {
$feedConfig = \Drupal::config('heartbeat_feed.settings');
$response = new AjaxResponse();
$response
->addCommand(new PrependCommand('.heartbeat-stream', HeartbeatBlock::renderOneHeartbeat(\Drupal::service('heartbeat')
->loadByUid(\Drupal::currentUser()
->id()))));
$response
->addCommand(new ClearPreviewCommand(true));
return $response;
}
}
return null;
}