12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- namespace Drupal\podcast_ror\Controller;
- use Drupal\Core\Controller\ControllerBase;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Drupal\Core\Template\TwigEnvironment;
- class PodcastRorController extends ControllerBase {
- protected $state;
- protected $twig;
- public function __construct($state, TwigEnvironment $twig) {
- $this->state = $state;
- $this->twig = $twig;
- }
- public static function create(ContainerInterface $container) {
- return new static(
- $container->get('state'),
- $container->get('twig')
- );
- }
- private function query() {
- //TODO: estrai argomento dall'url
- $arg = array('ror_news', 'redazionali', 'news_trasmissioni');
- $query = \Drupal::entityQuery('node');
- $query
- ->condition('status', '1')
- ->condition('type', $arg, 'IN')
- ->sort('nid', 'DESC')
- ->range(0, 20);
- $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();
- $data['nodes'][$nid]['summary'] = htmlspecialchars(substr(
- html_entity_decode(strip_tags($data['nodes'][$nid]['body'][0]['value'])),
- 0, 3500), ENT_XML1, 'UTF-8');
- }
- $data['audio'] = array();
- $first = true;
- foreach($nodes_e as $nid => $node) {
- $data['audio'][$nid] = array();
- foreach( $node->get('field_contenuti_audio')->referencedEntities() as $ai => $audio) {
- // print_r(get_class_methods(get_class($audio)));
- $data['audio'][$nid][$ai] = $audio->toArray();
- }
- if($first === true) {
- $first = false;
- }
- }
- return $data;
- }
- public function podcastAll() {
- $template = $this->twig->loadTemplate(drupal_get_path('module', 'podcast_ror') . '/templates/podcast.html.twig');
- $tmpl_data = [ 'ror' => array('podcast' => $this->query(),
- 'url' => \Drupal::service('path.current')->getPath(),
- )];
- $variables['ror']['url'] =
- $xml = $template->render($tmpl_data);
- $resp = new Response($xml, 200, array(
- 'Content-Type' => 'application/rss+xml'));
- return $resp;
- }
- }
|