Last active
February 21, 2020 02:15
-
-
Save bookworm2000/cd9806579da354d2dd116a44bb22b04c to your computer and use it in GitHub Desktop.
Example custom module to override votingapi_widgets classes
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
name: My Module | |
type: module | |
description: 'Custom Module to customize use of votingapi web contest.' | |
core: 8.x | |
package: Custom | |
dependencies: | |
- votingapi | |
- votingapi_widgets | |
- webform | |
- webform_content_creator |
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
<?php | |
/** | |
* @file mymodule.module | |
* | |
*/ | |
declare(strict_types = 1); | |
use \Drupal\webform\Entity\WebformSubmission; | |
/** | |
* Implements hook_ENTITY_TYPE_presave(). | |
*/ | |
function mymodule_webform_submission_presave(WebformSubmission $submission) { | |
// Retrieve the user input from the webform submission. | |
$submitted_data = $submission->getData(); | |
$zipcode = $submitted_data['zipcode']; | |
// Retrieve the state name from the database custom table. | |
// This is calling a service included in the mymodule.services.yml file. | |
$state_lookup = \Drupal::service('mymodule.state_lookup'); | |
$state_name = $state_lookup->findState($zipcode); | |
if ($state_name) { | |
// Update the webform submission state field with the state name. | |
$submitted_data['state'] = $state_name; | |
$submission->setData($submitted_data); | |
} | |
} | |
/** | |
* Implements hook_entity_type_build(). | |
*/ | |
function mymodule_entity_type_build(array &$entity_types) { | |
$plugins = \Drupal::service('plugin.manager.voting_api_widget.processor')->getDefinitions(); | |
foreach ($plugins as $plugin_id => $definition) { | |
// Override the votingapi_widgets form class for the custom widgets. | |
if ($plugin_id === 'special') { | |
$entity_types['vote']->setFormClass('votingapi_' . $plugin_id, 'Drupal\mymodule\Form\TenRatingForm'); | |
} | |
} | |
} | |
/** | |
* Implements hook_module_implements_alter(). | |
*/ | |
function mymodule_module_implements_alter(array &$implementations, string $hook) { | |
if ($hook === 'entity_type_build') { | |
$group = $implementations; | |
$group = $implementations['mymodule']; | |
unset($implementations['mymodule']); | |
$implementations['mymodule'] = $group; | |
} | |
} |
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
services: | |
mymodule.state_lookup: | |
class: Drupal\mymodule\StateLookup | |
arguments: ['@database'] |
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
<?php | |
declare(strict_types = 1); | |
namespace Drupal\mymodule\Plugin\votingapi_widget; | |
use Drupal\votingapi_widgets\Plugin\VotingApiWidgetBase; | |
/** | |
* Custom widget for voting. | |
* | |
* @VotingApiWidget( | |
* id = "special", | |
* label = @Translation("Special rating"), | |
* values = { | |
* 1 = @Translation("1"), | |
* 2 = @Translation("2"), | |
* 3 = @Translation("3"), | |
* 4 = @Translation("4"), | |
* 5 = @Translation("5"), | |
* 6 = @Translation("6"), | |
* 7 = @Translation("7"), | |
* 8 = @Translation("8"), | |
* 9 = @Translation("9"), | |
* 10 = @Translation("10"), | |
* }, | |
* ) | |
*/ | |
class SpecialWidget extends VotingApiWidgetBase { | |
/** | |
* Vote form. | |
*/ | |
public function buildForm($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, $style, $show_results, $read_only = FALSE): array { | |
$form = $this->getForm($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, $style, $show_results, $read_only); | |
$build = [ | |
'rating' => [ | |
'#theme' => 'container', | |
'#attributes' => [ | |
'class' => [ | |
'votingapi-widgets', | |
'special', | |
($read_only) ? 'read_only' : '', | |
], | |
], | |
'#children' => [ | |
'form' => $form, | |
], | |
], | |
]; | |
return $build; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getStyles(): array { | |
return [ | |
'default' => t('Default'), | |
'bars-horizontal' => t('Bars horizontal'), | |
'css-stars' => t('Css stars'), | |
'bars-movie' => t('Bars movie'), | |
'bars-pill' => t('Bars pill'), | |
'bars-square' => t('Bars square'), | |
'fontawesome-stars-o' => t('Fontawesome stars-o'), | |
'fontawesome-stars' => t('Fontawesome stars'), | |
'bootstrap-stars' => t('Bootstrap stars'), | |
]; | |
} | |
} |
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
<?php | |
declare(strict_types = 1); | |
namespace Drupal\mymodule; | |
use Drupal\Core\Database\Connection; | |
/** | |
* Contain methods for retrieving database content from custom state_data table. | |
* This is provided for example purposes and cannot be implemented without the table. | |
* This file belongs at the path: mymodule\src\StateLookup.php | |
* | |
* @package Drupal\mymodule | |
*/ | |
class StateLookup { | |
/** | |
* The database connection. | |
* | |
* @var \Drupal\Core\Database\Connection | |
*/ | |
protected $database; | |
/** | |
* Constructor. | |
* | |
* @param \Drupal\Core\Database\Connection $database | |
*/ | |
public function __construct(Connection $database) { | |
$this->database = $database; | |
} | |
/** | |
* Find the state name. | |
* | |
* @param string $zipcode | |
* The zipcode to search for. | |
* | |
* @return string|null | |
* The state name that matches the zipcode or NULL. | |
*/ | |
public function findStateName(string $zipcode): ?string { | |
// Build the query. | |
$query = $this->database->select('zipcode','s'); | |
$query->addField('s', 'sid', 'sid'); | |
$query->addField('s', 'zipcode', 'zipcode'); | |
$query->condition('s.zipcode', $zipcode, '='); | |
$query->join('state_data', 'd', 'd.sid = s.sid'); | |
$query->addField('d', 'state_name', 'state_name'); | |
// Check to see if there are any results before updating the webform submission. | |
if ($state_value = $query->execute()->fetchAll()) { | |
return $state_value[0]->state_name; | |
} | |
else { | |
return NULL; | |
} | |
} | |
} |
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
<?php | |
declare(strict_types = 1); | |
namespace Drupal\mymodule\Form; | |
use Drupal\Core\Entity\ContentEntityForm; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\Component\Utility\Html; | |
use Drupal\votingapi\VoteInterface; | |
use Drupal\votingapi_widgets\Form\BaseRatingForm; | |
use Drupal\Core\Entity\EntityRepositoryInterface; | |
use Drupal\Core\Entity\EntityTypeBundleInfoInterface; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
/** | |
* Form controller override for votingapi_widgets content entity form. | |
* | |
*/ | |
class TenRatingForm extends BaseRatingForm { | |
public $plugin; | |
/** | |
* Constructs a voting form entity. Useful if injecting database lookup services but not included here. | |
* | |
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository | |
* The entity repository service. | |
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info | |
* The entity type bundle service. | |
*/ | |
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL) { | |
parent::__construct($entity_repository, $entity_type_bundle_info); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function create(ContainerInterface $container) { | |
return new static( | |
$container->get('entity.repository'), | |
$container->get('entity_type.bundle.info') | |
); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(array $form, FormStateInterface $form_state): array { | |
$form = parent::buildForm($form, $form_state); | |
$entity = $this->getEntity(); | |
$options = $form_state->get('options'); | |
$form_id = Html::getUniqueId('vote-form'); | |
$plugin = $form_state->get('plugin'); | |
$form['#cache']['contexts'][] = 'user.permissions'; | |
$form['#cache']['contexts'][] = 'user.roles:authenticated'; | |
$form['#attributes']['id'] = $form_id; | |
$form['value'] = [ | |
'#type' => 'select', | |
'#options' => $options, | |
'#attributes' => [ | |
'autocomplete' => 'off', | |
'data-default-value' => 1, | |
'data-style' => ($form_state->get('style')) ? $form_state->get('style') : 'default', | |
], | |
'#default_value' => 1, | |
]; | |
if ($form_state->get('read_only') || !$plugin->canVote($entity)) { | |
$form['value']['#attributes']['disabled'] = 'disabled'; | |
} | |
if ($form_state->get('show_results')) { | |
$form['result'] = [ | |
'#theme' => 'container', | |
'#attributes' => [ | |
'class' => ['vote-result'], | |
], | |
'#children' => [], | |
'#weight' => 100, | |
]; | |
$form['result']['#children']['result'] = $plugin->getVoteSummary($entity); | |
} | |
$form['submit'] = $form['actions']['submit']; | |
$form['actions']['#access'] = FALSE; | |
$form['submit'] += [ | |
'#type' => 'button', | |
'#ajax' => [ | |
'callback' => [$this, 'ajaxSubmit'], | |
'event' => 'click', | |
'wrapper' => $form_id, | |
'progress' => [ | |
'method' => 'replace', | |
], | |
], | |
]; | |
return $form; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment