CalendarRorController.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. $na = $node->toArray();
  34. if(count($na['body']) > 0) { // c'e' gente che pubblica articoli senza body
  35. $body = $na['body'][0]['value'];
  36. } else {
  37. $body = '';
  38. }
  39. $data['nodes'][$nid] = $na;
  40. $data['nodes'][$nid]['summary'] = htmlspecialchars(substr(
  41. html_entity_decode(strip_tags($body)),
  42. 0, 3500), ENT_XML1, 'UTF-8');
  43. }
  44. return $data;
  45. }
  46. public function ics() {
  47. // TODO: use $path
  48. $template = $this->twig->loadTemplate(drupal_get_path('module', 'calendar_ror') . '/templates/calendar.ics.twig');
  49. $tmpl_data = [ 'ror' => array('calendar' => $this->query(),
  50. 'url' => \Drupal::service('path.current')->getPath(),
  51. )];
  52. //$variables['ror']['url'] =
  53. $xml = $template->render($tmpl_data);
  54. $resp = new Response($xml, 200, array( 'Content-Type' => 'application/rss+xml'));
  55. return $resp;
  56. }
  57. }