auto-date.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\Plugin;
  4. use RocketTheme\Toolbox\Event\Event;
  5. /**
  6. * Class AutoDatePlugin
  7. * @package Grav\Plugin
  8. */
  9. class AutoDatePlugin extends Plugin
  10. {
  11. /**
  12. * @return array
  13. *
  14. * The getSubscribedEvents() gives the core a list of events
  15. * that the plugin wants to listen to. The key of each
  16. * array section is the event that the plugin listens to
  17. * and the value (in the form of an array) contains the
  18. * callable (or function) as well as the priority. The
  19. * higher the number the higher the priority.
  20. */
  21. public static function getSubscribedEvents()
  22. {
  23. return [
  24. 'onAdminCreatePageFrontmatter' => ['onAdminCreatePageFrontmatter', 0]
  25. ];
  26. }
  27. /**
  28. * Initialize the plugin
  29. */
  30. public function onAdminCreatePageFrontmatter(Event $event)
  31. {
  32. $header = $event['header'];
  33. if (!isset($header['date'])) {
  34. $header['date'] = date($this->grav['config']->get('system.pages.dateformat.default', 'H:i d-m-Y'));
  35. $header['publish_date'] = date($this->grav['config']->get('system.pages.dateformat.default', 'H:i d-m-Y'));
  36. $event['header'] = $header;
  37. }
  38. }
  39. }