View source
<?php
namespace Drupal\private_message\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Component\Utility\Html;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\private_message\Ajax\PrivateMessageInboxTriggerUpdateCommand;
use Drupal\private_message\Ajax\PrivateMessageLoadNewMessagesCommand;
use Drupal\private_message\Entity\PrivateMessageThread;
use Drupal\private_message\Entity\PrivateMessageThreadInterface;
use Drupal\private_message\Service\PrivateMessageServiceInterface;
use Drupal\private_message\Service\PrivateMessageThreadManagerInterface;
use Drupal\user\UserDataInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PrivateMessageForm extends ContentEntityForm {
protected $formId;
protected $currentUser;
protected $entityTypeManager;
protected $typedDataManager;
protected $userData;
protected $config;
protected $privateMessageService;
protected $privateMessageThreadManager;
protected $userManager;
public function __construct(EntityManagerInterface $entityManager, AccountProxyInterface $currentUser, EntityTypeManagerInterface $entityTypeManager, TypedDataManagerInterface $typedDataManager, UserDataInterface $userData, ConfigFactoryInterface $configFactory, PrivateMessageServiceInterface $privateMessageService, PrivateMessageThreadManagerInterface $privateMessageThreadManager) {
parent::__construct($entityManager);
$this->currentUser = $currentUser;
$this->entityTypeManager = $entityTypeManager;
$this->typedDataManager = $typedDataManager;
$this->userData = $userData;
$this->config = $configFactory
->get('private_message.settings');
$this->privateMessageService = $privateMessageService;
$this->privateMessageThreadManager = $privateMessageThreadManager;
$this->userManager = $entityManager
->getStorage('user');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager'), $container
->get('current_user'), $container
->get('entity_type.manager'), $container
->get('typed_data_manager'), $container
->get('user.data'), $container
->get('config.factory'), $container
->get('private_message.service'), $container
->get('private_message.thread_manager'));
}
public function setFormId($id) {
$this->formId = Html::escape($id);
}
public function getFormId() {
$form_id = parent::getFormId();
if ($this->formId) {
$form_id .= '-' . $this->formId;
}
return $form_id;
}
public function buildForm(array $form, FormStateInterface $form_state, PrivateMessageThreadInterface $private_message_thread = NULL) {
$form = parent::buildForm($form, $form_state);
if ($private_message_thread) {
$form_state
->set('thread_members', $private_message_thread
->getMembers());
$form['actions']['submit']['#ajax'] = [
'callback' => '::ajaxCallback',
];
$form['#attached']['library'][] = 'private_message/message_form';
$form['message']['widget'][0]['#attributes']['autofocus'] = 'autofocus';
}
else {
$private_message_thread = PrivateMessageThread::create();
$form_copy = $form;
$form_state_copy = clone $form_state;
$form_display = EntityFormDisplay::collectRenderDisplay($private_message_thread, 'default');
$form_display
->buildForm($private_message_thread, $form_copy, $form_state_copy);
$form['members'] = $form_copy['members'];
$form['#validate'][] = '::validateMembers';
}
if ($this->config
->get('hide_form_filter_tips')) {
$form['#after_build'][] = '::afterBuild';
}
return $form;
}
public function validateMembers(array &$form, FormStateInterface $form_state) {
$entity_type = $this->entityTypeManager
->getDefinition('private_message_thread');
$field_definitions = PrivateMessageThread::baseFieldDefinitions($entity_type);
$members_field = $field_definitions['members'];
$members = [];
foreach ($form_state
->getValue('members') as $info) {
if (is_array($info) && is_numeric($info[0]['target_id'])) {
$user = $this->userManager
->load($info[0]['target_id']);
if ($user) {
$members[] = $user;
}
}
}
$typed_data = $this->typedDataManager
->create($members_field, $members);
$violations = $typed_data
->validate();
if ($violations
->count() > 0) {
foreach ($violations as $violation) {
$form_state
->setError($form['members'], $violation
->getMessage());
}
}
}
public function ajaxCallback(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$form['message']['widget'][0]['value']['#value'] = '';
$response
->addCommand(new ReplaceCommand(NULL, $form));
$response
->addCommand(new PrivateMessageLoadNewMessagesCommand());
$response
->addCommand(new PrivateMessageInboxTriggerUpdateCommand());
return $response;
}
public function afterBuild(array $form, FormStateInterface $form_state) {
$form['message']['widget'][0]['format']['#access'] = FALSE;
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$status = parent::save($form, $form_state);
$members = $form_state
->get('thread_members');
if (!$members) {
$current_user = $this->userManager
->load($this->currentUser
->id());
$members = [
$current_user,
];
foreach ($form_state
->getValue('members') as $info) {
$user = $this->userManager
->load($info['target_id']);
if ($user) {
$members[] = $user;
}
}
}
$private_message_thread = $this->privateMessageService
->getThreadForMembers($members);
$this->privateMessageThreadManager
->saveThread($this->entity, $members, [], $private_message_thread);
$form_state
->setRedirect('entity.private_message_thread.canonical', [
'private_message_thread' => $private_message_thread
->id(),
]);
return $status;
}
}