blog.module 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * @file
  4. * Enables multi-user blogs.
  5. */
  6. /**
  7. * Implements hook_node_info().
  8. */
  9. function blog_node_info() {
  10. return array(
  11. 'blog' => array(
  12. 'name' => t('Blog entry'),
  13. 'base' => 'blog',
  14. 'description' => t('Use for multi-user blogs. Every user gets a personal blog.'),
  15. )
  16. );
  17. }
  18. /**
  19. * Implements hook_user_view().
  20. */
  21. function blog_user_view($account) {
  22. if (user_access('create blog content', $account)) {
  23. $account->content['summary']['blog'] = array(
  24. '#type' => 'user_profile_item',
  25. '#title' => t('Blog'),
  26. // l() escapes the attributes, so we should not escape !username here.
  27. '#markup' => l(t('View recent blog entries'), "blog/$account->uid", array('attributes' => array('title' => t("Read !username's latest blog entries.", array('!username' => format_username($account)))))),
  28. '#attributes' => array('class' => array('blog')),
  29. );
  30. }
  31. }
  32. /**
  33. * Implements hook_help().
  34. */
  35. function blog_help($path, $arg) {
  36. switch ($path) {
  37. case 'admin/help#blog':
  38. $output = '<h3>' . t('About') . '</h3>';
  39. $output .= '<p>' . t("The Blog module allows registered users to maintain an online journal, or <em>blog</em>. Blogs are made up of individual <em>blog entries</em>. By default, the blog entries are displayed by creation time in descending order, with comments enabled, and are promoted to the site's front page. For more information, see the online handbook entry for <a href='@blog'>Blog module</a>.", array('@blog' => 'http://drupal.org/documentation/modules/blog/')) . '</p>';
  40. $output .= '<h3>' . t('Uses') . '</h3>';
  41. $output .= '<dl>';
  42. $output .= '<dt>' . t('Single-user blogs') . '</dt>';
  43. $output .= '<dd>' . t("Each user's blog entries are automatically displayed with a link to the user's main blog page. You can create as many single-user blogs as you have site users with permission to create blog content.") . '</dd>';
  44. $output .= '<dt>' . t('Multi-user blogs') . '</dt>';
  45. $output .= '<dd>' . t("Blog entries from each single-user blog are also aggregated into one central multi-user blog, which displays the blog content of all users in a single listing.") . '</dd>';
  46. $output .= '<dt>' . t('Navigation') . '</dt>';
  47. $output .= '<dd>' . t("There is an optional <em>Blogs</em> menu item added to the Navigation menu, which displays all blogs available on your site, and a <em>My blog</em> item displaying the current user's blog entries.") . '</dd>';
  48. $output .= '<dt>' . t('Blocks') . '</dt>';
  49. $output .= '<dd>' . t('The Blog module also creates a default <em>Recent blog posts</em> block that may be enabled at the <a href="@blocks">blocks administration page</a>.', array('@blocks' => url('admin/structure/block'))) . '</dd>';
  50. $output .= '</dl>';
  51. return $output;
  52. }
  53. }
  54. /**
  55. * Implements hook_form().
  56. */
  57. function blog_form($node, $form_state) {
  58. return node_content_form($node, $form_state);
  59. }
  60. /**
  61. * Implements hook_view().
  62. */
  63. function blog_view($node, $view_mode) {
  64. if ($view_mode == 'full' && node_is_page($node)) {
  65. // Breadcrumb navigation. l() escapes title, so we should not escape !name.
  66. drupal_set_breadcrumb(array(l(t('Home'), NULL), l(t('Blogs'), 'blog'), l(t("!name's blog", array('!name' => format_username($node))), 'blog/' . $node->uid)));
  67. }
  68. return $node;
  69. }
  70. /**
  71. * Implements hook_node_view().
  72. */
  73. function blog_node_view($node, $view_mode) {
  74. if ($view_mode != 'rss') {
  75. if ($node->type == 'blog' && (arg(0) != 'blog' || arg(1) != $node->uid)) {
  76. // This goes to l(), which escapes !username in both title and attributes.
  77. $links['blog_usernames_blog'] = array(
  78. 'title' => t("!username's blog", array('!username' => format_username($node))),
  79. 'href' => "blog/$node->uid",
  80. 'attributes' => array('title' => t("Read !username's latest blog entries.", array('!username' => format_username($node)))),
  81. );
  82. $node->content['links']['blog'] = array(
  83. '#theme' => 'links__node__blog',
  84. '#links' => $links,
  85. '#attributes' => array('class' => array('links', 'inline')),
  86. );
  87. }
  88. }
  89. }
  90. /**
  91. * Implements hook_menu().
  92. */
  93. function blog_menu() {
  94. $items['blog'] = array(
  95. 'title' => 'Blogs',
  96. 'page callback' => 'blog_page_last',
  97. 'access arguments' => array('access content'),
  98. 'type' => MENU_SUGGESTED_ITEM,
  99. 'file' => 'blog.pages.inc',
  100. );
  101. $items['blog/%user_uid_optional'] = array(
  102. 'title' => 'My blog',
  103. 'page callback' => 'blog_page_user',
  104. 'page arguments' => array(1),
  105. 'access callback' => 'blog_page_user_access',
  106. 'access arguments' => array(1),
  107. 'file' => 'blog.pages.inc',
  108. );
  109. $items['blog/%user/feed'] = array(
  110. 'title' => 'Blogs',
  111. 'page callback' => 'blog_feed_user',
  112. 'page arguments' => array(1),
  113. 'access callback' => 'blog_page_user_access',
  114. 'access arguments' => array(1),
  115. 'type' => MENU_CALLBACK,
  116. 'file' => 'blog.pages.inc',
  117. );
  118. $items['blog/feed'] = array(
  119. 'title' => 'Blogs',
  120. 'page callback' => 'blog_feed_last',
  121. 'access arguments' => array('access content'),
  122. 'type' => MENU_CALLBACK,
  123. 'file' => 'blog.pages.inc',
  124. );
  125. return $items;
  126. }
  127. /**
  128. * Implements hook_menu_local_tasks_alter().
  129. */
  130. function blog_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  131. global $user;
  132. // Add action link to 'node/add/blog' on 'blog' page.
  133. if ($root_path == 'blog') {
  134. $item = menu_get_item('node/add/blog');
  135. if ($item['access']) {
  136. $item['title'] = t('Create new blog entry');
  137. $data['actions']['output'][] = array(
  138. '#theme' => 'menu_local_action',
  139. '#link' => $item,
  140. );
  141. }
  142. }
  143. // Provide a helper action link to the author on the 'blog/%' page.
  144. elseif ($root_path == 'blog/%' && isset($router_item['page_arguments'][0]->uid) && $router_item['page_arguments'][0]->uid == $user->uid) {
  145. $data['actions']['output']['blog'] = array(
  146. '#theme' => 'menu_local_action',
  147. );
  148. if (user_access('create blog content')) {
  149. $data['actions']['output']['blog']['#link']['title'] = t('Post new blog entry.');
  150. $data['actions']['output']['blog']['#link']['href'] = 'node/add/blog';
  151. }
  152. else {
  153. $data['actions']['output']['blog']['#link']['title'] = t('You are not allowed to post a new blog entry.');
  154. }
  155. }
  156. }
  157. /**
  158. * Access callback for user blog pages.
  159. */
  160. function blog_page_user_access($account) {
  161. // The visitor must be able to access the site's content.
  162. // For a blog to 'exist' the user must either be able to
  163. // create new blog entries, or it must have existing posts.
  164. return $account->uid && user_access('access content') && (user_access('create blog content', $account) || _blog_post_exists($account));
  165. }
  166. /**
  167. * Helper function to determine if a user has blog posts already.
  168. */
  169. function _blog_post_exists($account) {
  170. return (bool)db_select('node', 'n')
  171. ->fields('n', array('nid'))
  172. ->condition('type', 'blog')
  173. ->condition('uid', $account->uid)
  174. ->condition('status', 1)
  175. ->range(0, 1)
  176. ->addTag('node_access')
  177. ->execute()
  178. ->fetchField();
  179. }
  180. /**
  181. * Implements hook_block_info().
  182. */
  183. function blog_block_info() {
  184. $block['recent']['info'] = t('Recent blog posts');
  185. $block['recent']['properties']['administrative'] = TRUE;
  186. return $block;
  187. }
  188. /**
  189. * Implements hook_block_configure().
  190. */
  191. function blog_block_configure($delta = '') {
  192. if ($delta == 'recent') {
  193. $form['blog_block_count'] = array(
  194. '#type' => 'select',
  195. '#title' => t('Number of recent blog posts to display'),
  196. '#default_value' => variable_get('blog_block_count', 10),
  197. '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 30)),
  198. );
  199. return $form;
  200. }
  201. }
  202. /**
  203. * Implements hook_block_save().
  204. */
  205. function blog_block_save($delta = '', $edit = array()) {
  206. if ($delta == 'recent') {
  207. variable_set('blog_block_count', $edit['blog_block_count']);
  208. }
  209. }
  210. /**
  211. * Implements hook_block_view().
  212. *
  213. * Displays the most recent 10 blog titles.
  214. */
  215. function blog_block_view($delta = '') {
  216. global $user;
  217. if (user_access('access content')) {
  218. $result = db_select('node', 'n')
  219. ->fields('n', array('nid', 'title', 'created'))
  220. ->condition('type', 'blog')
  221. ->condition('status', 1)
  222. ->orderBy('created', 'DESC')
  223. ->range(0, variable_get('blog_block_count', 10))
  224. ->addTag('node_access')
  225. ->execute();
  226. if ($node_title_list = node_title_list($result)) {
  227. $block['subject'] = t('Recent blog posts');
  228. $block['content']['blog_list'] = $node_title_list;
  229. $block['content']['blog_more'] = array(
  230. '#theme' => 'more_link',
  231. '#url' => 'blog',
  232. '#title' => t('Read the latest blog entries.'),
  233. );
  234. return $block;
  235. }
  236. }
  237. }
  238. /**
  239. * Implements hook_rdf_mapping().
  240. */
  241. function blog_rdf_mapping() {
  242. return array(
  243. array(
  244. 'type' => 'node',
  245. 'bundle' => 'blog',
  246. 'mapping' => array(
  247. 'rdftype' => array('sioc:Post', 'sioct:BlogPost'),
  248. ),
  249. ),
  250. );
  251. }