class PrivateMessageThreadMessageFormatter in Private Message 8
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldFormatter/PrivateMessageThreadMessageFormatter.php \Drupal\private_message\Plugin\Field\FieldFormatter\PrivateMessageThreadMessageFormatter
Defines the private message thread message field formatter.
Plugin annotation
@FieldFormatter(
id = "private_message_thread_message_formatter",
label = @Translation("Private Message Messages"),
field_types = {
"entity_reference"
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Field\FormatterBase implements FormatterInterface, ContainerFactoryPluginInterface
- class \Drupal\private_message\Plugin\Field\FieldFormatter\PrivateMessageThreadMessageFormatter implements ContainerFactoryPluginInterface
- class \Drupal\Core\Field\FormatterBase implements FormatterInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of PrivateMessageThreadMessageFormatter
File
- src/
Plugin/ Field/ FieldFormatter/ PrivateMessageThreadMessageFormatter.php, line 27
Namespace
Drupal\private_message\Plugin\Field\FieldFormatterView source
class PrivateMessageThreadMessageFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
/**
* The entity manager service.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* The CSRF token generator.
*
* @var \Drupal\Core\Access\CsrfTokenGenerator
*/
protected $csrfTokenGenerator;
/**
* The user manager service.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userManager;
/**
* Construct a PrivateMessageThreadFormatter object.
*
* @param string $plugin_id
* The ID of the plugin.
* @param mixed $plugin_definition
* The plugin definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field definition.
* @param array $settings
* The field settings.
* @param mixed $label
* The label of the field.
* @param string $view_mode
* The current view mode.
* @param array $third_party_settings
* The third party settings.
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* The entity manager service.
* @param |Drupal\Core\Session\AccountProxyInterface $currentUser
* The current user.
* @param \Drupal\Core\Access\CsrfTokenGenerator $csrfTokenGenerator
* The CSRF token generator.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityManagerInterface $entityManager, AccountProxyInterface $currentUser, CsrfTokenGenerator $csrfTokenGenerator) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->entityManager = $entityManager;
$this->currentUser = $currentUser;
$this->csrfTokenGenerator = $csrfTokenGenerator;
$this->userManager = $entityManager
->getStorage('user');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('entity.manager'), $container
->get('current_user'), $container
->get('csrf_token'));
}
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return $field_definition
->getFieldStorageDefinition()
->getTargetEntityTypeId() == 'private_message_thread' && $field_definition
->getFieldStorageDefinition()
->getSetting('target_type') == 'private_message';
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'message_count' => 5,
'ajax_previous_load_count' => 5,
'message_order' => 'asc',
'ajax_refresh_rate' => 20,
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$settings = $this
->getSettings();
$summary[] = $this
->t('Number of threads to show on load: @count', [
'@count' => $settings['message_count'],
]);
$summary[] = $this
->t('Number of threads to show when clicking load previous: @count', [
'@count' => $settings['ajax_previous_load_count'],
]);
$summary[] = $this
->t('Order of messages: @order', [
'@order' => $this
->translateKey('order', $settings['message_order']),
]);
if ($settings['ajax_refresh_rate']) {
$summary[] = $this
->t('Ajax refresh rate: @count seconds', [
'@count' => $settings['ajax_refresh_rate'],
]);
}
else {
$summary[] = $this
->t('Ajax refresh rate: Ajax refresh disabled');
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['message_count'] = [
'#title' => $this
->t('Message Count'),
'#type' => 'number',
'#default_value' => $this
->getSetting('message_count'),
'#description' => $this
->t('The number of messages to display on load'),
];
$element['ajax_previous_load_count'] = [
'#title' => $this
->t('Load Previous Ajax Count'),
'#type' => 'number',
'#default_value' => $this
->getSetting('ajax_previous_load_count'),
'#description' => $this
->t('The number of previous messages to load using ajax when clicking the load previous link'),
];
$element['ajax_refresh_rate'] = [
'#title' => $this
->t('Ajax refresh rate'),
'#type' => 'number',
'#default_value' => $this
->getSetting('ajax_refresh_rate'),
'#description' => $this
->t('The number of seconds between checks for new messages. Set to zero to disable. Note that a lower number will cause more requests, use more bandwidth, and cause more strain on the server. As such, it is not recommended to set a value lower than five (5) seconds.'),
];
$element['message_order'] = [
'#type' => 'radios',
'#title' => $this
->t('Message direction'),
'#options' => [
'asc' => $this
->translateKey('order', 'asc'),
'desc' => $this
->translateKey('order', 'desc'),
],
'#description' => $this
->t('Whether to show messages first to last, or last to first'),
'#default_value' => $this
->getSetting('message_order'),
];
return $element;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
$private_message_thread = $items
->getEntity();
$element = [
'#prefix' => '<div class="private-message-thread-messages">',
'#suffix' => '</div>',
];
$view_builder = $this->entityManager
->getViewBuilder('private_message');
$user = $this->userManager
->load($this->currentUser
->id());
$messages = $private_message_thread
->filterUserDeletedMessages($user);
$messages = array_slice($messages, -1 * $this
->getSetting('message_count'));
foreach ($messages as $message) {
$element[$message
->id()] = $view_builder
->view($message, 'full');
}
if ($this
->getSetting('message_order') == 'desc') {
$element = array_reverse($element);
}
$new_url = Url::fromRoute('private_message.ajax_callback', [
'op' => 'get_new_messages',
]);
$token = $this->csrfTokenGenerator
->get($new_url
->getInternalPath());
$new_url
->setOptions([
'absolute' => TRUE,
'query' => [
'token' => $token,
],
]);
$prev_url = Url::fromRoute('private_message.ajax_callback', [
'op' => 'get_old_messages',
]);
$token = $this->csrfTokenGenerator
->get($prev_url
->getInternalPath());
$prev_url
->setOptions([
'absolute' => TRUE,
'query' => [
'token' => $token,
],
]);
$load_url = Url::fromRoute('private_message.ajax_callback', [
'op' => 'load_thread',
]);
$load_token = $this->csrfTokenGenerator
->get($load_url
->getInternalPath());
$load_url
->setOptions([
'absolute' => TRUE,
'query' => [
'token' => $load_token,
],
]);
$element['#attached']['drupalSettings']['privateMessageThread'] = [
'newMessageCheckUrl' => $new_url
->toString(),
'previousMessageCheckUrl' => $prev_url
->toString(),
'messageOrder' => $this
->getSetting('message_order'),
'refreshRate' => $this
->getSetting('ajax_refresh_rate') * 1000,
'loadThreadUrl' => $load_url
->toString(),
];
$element['#attached']['library'][] = 'private_message/private_message_thread';
return $element;
}
/**
* Translates a given key.
*
* @param string $type
* The type of string being translated.
* @param string $value
* The value to be translated.
*
* @return mixed
* - If a translated value exists for the given type/value combination, a
* \Drupal\Core\StringTranslation\TranslatableMarkup object containing the
* translated value is returned.
* - If only the type exists, but not the value, the untranslated value as
* a string is returned.
* - If the type does not exist, the untranslated value is returned.
*/
private function translateKey($type, $value) {
if ($type == 'order') {
$keys = [
'asc' => $this
->t('Ascending'),
'desc' => $this
->t('Descending'),
];
return isset($keys[$value]) ? $keys[$value] : $value;
}
return $value;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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 | |
FormatterBase:: |
protected | property | The field definition. | |
FormatterBase:: |
protected | property | The label display setting. | |
FormatterBase:: |
protected | property |
The formatter settings. Overrides PluginSettingsBase:: |
|
FormatterBase:: |
protected | property | The view mode. | |
FormatterBase:: |
protected | function | Returns the value of a field setting. | |
FormatterBase:: |
protected | function | Returns the array of field settings. | |
FormatterBase:: |
public | function |
Allows formatters to load information for field values being displayed. Overrides FormatterInterface:: |
2 |
FormatterBase:: |
public | function |
Builds a renderable array for a fully themed field. Overrides FormatterInterface:: |
1 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginSettingsBase:: |
protected | property | Whether default settings have been merged into the current $settings. | |
PluginSettingsBase:: |
protected | property | The plugin settings injected by third party modules. | |
PluginSettingsBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
6 |
PluginSettingsBase:: |
public | function |
Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
protected | function | Merges default settings values into $settings. | |
PluginSettingsBase:: |
public | function |
Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface:: |
3 |
PluginSettingsBase:: |
public | function |
Sets the value of a setting for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the settings for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Unsets a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PrivateMessageThreadMessageFormatter:: |
protected | property | The CSRF token generator. | |
PrivateMessageThreadMessageFormatter:: |
protected | property | The current user. | |
PrivateMessageThreadMessageFormatter:: |
protected | property | The entity manager service. | |
PrivateMessageThreadMessageFormatter:: |
protected | property | The user manager service. | |
PrivateMessageThreadMessageFormatter:: |
public static | function |
Creates an instance of the plugin. Overrides FormatterBase:: |
|
PrivateMessageThreadMessageFormatter:: |
public static | function |
Defines the default settings for this plugin. Overrides PluginSettingsBase:: |
|
PrivateMessageThreadMessageFormatter:: |
public static | function |
Returns if the formatter can be used for the provided field. Overrides FormatterBase:: |
|
PrivateMessageThreadMessageFormatter:: |
public | function |
Returns a form to configure settings for the formatter. Overrides FormatterBase:: |
|
PrivateMessageThreadMessageFormatter:: |
public | function |
Returns a short summary for the current formatter settings. Overrides FormatterBase:: |
|
PrivateMessageThreadMessageFormatter:: |
private | function | Translates a given key. | |
PrivateMessageThreadMessageFormatter:: |
public | function |
Builds a renderable array for a field value. Overrides FormatterInterface:: |
|
PrivateMessageThreadMessageFormatter:: |
public | function |
Construct a PrivateMessageThreadFormatter object. Overrides FormatterBase:: |
|
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. |