コード例 #1
0
 /**
  * {@inheritdoc}
  */
 public function submit(array $form, array &$form_state)
 {
     $t_args = array('%title' => $this->entity->getTitle());
     $this->entity->delete();
     drupal_set_message($this->t('The menu link %title has been deleted.', $t_args));
     $this->logger->notice('Deleted menu link %title.', $t_args);
     $form_state['redirect_route'] = array('route_name' => '<front>');
 }
コード例 #2
0
 /**
  * {@inheritdoc}
  */
 public function submit(array $form, FormStateInterface $form_state)
 {
     $t_args = array('%title' => $this->entity->getTitle());
     $this->entity->delete();
     drupal_set_message($this->t('The menu link %title has been deleted.', $t_args));
     $this->logger->notice('Deleted menu link %title.', $t_args);
     $form_state->setRedirect('<front>');
 }
コード例 #3
0
 /**
  * {@inheritdoc}
  */
 public function sendMailMessages(MessageInterface $message, AccountInterface $sender)
 {
     // Clone the sender, as we make changes to mail and name properties.
     $sender_cloned = clone $this->userStorage->load($sender->id());
     $params = array();
     $current_langcode = $this->languageManager->getCurrentLanguage()->getId();
     $recipient_langcode = $this->languageManager->getDefaultLanguage()->getId();
     $contact_form = $message->getContactForm();
     if ($sender_cloned->isAnonymous()) {
         // At this point, $sender contains an anonymous user, so we need to take
         // over the submitted form values.
         $sender_cloned->name = $message->getSenderName();
         $sender_cloned->mail = $message->getSenderMail();
         // For the email message, clarify that the sender name is not verified; it
         // could potentially clash with a username on this site.
         $sender_cloned->name = $this->t('@name (not verified)', array('@name' => $message->getSenderName()));
     }
     // 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';
     $this->mailManager->mail('contact', $key_prefix . '_mail', $to, $recipient_langcode, $params, $sender_cloned->getEmail());
     // If requested, send a copy to the user, using the current language.
     if ($message->copySender()) {
         $this->mailManager->mail('contact', $key_prefix . '_copy', $sender_cloned->getEmail(), $current_langcode, $params, $sender_cloned->getEmail());
     }
     // If configured, send an auto-reply, using the current language.
     if (!$message->isPersonal() && $contact_form->getReply()) {
         // User contact forms do not support an auto-reply message, so this
         // message always originates from the site.
         if (!$sender_cloned->getEmail()) {
             $this->logger->error('Error sending auto-reply, missing sender e-mail address in %contact_form', ['%contact_form' => $contact_form->label()]);
         } else {
             $this->mailManager->mail('contact', 'page_autoreply', $sender_cloned->getEmail(), $current_langcode, $params);
         }
     }
     if (!$message->isPersonal()) {
         $this->logger->notice('%sender-name (@sender-from) sent an email regarding %contact_form.', array('%sender-name' => $sender_cloned->getUsername(), '@sender-from' => $sender_cloned->getEmail(), '%contact_form' => $contact_form->label()));
     } else {
         $this->logger->notice('%sender-name (@sender-from) sent %recipient-name an email.', array('%sender-name' => $sender_cloned->getUsername(), '@sender-from' => $sender_cloned->getEmail(), '%recipient-name' => $message->getPersonalRecipient()->getUsername()));
     }
 }
