class UserPointsTransactor in User Points 8
Transactor for user points type transactions.
Plugin annotation
@Transactor(
id = "userpoints",
title = @Translation("User points"),
description = @Translation("Transactor for user points system."),
supported_entity_types = {
"user",
},
transaction_fields = {
{
"name" = "amount",
"type" = "decimal",
"title" = @Translation("Amount"),
"description" = @Translation("A numeric field with the amount of points."),
"required" = TRUE,
"list" = TRUE,
},
{
"name" = "balance",
"type" = "decimal",
"title" = @Translation("Balance"),
"description" = @Translation("A numeric field to store the current points balance."),
"required" = TRUE,
"list" = TRUE,
},
{
"name" = "log_message",
"type" = "string",
"title" = @Translation("Log message"),
"description" = @Translation("A log message with details about the transaction."),
"required" = FALSE,
},
},
target_entity_fields = {
{
"name" = "last_transaction",
"type" = "entity_reference",
"title" = @Translation("Last transaction"),
"description" = @Translation("A reference field in the user entity type to update with a reference to the last executed transaction of this type."),
"required" = FALSE,
},
{
"name" = "target_balance",
"type" = "decimal",
"title" = @Translation("Balance"),
"description" = @Translation("A numeric field in the user entity to update with the current points balance."),
"required" = FALSE,
},
},
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\transaction\TransactorBase implements TransactorPluginInterface uses StringTranslationTrait
- class \Drupal\transaction\Plugin\Transaction\GenericTransactor
- class \Drupal\transaction\Plugin\Transaction\BalanceTransactor
- class \Drupal\userpoints\Plugin\Transaction\UserPointsTransactor
- class \Drupal\transaction\Plugin\Transaction\BalanceTransactor
- class \Drupal\transaction\Plugin\Transaction\GenericTransactor
- class \Drupal\transaction\TransactorBase implements TransactorPluginInterface uses StringTranslationTrait
Expanded class hierarchy of UserPointsTransactor
File
- src/
Plugin/ Transaction/ UserPointsTransactor.php, line 66
Namespace
Drupal\userpoints\Plugin\TransactionView source
class UserPointsTransactor extends BalanceTransactor {
/**
* {@inheritdoc}
*/
public function validateTransaction(TransactionInterface $transaction) {
if (parent::validateTransaction($transaction)) {
// @todo check required fields and values
return TRUE;
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getTransactionDescription(TransactionInterface $transaction, $langcode = NULL) {
$settings = $transaction
->getType()
->getPluginSettings();
// Transaction amount.
$amount = $transaction
->get($settings['amount'])->value;
$t_options = $langcode ? [
'langcode' => $langcode,
] : [];
$t_args = [
'@status' => $transaction
->isPending() ? $this
->t('(pending)') : '',
];
if ($amount > 0) {
$description = $this
->t('Points credit @status', $t_args, $t_options);
}
elseif ($amount < 0) {
$description = $this
->t('Points debit @status', $t_args, $t_options);
}
else {
$description = $this
->t('Zero points transaction @status', $t_args, $t_options);
}
return $description;
}
/**
* {@inheritdoc}
*/
public function getExecutionIndications(TransactionInterface $transaction, $langcode = NULL) {
$settings = $transaction
->getType()
->getPluginSettings();
// Transaction amount.
$amount = $transaction
->get($settings['amount'])->value;
// @todo pretty print of amount according to default display settings
$t_args = [
'@amount' => abs($transaction
->get($settings['amount'])->value),
];
$t_options = $langcode ? [
'langcode' => $langcode,
] : [];
if ($amount > 0) {
$indication = $this
->t('The user will gain @amount points.', $t_args, $t_options);
}
elseif ($amount < 0) {
$indication = $this
->t('The user will loss @amount points.', $t_args, $t_options);
}
else {
$indication = $this
->t('The current user points balance will not be altered.', [], $t_options);
}
return $indication;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
/** @var \Drupal\transaction\TransactionTypeInterface $transaction_type */
$transaction_type = $form_state
->getFormObject()
->getEntity();
$transactor_settings = $transaction_type
->getPluginSettings();
// Applicable roles.
$roles = [];
foreach (Role::loadMultiple() as $role_id => $role_entity) {
if (!in_array($role_id, [
RoleInterface::ANONYMOUS_ID,
RoleInterface::AUTHENTICATED_ID,
])) {
$roles[$role_id] = $role_entity
->label();
}
}
asort($roles);
if (!count($roles)) {
$roles = [
'' => $this
->t('- None -'),
];
}
$form['roles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Applicable user roles'),
'#description' => $this
->t('The user roles to which this type of points is applicable. Leave empty to apply to any existing role.'),
'#options' => $roles,
'#default_value' => isset($transactor_settings['roles']) ? explode(',', $transactor_settings['roles']) : [
'',
],
];
return parent::buildConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
// Add roles to plugin settings settings.
$roles = [];
foreach ($form_state
->getValue('roles') as $role) {
if (!empty($role)) {
$roles[] = $role;
}
}
/** @var \Drupal\transaction\TransactionTypeInterface $transaction_type */
$transaction_type = $form_state
->getFormObject()
->getEntity();
$settings = $transaction_type
->getPluginSettings();
$settings['roles'] = implode(',', $roles);
$transaction_type
->setPluginSettings($settings);
return parent::submitConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function isApplicable(ContentEntityInterface $entity, TransactionTypeInterface $transaction_type = NULL) {
/** @var \Drupal\user\UserInterface $entity */
if (parent::isApplicable($entity)) {
if ($transaction_type) {
$settings = $transaction_type
->getPluginSettings();
// Apply to any user when no roles in settings.
return empty($settings['roles']) || !empty(array_intersect($entity
->getRoles(TRUE), explode(',', $settings['roles'])));
}
}
return FALSE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BalanceTransactor:: |
public | function |
Executes a transaction. Overrides GenericTransactor:: |
|
GenericTransactor:: |
public | function |
Compose human readable details for the given transaction. Overrides TransactorBase:: |
|
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. | |
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. | |
TransactorBase:: |
protected | property | The current user. | |
TransactorBase:: |
protected | property | The field manager. | |
TransactorBase:: |
protected | property | Prefix for new field creation. | |
TransactorBase:: |
protected | property | The transaction service. | |
TransactorBase:: |
protected | property | The transaction entity storage. | |
TransactorBase:: |
protected | function | Build configuration form fields to the target entity. | |
TransactorBase:: |
protected | function | Build configuration form fields to the transaction. | |
TransactorBase:: |
protected | function | Build transaction options configuration form. | |
TransactorBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
TransactorBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
TransactorBase:: |
protected | function | Creates a field config. | |
TransactorBase:: |
protected | function | Creates a new field. | |
TransactorBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
|
TransactorBase:: |
public static | function | Machine name exists callback for "inline" field creation. | |
TransactorBase:: |
protected | function | Builds a form field to reference a field. | |
TransactorBase:: |
protected | function | Search for fields of a given type in a given entity type. | |
TransactorBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
TransactorBase:: |
public | function |
Compose a message that describes the execution result of a transaction. Overrides TransactorPluginInterface:: |
|
TransactorBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
TransactorBase:: |
protected | function | Enable the display of a field. | |
TransactorBase:: |
public | function |
Handles the validation for the transactor plugin settings form. Overrides PluginFormInterface:: |
|
TransactorBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
TransactorPluginInterface:: |
constant | Generic result code for failed execution. | ||
TransactorPluginInterface:: |
constant | Generic result code for successful execution. | ||
UserPointsTransactor:: |
public | function |
Provides a form for this transactor plugin settings. Overrides TransactorBase:: |
|
UserPointsTransactor:: |
public | function |
Compose a messsage with execution indications for the given transaction. Overrides BalanceTransactor:: |
|
UserPointsTransactor:: |
public | function |
Compose a human readable description for the given transaction. Overrides BalanceTransactor:: |
|
UserPointsTransactor:: |
public | function |
Check if the transactor is applicable to a particular entity. Overrides TransactorBase:: |
|
UserPointsTransactor:: |
public | function |
Handles the settings form submit for this transactor plugin. Overrides TransactorBase:: |
|
UserPointsTransactor:: |
public | function |