Boilerplate to use a Directus Instance to Build a Custom Website, Content will be Manage by Directus
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.7 KiB

3 years ago
  1. <?php
  2. namespace App\Repositories;
  3. use App\Repositories\RepositoryAbstract;
  4. /**
  5. * request menu items from a menu
  6. *
  7. * @author Björn Hase, Tentakelfabrik
  8. * @license http://opensource.org/licenses/MIT The MIT License
  9. * @link https://gitea.tentakelfabrik.de/Tentakelfabrik/super-gear-directus
  10. *
  11. */
  12. class MenuRepository extends RepositoryAbstract
  13. {
  14. /** endpoint */
  15. protected $endpoint = 'menu_items';
  16. /**
  17. * find menu_items by name of menu
  18. * menu must be published
  19. *
  20. * @param string $slug
  21. * @return array
  22. */
  23. public function findOneByName($name)
  24. {
  25. $results = $this->queryBuilder
  26. ->fields([
  27. 'title',
  28. 'target',
  29. 'url',
  30. 'page.title',
  31. 'page.status',
  32. 'page.slug',
  33. 'menus.sort',
  34. 'menus.menus_id.name'
  35. ])
  36. ->aliases('page[title]', 'page_title')
  37. ->aliases('page[status]', 'page_status')
  38. ->aliases('page[slug]', 'page_slug')
  39. ->filter([
  40. 'page' => [
  41. 'status' => 'published'
  42. ],
  43. 'menus' => [
  44. 'menus_id' => [
  45. 'name' => $name,
  46. 'status' => 'published'
  47. ]
  48. ]
  49. ])
  50. ->find();
  51. // @TODO Workaround sort functions seems have problems with relationals fields
  52. if (count($results['data'])) {
  53. usort($results['data'], function($a, $b) {
  54. return ($a['menus'][0]['sort'] > $b['menus'][0]['sort']);
  55. });
  56. }
  57. return $results;
  58. }
  59. }