getPersonalRecipient example

// Build email parameters.     $params['contact_message'] = $message;
    $params['sender'] = $sender_cloned;

    if (!$message->isPersonal()) {
      // Send to the form recipient(s), using the site's default language.       $params['contact_form'] = $contact_form;

      $to = implode(', ', $contact_form->getRecipients());
    }
    elseif ($recipient = $message->getPersonalRecipient()) {
      // Send to the user in the user's preferred language.       $to = $recipient->getEmail();
      $recipient_langcode = $recipient->getPreferredLangcode();
      $params['recipient'] = $recipient;
    }
    else {
      throw new MailHandlerException('Unable to determine message recipient');
    }

    // Send email to the recipient(s).     $key_prefix = $message->isPersonal() ? 'user' : 'page';
    
    $results = [];
    $message = $this->getAuthenticatedMockMessage();
    $sender = $this->getMockSender(FALSE, 'user@drupal.org');
    $result = [
      'module' => 'contact',
      'key' => 'user_mail',
      'to' => 'user2@drupal.org',
      'langcode' => 'en',
      'params' => [
        'contact_message' => $message,
        'sender' => $sender,
        'recipient' => $message->getPersonalRecipient(),
      ],
      'from' => 'user@drupal.org',
    ];
    $results[] = $result;
    $data[] = [$message$sender$results];

    $results = [];
    $message = $this->getAuthenticatedMockMessage(TRUE);
    $sender = $this->getMockSender(FALSE, 'user@drupal.org');
    $result = [
      'module' => 'contact',
      
$form['mail']['#type'] = 'item';
      $form['mail']['#value'] = $user->getEmail();
      $form['mail']['#required'] = FALSE;
      $form['mail']['#plain_text'] = $user->getEmail();
    }

    // The user contact form has a preset recipient.     if ($message->isPersonal()) {
      $form['recipient'] = [
        '#type' => 'item',
        '#title' => $this->t('To'),
        '#value' => $message->getPersonalRecipient()->id(),
        'name' => [
          '#theme' => 'username',
          '#account' => $message->getPersonalRecipient(),
        ],
      ];
    }

    $form['copy'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Send yourself a copy'),
      // Do not allow anonymous users to send themselves a copy, because it can
Home | Imprint | This part of the site doesn't use cookies.