2013-04-23 40 views
0

我從果園​​代幣提供商那裏得到一個頭痛,任何幫助將是一件幸事。 我一直在複製評論標記。果園代幣 - >

目標是發送「問題」內容項目時發送的電子郵件。

這裏是我的內容類型「問題」的解決方案:

public interface ITokenProvider : IEventHandler 
{ 
    void Describe(dynamic context); 
    void Evaluate(dynamic context); 
} 

public class QuestionTokens : ITokenProvider 

{ 
    public QuestionTokens() 
    { 
     T = NullLocalizer.Instance; 
    } 
public Localizer T { get; set; } 

public void Describe(dynamic context) 
{ 
    //presume this is correct 
    context.For("Content", T("Content Items"), T("Content Items")) 
     .Token("QuestionText", T("Question Text"), T("Text of the question")) 
     .Token("QuestionAuthor", T("Question Author"), T("Author of the question")); 
    //presume this is incorrect? correct for the content type? 
    context.For("Question", T("Questions"), T("Questions from users")) 
     .Token("QuestionText", T("Question Text"), T("Text of the question")) 
     .Token("QuestionAuthor", T("Question Author"), T("Author of the question")); 
} 

public void Evaluate(dynamic context) 
{ 

    Func<IContent, object> questionTextAccessorFromContent = (content) => { 
     var part = content.As<QuestionPart>(); 
     return part.QuestionText; 
    }; 

    Func<QuestionPart, object> questionTextAccessor = (part) => 
    { 
     return part.QuestionText; 
    }; 

    Func<IContent, object> authorAccessorFromContent = (content) => { 
     var part = content.As<QuestionPart>(); 
     return part.Author; 
    }; 

    Func<QuestionPart, object> authorAccessor = (part) => 
    { 
     return part.Author; 
    }; 

    //doesnt work 
    context.For<IContent>("Content") 
     .Token("QuestionText", (Func<IContent, object>) 
    (content => content.As<QuestionPart>().Record.QuestionText)) 
     .Token("QuestionAuthor", (Func<IContent, object>) 
    (content => content.As<QuestionPart>().Record.Author)); 
    //doesnt work 
    context.For<IContent>("Question") 
     .Token("QuestionText", (Func<IContent, object>) 
    (content => content.As<QuestionPart>().Record.QuestionText)) 
     .Token("QuestionAuthor", (Func<IContent, object>) 
    (content => content.As<QuestionPart>().Record.Author)); 
    //doesnt work 
    context.For<QuestionPart>("Question") 
     .Token("QuestionText", (Func<QuestionPart, object>) 
    (content => content.Record.QuestionText)) 
     .Token("Author", (Func<QuestionPart, object>) 
    (content => content.Record.Author)); ; 
} 

private static string QuestionText(IContent question) 
{ 
    var questionPart = question.As<QuestionPart>(); 
    return questionPart.QuestionText; 
} 

private static string Author(IContent question) 
{ 
    var questionPart = question.As<QuestionPart>(); 
    return questionPart.Author; 
} 

}

這是Action(當問題被出版),它發送一個電子郵件正文: (更新爲測試Medeiros的建議)

<p>A question has been created by: {Content.QuestionAuthor}</p> 
<p>A question has been created by: {Content.QuestionAuthor.Text}</p> 
<p>A question has been created by: {Content.QuestionAuthor.Value}</p> 
<p>A question has been created by: {Question.QuestionAuthor}</p> 
<p>A question has been created by: {Question.QuestionAuthor.Text}</p> 
<p>A question has been created by: {Question.QuestionAuthor.Value}</p> 
<p>Message: {Content.QuestionText}</p> 
<p>Message: {Content.QuestionText.Text}</p> 
<p>Message: {Content.QuestionText.Value}</p> 
<p>Message: {Question.QuestionText}</p> 
<p>Message: {Question.QuestionText.Text}</p> 
<p>Message: {Question.QuestionText.Value}</p> 

所有'我'的標記被替換爲空白文本。其他令牌,如:{Content.ContentType} {User.Email}工作得很好。任何人注意到的任何錯誤或暗示都將非常有用。

謝謝,馬特

回答

1

IVe中重新編寫的令牌供應商,它的工作原理有點...(現在的工作完全)

public class QuestionTokens : Orchard.Tokens.ITokenProvider 
{ 
    private readonly IContentManager contentManager; 
    private readonly IWorkContextAccessor workContextAccessor; 

    public QuestionTokens(IContentManager contentManager, IWorkContextAccessor workContextAccessor) 
    { 
     this.workContextAccessor = workContextAccessor; 
     this.contentManager = contentManager; 
     T = NullLocalizer.Instance; 
    } 

    public Localizer T { get; set; } 

