initial commit
This commit is contained in:
commit
b69bd7f300
5 changed files with 90 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
.*.sw*
|
9
calendar_ror.info.yml
Normal file
9
calendar_ror.info.yml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
name: Calendario ROR
|
||||||
|
type: module
|
||||||
|
description: 'Calendario ICS delle iniziative di ROR'
|
||||||
|
core: 8.x
|
||||||
|
version: 0.1
|
||||||
|
package: Ondarossa
|
||||||
|
dependencies:
|
||||||
|
- field
|
||||||
|
- link
|
9
calendar_ror.routing.yml
Normal file
9
calendar_ror.routing.yml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
calendar_ror.ics:
|
||||||
|
path: '/calendar/calendar.ics'
|
||||||
|
defaults:
|
||||||
|
_controller: '\Drupal\calendar_ror\Controller\CalendarRorController::ics'
|
||||||
|
_title: 'Global calendar'
|
||||||
|
requirements:
|
||||||
|
_access: 'TRUE'
|
||||||
|
options:
|
||||||
|
no_cache: 'TRUE'
|
56
src/Controller/CalendarRorController.php
Normal file
56
src/Controller/CalendarRorController.php
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
<?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) {
|
||||||
|
$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');
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
15
templates/calendar.ics.twig
Normal file
15
templates/calendar.ics.twig
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
BEGIN:VCALENDAR
|
||||||
|
PRODID:drupal-calendar-ror
|
||||||
|
VERSION:2.0
|
||||||
|
{% for nid, node in ror.calendar.nodes %}
|
||||||
|
|
||||||
|
BEGIN:VEVENT
|
||||||
|
SUMMARY: {{node.title.0.value}}
|
||||||
|
UID: node-{{node.nid.0.value}}@www.ondarossa.info
|
||||||
|
DTSTAMP: {{node.created.0.value}}
|
||||||
|
DTSTART: {{node.field_data_evento.0.value|date("Ymd\\THis")}}Z
|
||||||
|
DURATION: PT1H
|
||||||
|
URL: http://www.ondarossa.info{{node.path.0.alias}}
|
||||||
|
END:VEVENT
|
||||||
|
{% endfor %}
|
||||||
|
END:VCALENDAR
|
Loading…
Reference in a new issue