state = $state; $this->twig = $twig; } public static function create(ContainerInterface $container) { return new static( $container->get('state'), $container->get('twig') ); } private function query($baseQuery) { //TODO: estrai argomento dall'url $arg = array('ror_news', 'redazionali', 'news_trasmissioni', 'rassegna_stampa'); $now = new \Drupal\Core\Datetime\DrupalDateTime('now'); $now_formatted = $now->format(\Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::DATETIME_STORAGE_FORMAT); $query = $baseQuery ->condition('status', '1') ->condition('type', $arg, 'IN') ->condition('field_tx_date', $now_formatted, '<') ->sort('field_tx_date', 'DESC') ->range(0, 30); // \Drupal::logger('podcast_ror')->info(var_export(\Drupal::request()->query->get('trxid'), true)); //$trxid = \Drupal::request()->query->get('trxid'); // TODO: se nell'url c'e' un trx=, allora bisogna // aggiungere che arg=news_trasmissioni // fare join con node__field_trasmissione ON // node.nid = node__field_trasmissione.entity_id // condition: node__field_trasmissione.field_trasmissione_target_id=$trx $nids = $query->execute(); // TODO: load contenuti_audio referenced entities $data = array(); $nodes_e = \Drupal\node\Entity\Node::loadMultiple($nids); $data['nodes'] = array(); foreach($nodes_e as $nid => $node) { $data['nodes'][$nid] = $node->toArray(); if( count($data['nodes'][$nid]['body']) > 0 ) { $data['nodes'][$nid]['summary'] = htmlspecialchars(substr( html_entity_decode(strip_tags($data['nodes'][$nid]['body'][0]['value'])), 0, 3500), ENT_XML1, 'UTF-8'); } else { $data['nodes'][$nid]['summary'] = ""; } } $first = true; foreach($nodes_e as $nid => $node) { $data['nodes'][$nid]['audio'] = array(); $data['nodes'][$nid]['img'] = array(); foreach( $node->get('field_contenuti_audio')->referencedEntities() as $ai => $entity) { $data['nodes'][$nid]['audio'][$ai] = $entity->toArray(); //$data['nodes'][$nid]['audio'][$ai]['text'] = print_r($data['nodes'][$nid]['audio'][$ai]['field_audio_link'], true); } if($node->hasField('field_image')) { $node_images = $node->get('field_image'); foreach( $node_images->referencedEntities() as $ai => $entity) { $arr = $entity->toArray(); $data['nodes'][$nid]['img'][] = array( 'uri' => file_create_url($arr['uri'][0]['value']), // 'raw' => $arr, ); } } else { // alcuni content_type non hanno immagine (rassegna stampa) $data['nodes'][$nid]['img'][] = []; } $data['nodes'][$nid]['tx'] = ""; if($node->getType() == 'news_trasmissioni') { $tx = \Drupal\taxonomy\Entity\Term::load( $node->get('field_trasmissione')->target_id ); if($tx !== null) { $data['nodes'][$nid]['tx'] = $tx->getName(); } } if($first === true) { $first = false; } } return $data; } public function podcast($query, $title) { // TODO: add $title argument to the function $template = $this->twig->loadTemplate(drupal_get_path('module', 'podcast_ror') . '/templates/podcast.html.twig'); $tmpl_data = [ 'ror' => array('podcast' => $this->query($query), 'url' => \Drupal::service('path.current')->getPath(), 'title' => $title, )]; //$variables['ror']['url'] = $xml = $template->render($tmpl_data); $resp = new Response($xml, 200, array( 'Content-Type' => 'application/rss+xml')); return $resp; } public function podcastAll() { $query = \Drupal::entityQuery('node'); return $this->podcast($query, 'Tutti gli aggiornamenti, i redazionali, le trasmissioni della radio di chi se la sente'); } public function podcastByType($contentType) { $query = \Drupal::entityQuery('node') ->condition('type', $contentType); // TODO: get "nice name" for $contentType return $this->podcast($query, $contentType); } public function podcastByTrxID($trxID) { $query = \Drupal::entityQuery('node') ->condition('type', 'news_trasmissioni') ->condition('field_trasmissione', $trxID); // TODO: discover Trx name, and pass it as podcast title return $this->podcast($query, 'Trasmissione ' . $trxID); } }