2017-09-15 84 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