1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- namespace Drupal\calendar_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 CalendarRorController 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() {
- $query = \Drupal::entityQuery('node');
- $query
- ->condition('status', '1')
- ->condition('type', 'eventi')
- ->sort('nid', 'DESC')
- ->range(0, 20);
- $nids = $query->execute();
- $data = array();
- $nodes_e = \Drupal\node\Entity\Node::loadMultiple($nids);
- $data['nodes'] = array();
- foreach($nodes_e as $nid => $node) {
- $na = $node->toArray();
- if(count($na['body']) > 0) { // c'e' gente che pubblica articoli senza body
- $body = $na['body'][0]['value'];
- } else {
- $body = '';
- }
- $data['nodes'][$nid] = $na;
- $data['nodes'][$nid]['summary'] = htmlspecialchars(substr(
- html_entity_decode(strip_tags($body)),
- 0, 3500), ENT_XML1, 'UTF-8');
- }
- return $data;
- }
- public function ics() {
- // TODO: use $path
- $template = $this->twig->loadTemplate(drupal_get_path('module', 'calendar_ror') . '/templates/calendar.ics.twig');
- $tmpl_data = [ 'ror' => array('calendar' => $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;
- }
- }
|