Jack O'Connor
Jack O'Connor

Reputation: 334

Passing the ID of a blog entry to a blog holder method

I am using the blog module for SilverStripe. Within the blog holder template there is a loop which loops over each blog entry. I can't run any methods from blog entry in this loop, i can only run blog holder methods. So.. I want to pass the ID of the current blog entry to the blog holder method I have written so I can retrieve the AuthorID (custom field I added) from the blog entry.

I have tried

BlogHolder.php

function getAuthor($identity) {
    $Author = DataObject::get_by_id('Person', $identity);
}

BlogHolder.ss

<p id="author-tag">By $getAuthor($ID)</p>

I have also tried to pass a direct integer for testing purposes and this also returned an error about the $identity variable being undefined, so it seems nothing is actually being passed to the method at all.

The error I receive is

[Warning] Missing argument 1 for BlogHolder_Controller::getAuthor(), called in /var/www/sites/quadra/framework/view/ViewableData.php on line 108 and defined

as well as

[Notice] Undefined variable: identity

and also..

[User Warning] DataObject::get_by_id passed a non-numeric ID #DataList

So the question here really is: Within the blog entries loop in the blog holder template, how can I pass the ID of the current blog entry to a method within the blog holder controller.

Thanks guys.

Upvotes: 0

Views: 191

Answers (2)

3dgoo
3dgoo

Reputation: 15794

I am guessing you are using the SilverStripe Blog module 1.0 branch for SilverStripe 3.1.

I can't run any methods from blog entry in this loop, I can only run blog holder methods.

You should be able to call all of the BlogEntry variables and functions that are in the the BlogEntry class. You cannot call BlogEntry_Controller functions from the BlogHolder page, but you can call all the BlogEntry variables and functions.

BlogEntry has an Author text variable in $db so you should be able to call $Author from inside your blog entry loop.

You could use the templates the blog module comes with. This displays information like the entry Author, Tags, Summary, Date.

Or if you want to create your own custom templates then you can look at the existing templates as a reference.

Here is the current 1.0 branch blog holder template and how it loops through its entries.

BlogHolder.ss

<% include BlogSideBar %>

<div id="BlogContent" class="blogcontent typography">

    <% include BreadCrumbs %>

    <% if SelectedTag %>
        <h3><% _t('BlogHolder_ss.VIEWINGTAGGED', 'Viewing entries tagged with') %> '$SelectedTag'</h3>
    <% else_if SelectedDate %>
        <h3><% _t('BlogHolder_ss.VIEWINGPOSTEDIN', 'Viewing entries posted in') %> $SelectedNiceDate</h3>
    <% else_if SelectedAuthor %>
        <h3><% _t('BlogHolder_ss.VIEWINGPOSTEDBY', 'Viewing entries posted by') %> $SelectedAuthor</h3>
    <% end_if %>

    <% if BlogEntries %>
        <% loop BlogEntries %>
            <% include BlogSummary %>
        <% end_loop %>
    <% else %>
        <h2><% _t('BlogHolder_ss.NOENTRIES', 'There are no blog entries') %></h2>
    <% end_if %>

    <% include BlogPagination %>

</div>

In the BlogEntries loop it includes the template BlogSummary.ss for each entry.

BlogSummary.ss

<div class="blogSummary">
    <h2 class="postTitle"><a href="$Link" title="<% _t('BlogSummary_ss.VIEWFULL', 'View full post titled -') %> '$Title'">$MenuTitle</a></h2>
    <p class="authorDate"><% _t('BlogSummary_ss.POSTEDBY', 'Posted by') %> $Author.XML <% _t('BlogSummary_ss.POSTEDON', 'on') %> $Date.Long | <a href="$Link#comments-holder" title="View Comments Posted">$Comments.Count <% _t('BlogEntry_ss.COMMENTS', 'Comments') %></a></p>
    <% if TagsCollection %>
        <p class="tags">
            <% _t('BlogSummary_ss.TAGS','Tags') %>:
            <% loop TagsCollection %>
                <a href="$Link" title="View all posts tagged '$Tag'" rel="tag">$Tag</a><% if not Last %>,<% end_if %>
            <% end_loop %>
        </p>
    <% end_if %>

    <% if BlogHolder.ShowFullEntry %>
        $Content
    <% else %> 
        <p>$Content.FirstParagraph(html)</p>
    <% end_if %>

    <p class="blogVitals">
        <a href="$Link#comments-holder" class="comments" title="View Comments for this post">
            $Comments.Count <% _t('BlogSummary_ss.SUMMARYCOMMENTS','comment(s)') %>
        </a> 
        | 
        <a href="$Link" class="readmore" title="Read Full Post">
            <% _t('BlogSummary_ss.READFULLPOST','Read the full post') %>
        </a>
    </p>
</div>

In the BlogSummary you can see items like $Author and $Date.Long.

Create your template like this. You shouldn't need to call a function in BlogHolder passing in an entry ID.

Upvotes: 1

Gavin Bruce
Gavin Bruce

Reputation: 1809

Assuming there is a has_one relationship from BlogEntry to Author, you should be able to put <p id="author-tag">$Author.Name</p> in your templates

Upvotes: 0

Related Questions