PodcastRorController.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. use Drupal\node\NodeInterface;
  8. class PodcastRorController 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. //TODO: estrai argomento dall'url
  23. $arg = array('ror_news', 'redazionali', 'news_trasmissioni');
  24. $query = \Drupal::entityQuery('node');
  25. $query
  26. ->condition('status', '1')
  27. ->condition('type', $arg, 'IN')
  28. ->sort('nid', 'DESC')
  29. ->range(0, 20);
  30. $nids = $query->execute();
  31. // TODO: load contenuti_audio referenced entities
  32. $data = array();
  33. $nodes_e = \Drupal\node\Entity\Node::loadMultiple($nids);
  34. $data['nodes'] = array();
  35. foreach($nodes_e as $nid => $node) {
  36. $data['nodes'][$nid] = $node->toArray();
  37. $data['nodes'][$nid]['summary'] = htmlspecialchars(substr(
  38. html_entity_decode(strip_tags($data['nodes'][$nid]['body'][0]['value'])),
  39. 0, 3500), ENT_XML1, 'UTF-8');
  40. }
  41. $first = true;
  42. foreach($nodes_e as $nid => $node) {
  43. $data['nodes'][$nid]['audio'] = array();
  44. $data['nodes'][$nid]['img'] = array();
  45. foreach( $node->get('field_contenuti_audio')->referencedEntities() as $ai => $entity) {
  46. $data['nodes'][$nid]['audio'][$ai] = $entity->toArray();
  47. //$data['nodes'][$nid]['audio'][$ai]['text'] = print_r($data['nodes'][$nid]['audio'][$ai]['field_audio_link'], true);
  48. }
  49. foreach( $node->get('field_image')->referencedEntities() as $ai => $entity) {
  50. $arr = $entity->toArray();
  51. $data['nodes'][$nid]['img'][] = array(
  52. 'uri' => file_create_url($arr['uri'][0]['value']),
  53. // 'raw' => $arr,
  54. );
  55. }
  56. if($first === true) {
  57. // print_r($data['nodes'][$nid]['img']);
  58. $first = false;
  59. }
  60. }
  61. return $data;
  62. }
  63. public function podcast($path) {
  64. // TODO: use $path
  65. $template = $this->twig->loadTemplate(drupal_get_path('module', 'podcast_ror') . '/templates/podcast.html.twig');
  66. $tmpl_data = [ 'ror' => array('podcast' => $this->query(),
  67. 'url' => \Drupal::service('path.current')->getPath(),
  68. )];
  69. //$variables['ror']['url'] =
  70. $xml = $template->render($tmpl_data);
  71. $resp = new Response($xml, 200, array( 'Content-Type' => 'application/rss+xml'));
  72. return $resp;
  73. }
  74. public function podcastAll() {
  75. return $this->podcast('all.xml');
  76. }
  77. public function podcastAny(NodeInterface $ctype) {
  78. return new Response('tipo: BOH', 200,array( 'Content-Type' => 'application/rss+xml'));
  79. // return $this->podcast($ctype);
  80. }
  81. }