AdminTwigExtension.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @package Grav\Plugin\Admin
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Plugin\Admin\Twig;
  9. use Grav\Common\Data\Data;
  10. use Grav\Common\Grav;
  11. use Grav\Common\Page\Interfaces\PageInterface;
  12. use Grav\Common\Utils;
  13. use Grav\Common\Yaml;
  14. use Grav\Common\Language\Language;
  15. use Twig\Extension\AbstractExtension;
  16. use Twig\TwigFilter;
  17. use Twig\TwigFunction;
  18. use Grav\Plugin\Admin\Admin;
  19. class AdminTwigExtension extends AbstractExtension
  20. {
  21. /** @var Grav */
  22. protected $grav;
  23. /** @var Language $lang */
  24. protected $lang;
  25. public function __construct()
  26. {
  27. $this->grav = Grav::instance();
  28. $this->lang = $this->grav['user']->language;
  29. }
  30. public function getFilters(): array
  31. {
  32. return [
  33. new TwigFilter('tu', [$this, 'tuFilter']),
  34. new TwigFilter('toYaml', [$this, 'toYamlFilter']),
  35. new TwigFilter('fromYaml', [$this, 'fromYamlFilter']),
  36. new TwigFilter('adminNicetime', [$this, 'adminNicetimeFilter']),
  37. new TwigFilter('nested', [$this, 'nestedFilter']),
  38. new TwigFilter('flatten', [$this, 'flattenFilter']),
  39. ];
  40. }
  41. public function getFunctions(): array
  42. {
  43. return [
  44. new TwigFunction('admin_route', [$this, 'adminRouteFunc']),
  45. new TwigFunction('getPageUrl', [$this, 'getPageUrl']),
  46. new TwigFunction('clone', [$this, 'cloneFunc']),
  47. new TwigFunction('data', [$this, 'dataFunc']),
  48. ];
  49. }
  50. public function nestedFilter($current, $name)
  51. {
  52. $path = explode('.', trim($name, '.'));
  53. foreach ($path as $field) {
  54. if (is_object($current) && isset($current->{$field})) {
  55. $current = $current->{$field};
  56. } elseif (is_array($current) && isset($current[$field])) {
  57. $current = $current[$field];
  58. } else {
  59. return null;
  60. }
  61. }
  62. return $current;
  63. }
  64. public function flattenFilter($array)
  65. {
  66. return Utils::arrayFlattenDotNotation($array);
  67. }
  68. public function cloneFunc($obj)
  69. {
  70. return clone $obj;
  71. }
  72. public function adminRouteFunc(string $route = '', string $languageCode = null)
  73. {
  74. /** @var Admin $admin */
  75. $admin = Grav::instance()['admin'];
  76. return $admin->getAdminRoute($route, $languageCode)->toString(true);
  77. }
  78. public function getPageUrl(PageInterface $page)
  79. {
  80. /** @var Admin $admin */
  81. $admin = Grav::instance()['admin'];
  82. return $admin->getAdminRoute('/pages' . $page->rawRoute(), $page->language())->toString(true);
  83. }
  84. public static function tuFilter()
  85. {
  86. $args = func_get_args();
  87. $numargs = count($args);
  88. $lang = null;
  89. if (($numargs === 3 && is_array($args[1])) || ($numargs === 2 && !is_array($args[1]))) {
  90. $lang = array_pop($args);
  91. } elseif ($numargs === 2 && is_array($args[1])) {
  92. $subs = array_pop($args);
  93. $args = array_merge($args, $subs);
  94. }
  95. return Grav::instance()['admin']->translate($args, $lang);
  96. }
  97. public function toYamlFilter($value, $inline = null)
  98. {
  99. return Yaml::dump($value, $inline);
  100. }
  101. public function fromYamlFilter($value)
  102. {
  103. return Yaml::parse($value);
  104. }
  105. public function adminNicetimeFilter($date, $long_strings = true)
  106. {
  107. return Grav::instance()['admin']->adminNiceTime($date, $long_strings);
  108. }
  109. public function dataFunc(array $data, $blueprints = null)
  110. {
  111. return new Data($data, $blueprints);
  112. }
  113. }