class PrivateMessageForm in Private Message 8
Same name and namespace in other branches
- 8.2 src/Form/PrivateMessageForm.php \Drupal\private_message\Form\PrivateMessageForm
Defines the private message form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\Core\Entity\ContentEntityForm implements ContentEntityFormInterface
- class \Drupal\private_message\Form\PrivateMessageForm
- class \Drupal\Core\Entity\ContentEntityForm implements ContentEntityFormInterface
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of PrivateMessageForm
File
- src/
Form/ PrivateMessageForm.php, line 28
Namespace
Drupal\private_message\FormView source
class PrivateMessageForm extends ContentEntityForm {
/**
* A unique instance identifier for the form.
*
* @var int
*/
protected $formId;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The typed data manager service.
*
* @var \Drupal\Core\TypedData\TypedDataManagerInterface
*/
protected $typedDataManager;
/**
* The user data service.
*
* @var \Drupal\user\UserDataInterface
*/
protected $userData;
/**
* The private message configuration.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* The private message service.
*
* @var \Drupal\private_message\Service\PrivateMessageServiceInterface
*/
protected $privateMessageService;
/**
* The private message thread manager service.
*
* @var \Drupal\private_message\Service\PrivateMessageThreadManagerInterface
*/
protected $privateMessageThreadManager;
/**
* The user manager service.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userManager;
/**
* Constructs a PrivateMessageForm object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* The entity manager service.
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
* The current user.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager service.
* @param \Drupal\Core\TypedData\TypedDataManagerInterface $typedDataManager
* The typed data manager service.
* @param \Drupal\user\UserDataInterface $userData
* The user data service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The configuration factory service.
* @param \Drupal\private_message\Service\PrivateMessageServiceInterface $privateMessageService
* The private message service.
* @param \Drupal\private_message\Service\PrivateMessageThreadManagerInterface $privateMessageThreadManager
* The private message thread manager service.
*/
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');
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* Set the ID of the form.
*
* This allows for the form to be used multiple times on a page.
*
* @param mixed $id
* An ID required to be unique each time the form is called on a page.
*/
public function setFormId($id) {
$this->formId = Html::escape($id);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
$form_id = parent::getFormId();
if ($this->formId) {
$form_id .= '-' . $this->formId;
}
return $form_id;
}
/**
* {@inheritdoc}
*/
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',
];
// Only to do these when using #ajax.
$form['#attached']['library'][] = 'private_message/message_form';
$form['message']['widget'][0]['#attributes']['autofocus'] = 'autofocus';
}
else {
// Create a dummy private message thread form so as to retrieve the
// members element from it.
$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;
}
/**
* Validate members that have been added to a private message thread.
*
* Validates that submitted members have permission to use the Private message
* system. This validation is not added automatically, as the members field is
* not part of the Private Message entity, but rather something that has been
* shoehorned in from the PrivateMessageThread entity, to make for a better
* user experience, by creating a thread and a message in a single form.
*
* @see \Drupal\private_message\Entity\PrivateMessageThead::baseFieldDefinitions
*/
public function validateMembers(array &$form, FormStateInterface $form_state) {
// The members form element was loaded from the PrivateMessageThread entity
// type. As it is not a part of the PrivateMessage entity, for which this
// form is built, the constraints that are a part of the field on the
// Private Message Thread are not applied. As such, the constraints need to
// be checked manually.
// First, get the PrivateMessageThread entity type.
$entity_type = $this->entityTypeManager
->getDefinition('private_message_thread');
// Next, load the field definitions as defined on the entity type.
$field_definitions = PrivateMessageThread::baseFieldDefinitions($entity_type);
// Get the member's field, as this is the field to be validated.
$members_field = $field_definitions['members'];
// Retrieve any members submitted on the form.
$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;
}
}
}
// Get a typed data element that can be used for validation.
$typed_data = $this->typedDataManager
->create($members_field, $members);
// Validate the submitted members.
$violations = $typed_data
->validate();
// Check to see if any contraint violations were found.
if ($violations
->count() > 0) {
// Output any errors for found constraint violations.
foreach ($violations as $violation) {
$form_state
->setError($form['members'], $violation
->getMessage());
}
}
}
/**
* Ajax callback for the PrivateMessageForm.
*/
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;
}
/**
* After build callback for the Private Message Form.
*/
public function afterBuild(array $form, FormStateInterface $form_state) {
$form['message']['widget'][0]['format']['#access'] = FALSE;
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$status = parent::save($form, $form_state);
$members = $form_state
->get('thread_members');
if (!$members) {
// Generate an array containing the members of the thread.
$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;
}
}
}
// Get a private message thread containing the given users.
$private_message_thread = $this->privateMessageService
->getThreadForMembers($members);
// Save the thread.
$this->privateMessageThreadManager
->saveThread($this->entity, $members, [], $private_message_thread);
// Send the user to the private message page. As this thread is the newest,
// it wll be at the top of the list.
$form_state
->setRedirect('entity.private_message_thread.canonical', [
'private_message_thread' => $private_message_thread
->id(),
]);
return $status;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentEntityForm:: |
protected | property |
The entity being used by this form. Overrides EntityForm:: |
9 |
ContentEntityForm:: |
protected | property | The entity repository service. | |
ContentEntityForm:: |
protected | property | The entity type bundle info service. | |
ContentEntityForm:: |
protected | property | The time service. | |
ContentEntityForm:: |
protected | function | Add revision form fields if the entity enabled the UI. | |
ContentEntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityForm:: |
3 |
ContentEntityForm:: |
protected | function |
Copies top-level form values to entity properties Overrides EntityForm:: |
|
ContentEntityForm:: |
protected | function | Flags violations for the current form. | 4 |
ContentEntityForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
13 |
ContentEntityForm:: |
protected | function | Returns the bundle entity of the entity, or NULL if there is none. | |
ContentEntityForm:: |
protected | function | Gets the names of all fields edited in the form. | 4 |
ContentEntityForm:: |
public | function |
Gets the form display. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
public | function |
Gets the code identifying the active form language. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
protected | function | Should new revisions created on default. | |
ContentEntityForm:: |
protected | function |
Initializes the form state and the entity before the first form build. Overrides EntityForm:: |
1 |
ContentEntityForm:: |
protected | function | Initializes form language code values. | |
ContentEntityForm:: |
public | function |
Checks whether the current form language matches the entity one. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
protected | function |
Prepares the entity object before the form is built first. Overrides EntityForm:: |
1 |
ContentEntityForm:: |
public | function |
Sets the form display. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
protected | function | Checks whether the revision form fields should be added to the form. | |
ContentEntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides EntityForm:: |
3 |
ContentEntityForm:: |
public | function | Updates the changed time of the entity. | |
ContentEntityForm:: |
public | function | Updates the form language to reflect any change to the entity language. | |
ContentEntityForm:: |
public | function |
Button-level validation handlers are highly discouraged for entity forms,
as they will prevent entity validation from running. If the entity is going
to be saved during the form submission, this method should be manually
invoked from the button-level… Overrides FormBase:: |
3 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PrivateMessageForm:: |
protected | property | The private message configuration. | |
PrivateMessageForm:: |
protected | property | The current user. | |
PrivateMessageForm:: |
protected | property |
The entity type manager service. Overrides EntityForm:: |
|
PrivateMessageForm:: |
protected | property | A unique instance identifier for the form. | |
PrivateMessageForm:: |
protected | property | The private message service. | |
PrivateMessageForm:: |
protected | property | The private message thread manager service. | |
PrivateMessageForm:: |
protected | property | The typed data manager service. | |
PrivateMessageForm:: |
protected | property | The user data service. | |
PrivateMessageForm:: |
protected | property | The user manager service. | |
PrivateMessageForm:: |
public | function |
After build callback for the Private Message Form. Overrides EntityForm:: |
|
PrivateMessageForm:: |
public | function | Ajax callback for the PrivateMessageForm. | |
PrivateMessageForm:: |
public | function |
Form constructor. Overrides EntityForm:: |
|
PrivateMessageForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ContentEntityForm:: |
|
PrivateMessageForm:: |
public | function |
Returns a unique string identifying the form. Overrides EntityForm:: |
|
PrivateMessageForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
PrivateMessageForm:: |
public | function | Set the ID of the form. | |
PrivateMessageForm:: |
public | function | Validate members that have been added to a private message thread. | |
PrivateMessageForm:: |
public | function |
Constructs a PrivateMessageForm object. Overrides ContentEntityForm:: |
|
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |