Showing Guestbook form and entries on other pages in DRUPAL 7
Categories
Component ID
Component name
Component type
Maintenance status
Development status
Component security advisory coverage
Component created
Component changed
Component body
Guestbook module provides a site guestbook and individual user guestbooks. Other users can post comments on the users guestbooks. Now there is
only one drawback in this module that we can't show this guestbook on our desired regions or pages and there is no guestbook block which helps
in achieving this.
Here we'll show you how to achieve this task. Follow the below steps and you'll be able to show your guestbook form and entries on the desired
pages or regions. But we need user id or profile id of a user in the url to achieve this, as without having one of these two ids we can't be able to
filter the forms and entries according to the user.
1) Install and configure the guestbook module for drupal 7.(http://ftp.drupal.org/files/projects/guestbook-7.x-2.x-dev.tar.gz)
2) Now create a view of user type and display as a block.
-> Then add a relationship - User:Profile
-> add contextual filter - Profile:Profile id/User:uid
-> add fields - User:uid/Profile:uid and make it exclude from display.
-> add another field Global:php and write the below code in the Output Code
$output .= drupal_render(drupal_get_form('guestbook_form_entry_form', $row->uid));
print $output;
In the above code:
<------- drupal_render Renders HTML given a structured array tree.
drupal_get_form Returns a renderable form array for a given form ID.
guestbook_form_entry_form is the ID of the form.
$row->uid is the replacement code used from the view. ------>
3) This will make a block of Guestbook form which you can show on any page whose url contains profile id or user id of a user.
Now for showing the guestbook entries of a user on different page. Follow the below steps to achieve this.
1) Create a view of user type and display as a block.
-> Then add a relationship - User:Profile
-> add contextual filter - Profile:Profile id
-> add fields - User:uid/Profile:uid and make it exclude from display.
2) Now make this View's tpl file and write the below code in it.
$querysimilars = db_select('guestbook', 'g');
$querysimilars->fields('g',array('message'));
$querysimilars->fields('g',array('author'));
$querysimilars->condition('g.recipient', $view->result[0]->profile_users_uid);
$querysimilars->orderBy('g.id', 'DESC');
$result = $querysimilars->execute();
while ($data = $result->fetchAssoc()) {
$users = user_load(array('uid' => $data['author']));
$usersname = $users->name;
echo '<ul><li><div class="gmessage">' . $data['message'] . '</div><div class="postedby">posted by ' . $usersname . '</div></li></ul>';
}
In the above code:
<------ There is a sql query which selects fields (message and author) from table (guestbook).
Condition applied is - where g.recipient = the value coming in the view field which is written as($view->result[0]->profile_users_uid).
To print all the entries while loop is written.
Now to print the author name we have to load the user by his/her uid, which is taken from the guestbook table. ------>
3) This will make a block of Guestbook entries which you can show on any page whose url contains profile id or user id of a user.
