Skip to content

Commit

Permalink
Merge pull request #128 from nysenate/feature/aten-nys-73--auto-popul…
Browse files Browse the repository at this point in the history
…ate-address-user-registration

Feature: auto-populate address (from find my senator form) during user registration
  • Loading branch information
kzalewski authored Feb 1, 2024
2 parents 8f33fc1 + 8c78313 commit 5400a7a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
24 changes: 22 additions & 2 deletions web/modules/custom/nys_registration/src/Form/FindMySenatorForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
use Drupal\nys_registration\RegistrationHelper;
Expand Down Expand Up @@ -38,13 +39,26 @@ class FindMySenatorForm extends FormBase {
*/
protected $messenger;

/**
* The tempstore factory.
*
* @var \Drupal\Core\TempStore\PrivateTempStoreFactory
*/
protected $tempStoreFactory;

/**
* Constructor for service injection.
*/
public function __construct(RegistrationHelper $helper, SenatorsHelper $senatorsHelper, MessengerInterface $messenger) {
public function __construct(
RegistrationHelper $helper,
SenatorsHelper $senatorsHelper,
MessengerInterface $messenger,
PrivateTempStoreFactory $tempStoreFactory
) {
$this->helper = $helper;
$this->senatorsHelper = $senatorsHelper;
$this->messenger = $messenger;
$this->tempStoreFactory = $tempStoreFactory;
}

/**
Expand All @@ -55,6 +69,7 @@ public static function create(ContainerInterface $container): self {
$container->get('nys_registration.helper'),
$container->get('nys_senators.senators_helper'),
$container->get('messenger'),
$container->get('tempstore.private')
);
}

Expand Down Expand Up @@ -214,6 +229,7 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
</p>
</div>
</div>
' . $create_message . '
<div class="row c-find-my-senator--row">
<div class="columns large-12">
<h2 class="c-container--title">Senate District Map</h2>
Expand All @@ -222,7 +238,6 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
</iframe>
</div>
</div>
' . $create_message . '
</div>',
[]
);
Expand Down Expand Up @@ -255,6 +270,11 @@ public function getFormId(): string {
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$address = $form_state->getValue('field_address') ?? [];

// Save input address to current session for later reuse.
$tempstore = $this->tempStoreFactory->get('nys_registration');
$tempstore->set('find_my_senator_address', $address);

$district_term = $this->helper->getDistrictFromAddress($address);
$district_num = $district_term ? $district_term->field_district_number->value : 0;
$form_state->set('district_term', $district_term)
Expand Down
42 changes: 34 additions & 8 deletions web/modules/custom/nys_registration/src/Form/RegisterForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
use Drupal\nys_registration\RegistrationHelper;
Expand All @@ -34,27 +35,44 @@ class RegisterForm extends UserRegisterForm {
*/
protected SenatorsHelper $senatorsHelper;

/**
* The tempstore factory.
*
* @var \Drupal\Core\TempStore\PrivateTempStoreFactory
*/
protected $tempStoreFactory;

/**
* {@inheritDoc}
*/
public function __construct(EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager, RegistrationHelper $helper, SenatorsHelper $senatorsHelper, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
public function __construct(
EntityRepositoryInterface $entity_repository,
LanguageManagerInterface $language_manager,
EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL,
TimeInterface $time = NULL,
RegistrationHelper $helper,
SenatorsHelper $senatorsHelper,
PrivateTempStoreFactory $tempStoreFactory
) {
parent::__construct($entity_repository, $language_manager, $entity_type_bundle_info, $time);
$this->helper = $helper;
$this->senatorsHelper = $senatorsHelper;
$this->tempStoreFactory = $tempStoreFactory;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): self {
return new static(
$container->get('entity.repository'),
$container->get('language_manager'),
$container->get('nys_registration.helper'),
$container->get('nys_senators.senators_helper'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time')
);
$container->get('entity.repository'),
$container->get('language_manager'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time'),
$container->get('nys_registration.helper'),
$container->get('nys_senators.senators_helper'),
$container->get('tempstore.private')
);
}

/**
Expand Down Expand Up @@ -194,6 +212,14 @@ public function formBuildStep1(array &$form, FormStateInterface $form_state): ar
],
];

// Prefill address if value found from find my senator form.
$tempstore = $this->tempStoreFactory->get('nys_registration');
$find_my_senator_address = $tempstore->get('find_my_senator_address');
if (!empty($find_my_senator_address)) {
$form['field_address']['widget'][0]['address']['#default_value'] = $find_my_senator_address;
$this->messenger()->addStatus($this->t('We reused the address you provided in "Find My Senator". Please check that it is correct before proceeding.'));
}

// Check if redirected from Senator's Page.
if (\Drupal::request()->headers->get('referer')) {
$referrer = explode('/', \Drupal::request()->headers->get('referer'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ hr {

.c-find-my-senator--row {
margin-bottom:30px;
margin-top:30px;
}

hr.c-find-my-senator--divider {
Expand Down

0 comments on commit 5400a7a

Please sign in to comment.