abstract class VoteUpDownWidgetBase in Vote Up/Down 8
Defines a plugin base implementation that corresponding plugins will extend.
@todo Inject used classes.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\vud\Plugin\VoteUpDownWidgetBase implements VoteUpDownWidgetInterface uses StringTranslationTrait
Expanded class hierarchy of VoteUpDownWidgetBase
6 files declare their use of VoteUpDownWidgetBase
- Alternate.php in src/
Plugin/ VoteUpDownWidget/ Alternate.php - Plain.php in src/
Plugin/ VoteUpDownWidget/ Plain.php - Thumbs.php in src/
Plugin/ VoteUpDownWidget/ Thumbs.php - UpAndDown.php in src/
Plugin/ VoteUpDownWidget/ UpAndDown.php - UpDown.php in src/
Plugin/ VoteUpDownWidget/ UpDown.php
File
- src/
Plugin/ VoteUpDownWidgetBase.php, line 14
Namespace
Drupal\vud\PluginView source
abstract class VoteUpDownWidgetBase extends PluginBase implements VoteUpDownWidgetInterface {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function getWidgetId() {
return $this
->getPluginDefinition()['id'];
}
/**
* {@inheritdoc}
*/
public function getWidgetTemplate() {
return $this
->getPluginDefinition()['widget_template'];
}
/**
* {@inheritdoc}
*/
public function alterTemplateVariables(&$variables) {
// Nothing by default.
}
/**
* {@inheritdoc}
*/
public function getWidgetTemplateVars($base_path, &$variables) {
$variables['#template_path'] = $base_path . '/widgets/' . $this
->getWidgetId() . '/widget.html.twig';
array_push($variables['#attached']['library'], 'vud/' . $this
->getWidgetId());
return $variables;
}
/**
* {@inheritdoc}
*
* @fixme Do not use camelcase on variables, and fix coding standards.
*/
public function build($entity) {
$vote_storage = \Drupal::service('entity.manager')
->getStorage('vote');
$currentUser = \Drupal::currentUser();
$entityTypeId = $entity
->getEntityTypeId();
$entityId = $entity
->id();
$module_handler = \Drupal::service('module_handler');
$module_path = $module_handler
->getModule('vud')
->getPath();
// @todo: Implement voting API result functions instead of custom queries.
$up_points = \Drupal::entityQuery('vote')
->condition('value', 1)
->condition('entity_type', $entityTypeId)
->condition('entity_id', $entityId)
->count()
->execute();
$down_points = \Drupal::entityQuery('vote')
->condition('value', -1)
->condition('entity_type', $entityTypeId)
->condition('entity_id', $entityId)
->count()
->execute();
$points = $up_points - $down_points;
$unsigned_points = $up_points + $down_points;
$widget_name = $this
->getWidgetId();
$variables = [
'#theme' => 'vud_widget',
'#widget_template' => $widget_name,
'#entity_id' => $entityId,
'#entity_type_id' => $entityTypeId,
'#base_path' => $module_path,
'#widget_name' => $widget_name,
'#up_points' => $up_points,
'#down_points' => $down_points,
'#points' => $points,
'#unsigned_points' => $unsigned_points,
'#vote_label' => 'votes',
'#widget_instance_id' => "vud-widget-{$entityTypeId}-{$entityId}",
'#attached' => [
'library' => [
'vud/common',
],
],
];
$this
->getWidgetTemplateVars($module_path, $variables);
$up_access = $down_access = $reset_access = FALSE;
if (vud_can_vote($currentUser)) {
$variables['#show_links'] = TRUE;
$variables['#link_class_up'] = 'vud-link-up';
$variables['#link_class_down'] = 'vud-link-down';
$variables['#class_up'] = 'up';
$variables['#class_down'] = 'down';
$vote_type = \Drupal::config('vud.settings')
->get('tag', 'vote');
$user_votes_current_entity = $vote_storage
->getUserVotes($currentUser
->id(), $vote_type, $entityTypeId, $entityId);
if ($user_votes_current_entity != NULL) {
$user_vote_id = (int) array_values($user_votes_current_entity)[0];
$user_vote = $vote_storage
->load($user_vote_id)
->getValue();
}
else {
$user_vote = 0;
}
$up_access = $user_vote <= 0;
$down_access = $user_vote >= 0;
$reset_access = $user_vote != 0 && $currentUser
->hasPermission("reset vote up/down votes");
}
else {
$variables['#class_up'] = '';
$variables['#class_down'] = '';
}
if ($up_access) {
$variables['#show_up_as_link'] = TRUE;
$variables['#link_up'] = Url::fromRoute('vud.vote', [
'entity_type_id' => $entityTypeId,
'entity_id' => $entityId,
'vote_value' => 1,
'widget_name' => $widget_name,
'js' => 'nojs',
]);
$variables['#class_up'] .= ' active';
}
else {
$variables['#class_up'] .= ' inactive';
}
if ($down_access) {
$variables['#show_down_as_link'] = TRUE;
$variables['#link_down'] = Url::fromRoute('vud.vote', [
'entity_type_id' => $entityTypeId,
'entity_id' => $entityId,
'vote_value' => -1,
'widget_name' => $widget_name,
'js' => 'nojs',
]);
$variables['#class_down'] .= ' active';
}
else {
$variables['#class_down'] .= ' inactive';
}
if ($reset_access) {
$variables['#show_reset'] = TRUE;
$variables['#link_reset'] = Url::fromRoute('vud.reset', [
'entity_type_id' => $entityTypeId,
'entity_id' => $entityId,
'widget_name' => $widget_name,
'js' => 'nojs',
]);
$variables += [
'#reset_long_text' => $this
->t('Reset your vote'),
'#reset_short_text' => $this
->t('(reset)'),
'#link_class_reset' => 'reset',
];
}
// Let widgets change variables at the end.
$this
->alterTemplateVariables($variables);
return $variables;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
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. | |
VoteUpDownWidgetBase:: |
public | function |
Array containing all info for a plugin instance Overrides VoteUpDownWidgetInterface:: |
1 |
VoteUpDownWidgetBase:: |
public | function |
@fixme Do not use camelcase on variables, and fix coding standards. Overrides VoteUpDownWidgetInterface:: |
|
VoteUpDownWidgetBase:: |
public | function |
Returns the label of the specific plugin instance Overrides VoteUpDownWidgetInterface:: |
|
VoteUpDownWidgetBase:: |
public | function |
Returns the widget template for a specific plugin instance Overrides VoteUpDownWidgetInterface:: |
|
VoteUpDownWidgetBase:: |
public | function |
Returns the path to the widget template to be used. Overrides VoteUpDownWidgetInterface:: |