dr8_zth.module 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * @file
  4. * The blog module.
  5. */
  6. use Drupal\Core\Render\Element;
  7. use Drupal\dr8_zth\Entity\Blog;
  8. /**
  9. * Implements hook_theme().
  10. */
  11. function dr8_zth_theme() {
  12. return array(
  13. 'blog' => array(
  14. 'render element' => 'elements',
  15. ),
  16. );
  17. }
  18. /**
  19. * Returns whether the current page is the page of the passed-in blog.
  20. *
  21. * @param \Drupal\dr8_zth\Entity\Blog $blog
  22. * A blog entity.
  23. *
  24. * @return bool
  25. * Is the current route the canonical route for the blog ?
  26. */
  27. function dr8_zth_blog_is_page(Blog $blog) {
  28. if (\Drupal::routeMatch()->getRouteName() == 'entity.blog.canonical' && $page_blog_id = \Drupal::routeMatch()->getRawParameter('blog')) {
  29. return $page_blog_id == $blog->id();
  30. }
  31. return FALSE;
  32. }
  33. /**
  34. * Implements hook_preprocess_blog().
  35. */
  36. function template_preprocess_blog(&$variables) {
  37. // Raise deep information to the root.
  38. $variables['view_mode'] = $variables['elements']['#view_mode'];
  39. $variables['blog'] = $variables['elements']['#blog'];
  40. /** @var \Drupal\dr8_zth\BlogInterface $blog */
  41. $blog = $variables['blog'];
  42. $variables['url'] = $blog->toUrl()->toString();
  43. // We use name here because that is what appears in the UI.
  44. $variables['title'] = $variables['elements']['title'];
  45. unset($variables['elements']['title']);
  46. $variables['page'] = $variables['view_mode'] == 'full' && dr8_zth_blog_is_page($blog);
  47. // Helpful $content variable for templates.
  48. $variables['content'] = array();
  49. foreach (Element::children($variables['elements']) as $key) {
  50. $variables['content'][$key] = $variables['elements'][$key];
  51. }
  52. }