From 7d93c298cc0dd3d66d8c6ab61a0c10dca6f215bd Mon Sep 17 00:00:00 2001 From: Igor Biki <ibiki@waterloo.ca> Date: Mon, 5 Dec 2022 16:12:03 -0500 Subject: [PATCH] EXPHR: Adding di services. --- src/Form/UwAddUsersForm.php | 41 +++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/Form/UwAddUsersForm.php b/src/Form/UwAddUsersForm.php index 51d3e4cb..4e4668e4 100644 --- a/src/Form/UwAddUsersForm.php +++ b/src/Form/UwAddUsersForm.php @@ -4,12 +4,45 @@ namespace Drupal\uw_cfg_common\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\path_alias\AliasManager; +use Drupal\user\Entity\User; +use Drupal\uw_cfg_common\Service\UWLdap; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Form class for the content access form. */ class UwAddUsersForm extends FormBase { + /** + * Path alias manager from core. + */ + protected AliasManager $pathAliasManager; + + /** + * UW Ldap service from uw_cfg_common module. + */ + protected UWLdap $uwLdap; + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + // Instantiates this form class. + return new static( + $container->get('path_alias.manager'), + $container->get('uw_cfg_common.uw_ldap') + ); + } + + /** + * Class constructor. + */ + public function __construct(AliasManager $pathAliasManager, UWLdap $uwLdap) { + $this->pathAliasManager = $pathAliasManager; + $this->uwLdap = $uwLdap; + } + /** * {@inheritdoc} */ @@ -82,7 +115,7 @@ class UwAddUsersForm extends FormBase { if ($existing_user) { $this->messenger()->addError($this->t('The user ID <a href="@link"><em>@user</em></a> already exists and was skipped.', [ - '@link' => \Drupal::service('path_alias.manager')->getAliasByPath('/user/' . $existing_user->uid->value), + '@link' => $this->pathAliasManager->getAliasByPath('/user/' . $existing_user->uid->value), '@user' => $user, ] )); @@ -108,7 +141,7 @@ class UwAddUsersForm extends FormBase { $password = str_shuffle($password); // Do the LDAP lookup. - $person = \Drupal::service('uw_cfg_common.uw_ldap')->lookupPerson($user); + $person = $this->UWLdap->lookupPerson($user); if (!$person) { $this->messenger()->addError($this->t('The user ID <em>@user</em> could not be found (or was set to private) and was skipped.', ['@user' => $user])); continue; @@ -118,7 +151,7 @@ class UwAddUsersForm extends FormBase { $email = empty($person['mail'][0]) ? $user . '@uwaterloo.ca' : $person['mail'][0]; // Add the user. - $new_user = \Drupal\user\Entity\User::create(); + $new_user = User::create(); $new_user->setPassword($password); $new_user->enforceIsNew(); $new_user->setEmail($email); @@ -130,7 +163,7 @@ class UwAddUsersForm extends FormBase { $this->messenger()->addStatus($this->t('Created a new user with the user ID <a href="@link"><em>@user</em></a>.', [ - '@link' => \Drupal::service('path_alias.manager')->getAliasByPath('/user/' . $new_user->uid->value), + '@link' => $this->pathAliasManager->getAliasByPath('/user/' . $new_user->uid->value), '@user' => $user, ] )); -- GitLab