CalendarRorController.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Drupal\calendar_ror\Controller;
  3. use Drupal\Core\Controller\ControllerBase;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. use Drupal\Core\Template\TwigEnvironment;
  7. use Drupal\node\NodeInterface;
  8. class CalendarRorController extends ControllerBase {
  9. protected $state;
  10. protected $twig;
  11. public function __construct($state, TwigEnvironment $twig) {
  12. $this->state = $state;
  13. $this->twig = $twig;
  14. }
  15. public static function create(ContainerInterface $container) {
  16. return new static(
  17. $container->get('state'),
  18. $container->get('twig')
  19. );
  20. }
  21. private function query() {
  22. $query = \Drupal::entityQuery('node');
  23. $query
  24. ->condition('status', '1')
  25. ->condition('type', 'eventi')
  26. ->sort('nid', 'DESC')
  27. ->range(0, 20);
  28. $nids = $query->execute();
  29. $data = array();
  30. $nodes_e = \Drupal\node\Entity\Node::loadMultiple($nids);
  31. $data['nodes'] = array();
  32. foreach($nodes_e as $nid => $node) {
  33. $data['nodes'][$nid] = $node->toArray();
  34. $data['nodes'][$nid]['summary'] = htmlspecialchars(substr(
  35. html_entity_decode(strip_tags($data['nodes'][$nid]['body'][0]['value'])),
  36. 0, 3500), ENT_XML1, 'UTF-8');
  37. }
  38. return $data;
  39. }
  40. public function ics() {
  41. // TODO: use $path
  42. $template = $this->twig->loadTemplate(drupal_get_path('module', 'calendar_ror') . '/templates/calendar.ics.twig');
  43. $tmpl_data = [ 'ror' => array('calendar' => $this->query(),
  44. 'url' => \Drupal::service('path.current')->getPath(),
  45. )];
  46. //$variables['ror']['url'] =
  47. $xml = $template->render($tmpl_data);
  48. $resp = new Response($xml, 200, array( 'Content-Type' => 'application/rss+xml'));
  49. return $resp;
  50. }
  51. }