1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?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;
- use Drupal\node\NodeInterface;
- 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');
- }
- $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);
- }
- foreach( $node->get('field_image')->referencedEntities() as $ai => $entity) {
- $arr = $entity->toArray();
- $data['nodes'][$nid]['img'][] = array(
- 'uri' => file_create_url($arr['uri'][0]['value']),
- // 'raw' => $arr,
- );
- }
- if($first === true) {
- // print_r($data['nodes'][$nid]['img']);
- $first = false;
- }
- }
- return $data;
- }
- public function podcast($path) {
- // TODO: use $path
- $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;
- }
- public function podcastAll() {
- return $this->podcast('all.xml');
- }
- public function podcastAny(NodeInterface $ctype) {
- return new Response('tipo: BOH', 200,array( 'Content-Type' => 'application/rss+xml'));
- // return $this->podcast($ctype);
- }
- }
|