2017-09-15 99 views
1

我正在開發一個帶onContentAfterSave事件的Joomla插件,用於在保存新文章後發送電子郵件。Joomla:onContentAfterSave未觸發文章

當我保存新的菜單項或新的類別時,觸發事件。 但不適用於新文章。

Joomla! 3.7.5

public function onContentAfterSave($context, $article, $isNew){ 
    $link = JRoute::_(ContentHelperRoute::getArticleRoute( $article->id, $article->catid)); 

    $mailer = JFactory::getMailer(); 
    $config = JFactory::getConfig(); 
    $sender = array( 
     $config->get('mailfrom'), 
     $config->get('fromname') 
    ); 
    $mailer->setSender($sender); 


    $user = JFactory::getUser(); 
    $recipient = $user->email; 

    $recipient = array($recipient); 
    $mailer->addRecipient($recipient); 

    $body = '<p>Bonjour</p>' 
       .'Un nouveau <a href="'.$link.'">article</a> a été ajouté.'; 
    $mailer->isHtml(true); 
    $mailer->Encoding = 'base64'; 

    $mailer->setSubject('Nouveau article - BBN Times'); 
    $mailer->setBody($body); 


    $send = $mailer->Send(); 

    if ($send !== true) { 
     echo 'Error sending email: '; 
    } else { 
     echo 'Mail sent'; 
    } 

    return true; 
}  

回答

0

onContentAfterSave由Joomla!核心由擴展的模式,這意味着它應該被任何內容保存觸發觸發,而不是。

爲什麼它不在你的情況引發我能想到的原因有二:

  • 你在你的構造函數有一個條件檢查外延式並只允許特定的擴展使用該事件(或其他地方在你的插件)。
  • 你在上面的代碼中有錯誤。
+0

我的插件適用於所有類型(菜單,類別...),但不適用於文章類型。其實我使用這個條件 if($ isNew and in_array($ context,array('com_content.article','com_content.form'))) – emwww