Guest User

Untitled

a guest
Apr 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. services:
  2. event_countdown.date_calculator:
  3. class: Drupalevent_countdownDateCalculator
  4.  
  5. event_countdown.content:
  6. defaults:
  7. _title: 'Event Countdown'
  8. requirements:
  9. _permission: 'access content'
  10. options:
  11. no_cache: 'TRUE'
  12.  
  13. <?php
  14.  
  15. namespace Drupalevent_countdown;
  16.  
  17. class DateCalculator {
  18.  
  19. // forms the block's output based on given date
  20. public function daysUntilEventStarts($date) {
  21. // get difference in days between now and the given date
  22. $difference = $this->getDifferenceInDaysFromCurrentDate($date);
  23. // if event date is in the future
  24. if($difference >= 1) {
  25. return "Days left until the event starts: " . $difference;
  26. // if event date and current date are on the same day
  27. } else if($difference == 0) {
  28. return "The event is in progress.";
  29. // if event date is in the past
  30. } else {
  31. return "The event has already ended.";
  32. }
  33. }
  34.  
  35. // calculates difference in days between now and given date
  36. public function getDifferenceInDaysFromCurrentDate($date) {
  37. // current time
  38. $now = time();
  39. // event datetime field converted to timestamp
  40. $event_date = strtotime($date);
  41. // timestamp difference
  42. $difference = $event_date - $now;
  43. // timestamp difference rounded down to days
  44. return round($difference / (60 * 60 * 24));
  45. }
  46.  
  47. }
  48.  
  49. <?php
  50.  
  51. namespace Drupalevent_countdownPluginBlock;
  52.  
  53. use DrupalCoreBlockBlockBase;
  54. use SymfonyComponentDependencyInjectionContainerInterface;
  55. use Drupalevent_countdownDateCalculator;
  56.  
  57.  
  58. /**
  59. * Provides an event countdown block.
  60. *
  61. * @Block(
  62. * id = "event_countdown_block",
  63. * admin_label = @Translation("Event countdown block"),
  64. * category = @Translation("Event countdown block"),
  65. * )
  66. */
  67.  
  68. class EventCountdownBlock extends BlockBase {
  69.  
  70. protected $dateCalculator;
  71.  
  72. public function __construct(DateCalculator $dateCalculator) {
  73. $this->dateCalculator = $dateCalculator;
  74. }
  75.  
  76. public static function create(ContainerInterface $container) {
  77. return new static($container->get('event_countdown.date_calculator'));
  78. }
  79.  
  80. public function build() {
  81.  
  82. // get current node based on route
  83. $node = Drupal::routeMatch()->getParameter('node');
  84.  
  85. //check whether node type equals 'event' - if it does, get text output, otherwise display an error message
  86. if($node->getType() == "event") {
  87. // get datetime value from node
  88. $date = $node->field_event_date->value;
  89. // call service method to calculate difference in days
  90. $output = $this->dateCalculator->daysUntilEventStarts($date);
  91. } else {
  92. // display error
  93. $output = "Woops! This block is intended only for event pages.";
  94. }
  95.  
  96. return array(
  97. // output
  98. '#markup' => $output,
  99. // prevent block caching
  100. '#cache' => [
  101. 'max-age' => 0,
  102. ],
  103. );
  104. }
  105.  
  106. }
Add Comment
Please, Sign In to add comment