Problem/Motivation

"dynamic: allows on-the-fly creation of registration codes" - this feature is missing from the 8.x version.

Proposed resolution

Create Event dispatcher for custom validation.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

trigdog created an issue. See original summary.

trigdog’s picture

TR’s picture

Status: Active » Needs review

Moved to NR to trigger testing.

Status: Needs review » Needs work

The last submitted patch, 2: dynamic-regcodes-event-dispatcher_3362786-2.diff, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

trigdog’s picture

Status: Needs work » Needs review
FileSize
4.9 KB

Fixing coding standard issues.

trigdog’s picture

trigdog’s picture

The last submitted patch, 6: dynamic-regcodes-event-dispatcher_3362786-6.diff, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 7: dynamic-regcodes-event-dispatcher_3362786-7.diff, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

trigdog’s picture

Missed a couple. Hopefully this one is good.

trigdog’s picture

Here is an example of how to use this on your site:

In a custom module create a services file (ex. - mymodule.services.yml) file and an event subscriber (ex. - src/Event/MyModuleRegcodeSubscriber.php):

services:
  mymodule.regcode_subscriber:
    class: Drupal\mymodule\Event\MyModuleRegcodeSubscriber
    tags:
      - { name: 'event_subscriber' }
<?php

namespace Drupal\mymodule\Event;

use Drupal\regcode\Event\RegcodeValidateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class MyModuleRegcodeSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   *
   * @return array
   *   The event names to listen for, and the methods that should be executed.
   */
  public static function getSubscribedEvents() {
    return [
      RegcodeValidateEvent::REGCODE_VALIDATE => 'updateRegcode',
    ];
  }

  /**
   * React to a regcode validate event.
   *
   * @param \Drupal\regcode\Event\RegcodeValidateEvent $event
   *   Regcode validate event.
   */
  public function updateRegcode(RegcodeValidateEvent $event) {
    $user_code = $event->getUserEnteredRegcode();
    $db_code = $event->getRegcode();

    // Only react if the code isn't in regcode db table.
    if (!$db_code) {
      // Check if code is valid.
      if ($this->checkRegcode($user_code)) {
        // Create code on the fly.
        $code = new \stdClass();
        $code->code = $user_code;
        $event->setAndSaveAlteredRegcode($code);
      }
    }
  }

  /**
   * Validate the code.
   *
   * @return bool
   *   TRUE or FALSE if the code is valid.
   *
   */
  private function checkRegcode($code) {
    // Put custom validation code here.
  }

}

Status: Needs review » Needs work

The last submitted patch, 10: dynamic-regcodes-event-dispatcher_3362786-10.diff, failed testing. View results