    /// <summary> 
    /// Describes the specified context. 
    /// </summary> 
    /// <param name="context">The context.</param> 
    public void Describe(DescribeContext context) 
    { 
     context.For("Content", T("Content Items"), T("Content Items")) 
      .Token("QuestionText", T("Question Text"), T("Text of the question")) 
      .Token("QuestionAuthor", T("Question Author"), T("Author of the question")); 
    } 

    /// <summary> 
    /// Evaluates the specified context. 
    /// </summary> 
    /// <param name="context">The context.</param> 
    public void Evaluate(EvaluateContext context) 
    { 
     context.For<IContent>("Content") 
      .Token("QuestionText", content => QuestionText(content, context)) 
      .Token("QuestionAuthor", content => Author(content, context)); 
    } 

    private string QuestionText(IContent question, EvaluateContext context) 
    { 
     var questionPart = question.As<QuestionPart>(); 
     return questionPart.QuestionText; 
    } 

    private string Author(IContent question, EvaluateContext context) 
    { 
     var questionPart = question.As<QuestionPart>(); 
     return questionPart.Author; 
    } 

} 

我現在可以調試至少在私人QuestionText和作者方法的代碼。由於在控制器中觸發此行時發佈的事件觸發,檢索到的值爲'null': services.ContentManager.Create(「Question」);

因此,內容項目尚未填充,並且「已發佈」事件在其生命開始之前就已觸發。時間玩它的內容類型設置了一下,看看是否有所作爲...

最後所有工作根據需要。事件部分「發佈」事件仍然在發生,當我不期望它,所以我改變了內容的生活。

從控制器:(一些代碼,刪除服務,所以我沒有發佈更多的代碼)

var item = services.ContentManager.New<Models.QuestionPart>("Question"); 
this.TryUpdateModel(item); 
services.ContentManager.Create(item.ContentItem); //where the event is firing 
Services.ContentManager.Publish(item.ContentItem); //where i expected the event to fire :) 

最初它是創建,更新和保存。但事件限制了這一點。所以現在它的新,更新和創建。 至於代幣。他們只是: {Content.QuestionAuthor}和{Content.QuestionText}。當一切正常時,一切都是正確的,這很好,很簡單。

0

馬修,嘗試添加一個.value的爲:{Content.QuestionAuthor.Value}

如果這不起作用,嘗試。文本。

希望這會有所幫助。

+0

沒有運氣無論是價值還是在最後文本。好的提示雖然。我會嘗試更新我的消息。我曾嘗試過像註釋那樣的靜態方法,但它永遠不會到達該代碼塊。所以我想知道它的描述部分是錯的...? – Matthew 2013-04-24 11:52:17

0

原始問題中的代碼看起來準確。您遵循正確的Orchard Events總線慣例創建從IEventHandler繼承的接口link

您在控制器代碼中的更改可能是您獲取值的原因。我調試代碼的經驗是,它取決於你的規則訂閱什麼事件。例如,類型爲「Content Created」的事件在Evaluate方法中觸發調試器,但所有自定義零件屬性均爲null。但是,「提交自定義表單時」擊中並填充了值。

我花了整整一天的故障排除我的代碼看起來類似你上面,這是直到我開始測試在我的規則其他事件類型,它有居住在私有函數數據。

這裏是我的代碼:

public interface ITokenProvider : IEventHandler 
{ 
    void Describe(dynamic context); 
    void Evaluate(dynamic context); 
} 

public class PersonTokens : ITokenProvider 
{ 
    private readonly IContentManager _contentManager; 

    public Localizer T { get; set; } 

    public PersonTokens(IContentManager contentManager) 
    { 
    _contentManager = contentManager; 
    T = NullLocalizer.Instance; 
    } 
    public void Describe(dynamic context) 
    { 
    context.For("Content", T("Content Items"), T("Content Items")) 
     .Token("FirstName", T("First Name"), T("The Person's First Name")) 
     .Token("LastName", T("Last Name"), T("The Person's Last Name")) 
     .Token("EmailAddress", T("Email Address"), T("The Person's Email Address")) 
     .Token("PhoneNumber", T("Phone Number"), T("The Person's Phone Number")); 
    } 

    public void Evaluate(dynamic context) 
    { 
    // Orchard.Tokens.Implementation.TokenManager.EvaluateContextImpl 
    context.For<IContent>("Content") 
     .Token("FirstName", (Func<IContent, object>)FirstName) 
     .Token("LastName", (Func<IContent, object>)(content => content.As<PersonPart>().Record.LastName)) 
     .Token("EmailAddress", (Func<IContent, object>)(content => content.As<PersonPart>().EmailAddress)) 
     .Token("PhoneNumber", (Func<IContent, object>)PhoneNumber); // left the PhoneNumber out of sample 
    } 

    private string FirstName(IContent person) 
    { 
    var personPart = person.As<PersonPart>(); 
    return personPart.Record.FirstName; 
    } 
}