コード例 #4
0
 /**
  * {@inheritdoc}
  */
 public function sendSpool($limit = SpoolStorageInterface::UNLIMITED, array $conditions = array())
 {
     $check_counter = 0;
     // Send pending messages from database cache.
     $spool = $this->spoolStorage->getMails($limit, $conditions);
     if (count($spool) > 0) {
         // Switch to the anonymous user.
         $anonymous_user = new AnonymousUserSession();
         $this->accountSwitcher->switchTo($anonymous_user);
         $count_fail = $count_success = 0;
         $sent = array();
         $this->startTimer();
         while ($mail = $spool->nextMail()) {
             $mail->setKey('node');
             $result = $this->sendMail($mail);
             // Update spool status.
             // This is not optimal for performance but prevents duplicate emails
             // in case of PHP execution time overrun.
             foreach ($spool->getProcessed() as $msid => $row) {
                 $row_result = isset($row->result) ? $row->result : $result;
                 $this->spoolStorage->updateMails(array($msid), $row_result);
                 if ($row_result['status'] == SpoolStorageInterface::STATUS_DONE) {
                     $count_success++;
                     if (!isset($sent[$row->entity_type][$row->entity_id][$row->langcode])) {
                         $sent[$row->entity_type][$row->entity_id][$row->langcode] = 1;
                     } else {
                         $sent[$row->entity_type][$row->entity_id][$row->langcode]++;
                     }
                 }
                 if ($row_result['error']) {
                     $count_fail++;
                 }
             }
             // Check every n emails if we exceed the limit.
             // When PHP maximum execution time is almost elapsed we interrupt
             // sending. The remainder will be sent during the next cron run.
             if (++$check_counter >= static::SEND_CHECK_INTERVAL && ini_get('max_execution_time') > 0) {
                 $check_counter = 0;
                 // Break the sending if a percentage of max execution time was exceeded.
                 $elapsed = $this->getCurrentExecutionTime();
                 if ($elapsed > static::SEND_TIME_LIMIT * ini_get('max_execution_time')) {
                     $this->logger->warning('Sending interrupted: PHP maximum execution time almost exceeded. Remaining newsletters will be sent during the next cron run. If this warning occurs regularly you should reduce the !cron_throttle_setting.', array('!cron_throttle_setting' => \Drupal::l(t('Cron throttle setting'), new Url('simplenews.settings_mail'))));
                     break;
                 }
             }
         }
         // It is possible that all or at the end some results failed to get
         // prepared, report them separately.
         foreach ($spool->getProcessed() as $msid => $row) {
             $row_result = $row->result;
             $this->spoolStorage->updateMails(array($msid), $row_result);
             if ($row_result['status'] == SpoolStorageInterface::STATUS_DONE) {
                 $count_success++;
                 if (isset($row->langcode)) {
                     if (!isset($sent[$row->entity_type][$row->entity_id][$row->langcode])) {
                         $sent[$row->entity_type][$row->entity_id][$row->langcode] = 1;
                     } else {
                         $sent[$row->entity_type][$row->entity_id][$row->langcode]++;
                     }
                 }
             }
             if ($row_result['error']) {
                 $count_fail++;
             }
         }
         // Update subscriber count.
         if ($this->lock->acquire('simplenews_update_sent_count')) {
             foreach ($sent as $entity_type => $ids) {
                 foreach ($ids as $entity_id => $languages) {
                     \Drupal::entityManager()->getStorage($entity_type)->resetCache(array($entity_id));
                     $entity = entity_load($entity_type, $entity_id);
                     foreach ($languages as $langcode => $count) {
                         $translation = $entity->getTranslation($langcode);
                         $translation->simplenews_issue->sent_count = $translation->simplenews_issue->sent_count + $count;
                     }
                     $entity->save();
                 }
             }
             $this->lock->release('simplenews_update_sent_count');
         }
         // Report sent result and elapsed time. On Windows systems getrusage() is
         // not implemented and hence no elapsed time is available.
         if (function_exists('getrusage')) {
             $this->logger->notice('%success emails sent in %sec seconds, %fail failed sending.', array('%success' => $count_success, '%sec' => round($this->getCurrentExecutionTime(), 1), '%fail' => $count_fail));
         } else {
             $this->logger->notice('%success emails sent, %fail failed.', array('%success' => $count_success, '%fail' => $count_fail));
         }
         $this->state->set('simplenews.last_cron', REQUEST_TIME);
         $this->state->set('simplenews.last_sent', $count_success);
         $this->accountSwitcher->switchBack();
         return $count_success;
     }
 }