PodcastRorController.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Drupal\podcast_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. class PodcastRorController extends ControllerBase {
  8. protected $state;
  9. protected $twig;
  10. public function __construct($state, TwigEnvironment $twig) {
  11. $this->state = $state;
  12. $this->twig = $twig;
  13. }
  14. public static function create(ContainerInterface $container) {
  15. return new static(
  16. $container->get('state'),
  17. $container->get('twig')
  18. );
  19. }
  20. private function query() {
  21. //TODO: estrai argomento dall'url
  22. $arg = array('ror_news', 'redazionali', 'news_trasmissioni');
  23. $query = \Drupal::entityQuery('node');
  24. $query
  25. ->condition('status', '1')
  26. ->condition('type', $arg, 'IN')
  27. ->sort('nid', 'DESC')
  28. ->range(0, 20);
  29. $nids = $query->execute();
  30. // TODO: load contenuti_audio referenced entities
  31. $data = array();
  32. $nodes_e = \Drupal\node\Entity\Node::loadMultiple($nids);
  33. $data['nodes'] = array();
  34. foreach($nodes_e as $nid => $node) {
  35. $data['nodes'][$nid] = $node->toArray();
  36. $data['nodes'][$nid]['summary'] = htmlspecialchars(substr(
  37. html_entity_decode(strip_tags($data['nodes'][$nid]['body'][0]['value'])),
  38. 0, 3500), ENT_XML1, 'UTF-8');
  39. }
  40. $data['audio'] = array();
  41. $first = true;
  42. foreach($nodes_e as $nid => $node) {
  43. $data['audio'][$nid] = array();
  44. foreach( $node->get('field_contenuti_audio')->referencedEntities() as $ai => $audio) {
  45. // print_r(get_class_methods(get_class($audio)));
  46. $data['audio'][$nid][$ai] = $audio->toArray();
  47. }
  48. if($first === true) {
  49. $first = false;
  50. }
  51. }
  52. return $data;
  53. }
  54. public function podcastAll() {
  55. $template = $this->twig->loadTemplate(drupal_get_path('module', 'podcast_ror') . '/templates/podcast.html.twig');
  56. $tmpl_data = [ 'ror' => array('podcast' => $this->query(),
  57. 'url' => \Drupal::service('path.current')->getPath(),
  58. )];
  59. $variables['ror']['url'] =
  60. $xml = $template->render($tmpl_data);
  61. $resp = new Response($xml, 200, array(
  62. 'Content-Type' => 'application/rss+xml'));
  63. return $resp;
  64. }
  65. }