Currently the contact module uses the username in the contact form, email, ...

Patch attached replaces the usage of getUsername() with getDisplayName so the name is also overridable using hook_user_format_name_alter().

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

JeroenT created an issue. See original summary.

dawehner’s picture

Just a quick review ...

+++ b/core/modules/contact/src/MailHandler.php
@@ -127,16 +127,16 @@ public function sendMailMessages(MessageInterface $message, AccountInterface $se
       $this->logger->notice('%sender-name (@sender-from) sent an email regarding %contact_form.', array(
-        '%sender-name' => $sender_cloned->getUsername(),
+        '%sender-name' => $sender_cloned->getDisplayName(),
         '@sender-from' => $sender_cloned->getEmail(),
...
       $this->logger->notice('%sender-name (@sender-from) sent %recipient-name an email.', array(
-        '%sender-name' => $sender_cloned->getUsername(),
+        '%sender-name' => $sender_cloned->getDisplayName(),
         '@sender-from' => $sender_cloned->getEmail(),
-        '%recipient-name' => $message->getPersonalRecipient()->getUsername(),
+        '%recipient-name' => $message->getPersonalRecipient()->getDisplayName(),

Does that really make sense for the logger? For those purposes the username is easier to use

jibran’s picture

Title: Contact module: Use display name instead of username » Use display name instead of username
Status: Needs review » Needs work

NW for #2.

JeroenT’s picture

Status: Needs work » Needs review
FileSize
982 bytes
4.51 KB

Made changes as suggested in #2.

dawehner’s picture

Status: Needs review » Needs work
+++ b/core/modules/contact/src/Tests/ContactPersonalTest.php
@@ -60,7 +60,7 @@ protected function setUp() {
-    $mail = $this->webUser->getUsername() . '&escaped@example.com';
+    $mail = $this->webUser->getDisplayName() . '&escaped@example.com';
     $this->webUser->setEmail($mail)->save();

this is also wrong, its not about displaying.

JeroenT’s picture

Status: Needs work » Needs review
FileSize
1.06 KB
3.62 KB

Created a new patch. Made changes as suggested in #5.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Thank you

  • catch committed 84e0606 on 8.2.x
    Issue #2717805 by JeroenT, dawehner: Use display name instead of...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.2.x, thanks!

This is arguably a bug/task, but then it's also a behaviour change that people could be relying on. So leaving at 8.2.x for now since it doesn't feel urgent to apply to 8.1.x - the user will usually know their own username anyway.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

xjm’s picture

Title: Use display name instead of username » Use display name instead of username in contact form messages