You are here

abstract class VoteUpDownWidgetBase in Vote Up/Down 8

Defines a plugin base implementation that corresponding plugins will extend.

@todo Inject used classes.

Hierarchy

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

... See full list

File

src/Plugin/VoteUpDownWidgetBase.php, line 14

Namespace

Drupal\vud\Plugin
View 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

Namesort descending Modifiers Type Description Overrides
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
VoteUpDownWidgetBase::alterTemplateVariables public function Array containing all info for a plugin instance Overrides VoteUpDownWidgetInterface::alterTemplateVariables 1
VoteUpDownWidgetBase::build public function @fixme Do not use camelcase on variables, and fix coding standards. Overrides VoteUpDownWidgetInterface::build
VoteUpDownWidgetBase::getWidgetId public function Returns the label of the specific plugin instance Overrides VoteUpDownWidgetInterface::getWidgetId
VoteUpDownWidgetBase::getWidgetTemplate public function Returns the widget template for a specific plugin instance Overrides VoteUpDownWidgetInterface::getWidgetTemplate
VoteUpDownWidgetBase::getWidgetTemplateVars public function Returns the path to the widget template to be used. Overrides VoteUpDownWidgetInterface::getWidgetTemplateVars