diff --git a/pet.module b/pet.module index 61d73e4..8504a04 100644 --- a/pet.module +++ b/pet.module @@ -5,15 +5,33 @@ * Previewable Email Template module. */ +use Drupal\Core\Language\LanguageInterface; use Drupal\node\Entity\Node; use Drupal\pet\Entity\Pet; use Drupal\user\Entity\User; /** * Loads a PET by ID. + * + * @param integer $pid + * The pet id. + * @param \Drupal\Core\Language\LanguageInterface $language + * The language object. + * + * @return \Drupal\pet\PetInterface + * The pet object. */ -function pet_load($pid) { - return Pet::load($pid); +function pet_load($pid, LanguageInterface $language) { + /** @var \Drupal\pet\PetInterface $pet */ + $pet = Pet::load($pid); + if ($pet) { + $langcode = $language->getId(); + if ($pet->hasTranslation($langcode)) { + $pet = $pet->getTranslation($langcode); + } + } + + return $pet; } /** @@ -98,9 +116,12 @@ function pet_lookup_uid($mail) { * in the PET. * bcc - Optional bcc emails which if provided which will override the * bcc's in the PET. + * language - The language object. */ function pet_send_mail($pet_id, $recipients, $options) { - $pet = pet_load($pet_id); + /** @var \Drupal\Core\Language\LanguageInterface $language */ + $language = isset($options['language']) ? $options['language'] : \Drupal::languageManager()->getCurrentLanguage(); + $pet = pet_load($pet_id, $language); if (!$pet) { \Drupal::logger('pet') ->error('Unable to load PET %pet_id.', ['%pet_id' => $pet_id]); @@ -162,7 +183,7 @@ function pet_send_mail($pet_id, $recipients, $options) { $params['pet_to'] = $mail; $params['pet_uid'] = pet_lookup_uid($mail); } - $message_status[$params['pet_to']] = pet_send_one_mail($pet, $params); + $message_status[$params['pet_to']] = pet_send_one_mail($pet, $params, $language); } // Return message status. return $message_status; @@ -188,10 +209,12 @@ function pet_send_mail($pet_id, $recipients, $options) { * pet_reply_to (optional) * The $params array may also contain data passed in by other modules. One * use of this is for token substitution. + * @param \Drupal\Core\Language\LanguageInterface $language + * The language object. * * @see hook_pet_substitutions_alter() */ -function pet_send_one_mail(Pet $pet, $params) { +function pet_send_one_mail(Pet $pet, $params, LanguageInterface $language) { $pet_logging = \Drupal::config('pet.settings')->get('pet_logging'); if (!pet_is_valid($pet)) { @@ -246,10 +269,8 @@ function pet_send_one_mail(Pet $pet, $params) { // Provided for Mime Mail module; send ONLY plain text. $params['plain'] = $pet->getSendPlain(); - $language_interface = \Drupal::languageManager()->getCurrentLanguage(); - $langcode = $language_interface->getId(); $message = \Drupal::service('plugin.manager.mail') - ->mail('pet', $pet->id(), $params['pet_to'], $langcode, $params, $params['pet_from']); + ->mail('pet', $pet->id(), $params['pet_to'], $language->getId(), $params, $params['pet_from']); if ($message['send'] && $pet_logging == 0) { \Drupal::logger('pet') ->notice('Successfully sent email to %recipient', ['%recipient' => $params['pet_to']]);