forked from linnovate/openideal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from istolar/OI-40-voting-api
OI-40 Voting api
- Loading branch information
Showing
10 changed files
with
209 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
anonymous_window: 86400 | ||
anonymous_window: 604800 | ||
user_window: -1 | ||
calculation_schedule: immediate | ||
delete_everywhere: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
.votingapi-widgets.openideal-idea-useful .openideal-useful-rating a { | ||
.votingapi-widgets.like .like-rating a { | ||
border: none !important; | ||
margin-right: .5em; | ||
display: inline-block; | ||
} | ||
|
||
.votingapi-widgets.openideal-idea-useful .openideal-useful-rating a.disabled { | ||
.votingapi-widgets.like .like-rating a.disabled { | ||
opacity: 0.5; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Installation functions for Openideal Idea module. | ||
*/ | ||
|
||
/** | ||
* Implements hook_install(). | ||
*/ | ||
function openideal_idea_install() { | ||
// Assign a fairly low weight to ensure our implementation of | ||
// hook_entity_type_build() is run among the last ones. | ||
module_set_weight('openideal_idea', 10); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
modules/openideal_idea/src/Form/OpenidealBaseRatingForm.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace Drupal\openideal_idea\Form; | ||
|
||
use Drupal\Component\Datetime\TimeInterface; | ||
use Drupal\Core\Entity\EntityRepositoryInterface; | ||
use Drupal\Core\Entity\EntityTypeBundleInfoInterface; | ||
use Drupal\Core\Form\FormStateInterface; | ||
use Drupal\votingapi_widgets\Form\BaseRatingForm; | ||
use Drupal\votingapi\VoteResultFunctionManager; | ||
|
||
/** | ||
* Form controller for OpenidealBaseRatingForm class. | ||
* | ||
* Extend the votingapi_widgets module base form | ||
* and add the ability to cancel/delete the vote, | ||
* in like widget. | ||
* | ||
* @Todo: remove once the ability to cancel/delete vote will be added. | ||
* https://www.drupal.org/project/votingapi_widgets/issues/3051783 | ||
* https://www.drupal.org/project/votingapi_widgets/issues/2831545 | ||
*/ | ||
class OpenidealBaseRatingForm extends BaseRatingForm { | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function __construct(VoteResultFunctionManager $votingapi_result, EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) { | ||
parent::__construct($votingapi_result, $entity_repository); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm(array $form, FormStateInterface $form_state) { | ||
// Add the ability to delete vote. | ||
$form = parent::buildForm($form, $form_state); | ||
|
||
$form['delete'] = [ | ||
'#type' => 'submit', | ||
'#value' => $this->t('delete'), | ||
'#attributes' => ['class' => ['openideal-votes-like-delete', 'hidden']], | ||
'#ajax' => [ | ||
'callback' => [$this, 'ajaxSubmitDelete'], | ||
'event' => 'click', | ||
'wrapper' => $form['#attributes']['id'], | ||
'progress' => [ | ||
'type' => NULL, | ||
], | ||
], | ||
]; | ||
$form['submit']['#attributes']['class'][] = 'openideal-votes-like-submit'; | ||
$form['#attached']['drupalSettings']['openidealUser']['voted'] = !$this->getEntity()->isNew(); | ||
|
||
return $form; | ||
} | ||
|
||
/** | ||
* Ajax deletion submit handler. | ||
*/ | ||
public function ajaxSubmitDelete(array $form, FormStateInterface $form_state) { | ||
// Delete the vote and recalcute results via voting plugin manager. | ||
$entity = $this->getEntity(); | ||
$entity->delete(); | ||
$this->votingapiResult->recalculateResults($entity->getVotedEntityType(), $entity->getVotedEntityId(), $entity->getEntityTypeId()); | ||
|
||
// Values taken from parent::ajaxSubmit with appropriate adjustments. | ||
$settings = $form_state->get('settings'); | ||
$result_function = $this->getResultFunction($form_state); | ||
$plugin = $form_state->get('plugin'); | ||
$result_value = $this->getResults($result_function, TRUE); | ||
|
||
$form['value']['#attributes']['data-show-own-vote'] = 'true'; | ||
$form['value']['#default_value'] = 0; | ||
|
||
if ($settings['show_own_vote'] == '0') { | ||
$form['value']['#attributes']['data-show-own-vote'] = 'false'; | ||
$form['value']['#default_value'] = $result_value; | ||
} | ||
|
||
$form['value']['#attributes']['data-vote-value'] = 0; | ||
$form['value']['#attributes']['data-result-value'] = $result_value; | ||
if ($settings['show_results'] == '1') { | ||
$form['result']['#children']['result'] = $plugin->getVoteSummary($entity); | ||
} | ||
|
||
$form['#attached']['drupalSettings']['openidealUser']['voted'] = FALSE; | ||
|
||
return $form; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function ajaxSubmit(array $form, FormStateInterface $form_state) { | ||
$form = parent::ajaxSubmit($form, $form_state); | ||
// The validation provided by module not working because value in | ||
// settings is integer and compared that value with string. Comparison | ||
// executes with strict comparison - "===". | ||
if ($form_state->get('settings')['show_results'] == '1') { | ||
$form['result']['#children']['result'] = $form_state->get('plugin')->getVoteSummary($this->getEntity()); | ||
} | ||
|
||
$form['#attached']['drupalSettings']['openidealUser']['voted'] = TRUE; | ||
|
||
return $form; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
modules/openideal_idea/src/Plugin/votingapi_widget/OpenidealLikeWidget.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Drupal\openideal_idea\Plugin\votingapi_widget; | ||
|
||
use Drupal\votingapi_widgets\Plugin\votingapi_widget\LikeWidget; | ||
|
||
/** | ||
* Openideal Useful Widget. | ||
* | ||
* @VotingApiWidget( | ||
* id = "openideal_useful", | ||
* label = @Translation("Openideal Like"), | ||
* values = { | ||
* 1 = @Translation("Opdenideal Like"), | ||
* }, | ||
* ) | ||
*/ | ||
class OpenidealLikeWidget extends LikeWidget { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, $settings) { | ||
$form = parent::buildForm($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, $settings); | ||
$form['#attached']['library'] = ['openideal_idea/useful']; | ||
return $form; | ||
} | ||
|
||
} |
Oops, something went wrong.