Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rasben committed Sep 13, 2024
1 parent 50f6dd1 commit f2ed819
Show file tree
Hide file tree
Showing 11 changed files with 592 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
uuid: 014a5ed5-1c85-432f-a1b8-3814869178fd
langcode: en
status: true
dependencies:
config:
- core.entity_view_mode.media.redia_feed_large
- field.field.media.image.field_byline
- field.field.media.image.field_media_image
- image.style.redia_feed_large
- media.type.image
module:
- image
id: media.image.redia_feed_large
targetEntityType: media
bundle: image
mode: redia_feed_large
content:
field_media_image:
type: image
label: hidden
settings:
image_link: ''
image_style: redia_feed_large
image_loading:
attribute: lazy
third_party_settings: { }
weight: 0
region: content
hidden:
created: true
field_byline: true
langcode: true
name: true
search_api_excerpt: true
thumbnail: true
uid: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
uuid: 7cd49745-6747-4b76-b64a-f3e2a22a338e
langcode: en
status: true
dependencies:
config:
- core.entity_view_mode.media.redia_feed_small
- field.field.media.image.field_byline
- field.field.media.image.field_media_image
- image.style.redia_feed_small
- media.type.image
module:
- image
id: media.image.redia_feed_small
targetEntityType: media
bundle: image
mode: redia_feed_small
content:
field_media_image:
type: image
label: hidden
settings:
image_link: ''
image_style: redia_feed_small
image_loading:
attribute: lazy
third_party_settings: { }
weight: 0
region: content
hidden:
created: true
field_byline: true
langcode: true
name: true
search_api_excerpt: true
thumbnail: true
uid: true
11 changes: 11 additions & 0 deletions config/sync/core.entity_view_mode.media.redia_feed_large.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
uuid: 74eba980-7ec0-4b81-a6da-5c33830b08d9
langcode: en
status: true
dependencies:
module:
- media
id: media.redia_feed_large
label: 'Redia feed - large'
description: ''
targetEntityType: media
cache: true
11 changes: 11 additions & 0 deletions config/sync/core.entity_view_mode.media.redia_feed_small.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
uuid: fd0b39a0-f244-4d82-af84-54c63cf451bc
langcode: en
status: true
dependencies:
module:
- media
id: media.redia_feed_small
label: 'Redia feed - small'
description: ''
targetEntityType: media
cache: true
1 change: 1 addition & 0 deletions config/sync/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module:
dpl_react: 0
dpl_react_apps: 0
dpl_recommender: 0
dpl_redia_legacy: 0
dpl_related_content: 0
dpl_reservations: 0
dpl_rest_base: 0
Expand Down
15 changes: 15 additions & 0 deletions config/sync/image.style.redia_feed_large.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
uuid: 923e70e6-ea34-446d-ae06-2f6ac0ac4f00
langcode: en
status: true
dependencies: { }
name: redia_feed_large
label: 'Redia feed large'
effects:
71f3eb2a-c6f8-4a85-a16f-651a6e43239e:
uuid: 71f3eb2a-c6f8-4a85-a16f-651a6e43239e
id: image_scale
weight: 1
data:
width: 2000
height: null
upscale: false
15 changes: 15 additions & 0 deletions config/sync/image.style.redia_feed_small.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
uuid: 33f9623f-9eea-4b05-8e9c-f1d3ac224119
langcode: en
status: true
dependencies: { }
name: redia_feed_small
label: 'Redia feed small'
effects:
2cd37185-a9d4-43e1-ba81-66b73d18cae2:
uuid: 2cd37185-a9d4-43e1-ba81-66b73d18cae2
id: image_scale
weight: 1
data:
width: 220
height: null
upscale: true
9 changes: 9 additions & 0 deletions web/modules/custom/dpl_redia_legacy/dpl_redia_legacy.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: "DPL Redia Legacy feeds"
type: module
description: "Various legacy feeds, used for the Redia App."
package: DPL
core_version_requirement: ^10

dependencies:
- dpl_opening_hours:dpl_opening_hours
- drupal:drupal_typed
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dpl_redia_legacy.events:
path: '/ding-redia-rss/event'
defaults:
_controller: '\Drupal\dpl_redia_legacy\Controller\RssFeeds\EventsController::getFeed'
_title: 'Redia APP Event'
requirements:
_permission: 'access content'
options:
_format: 'xml'
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php

namespace Drupal\dpl_redia_legacy\Controller\RssFeeds;

use DanskernesDigitaleBibliotek\CMS\Api\Model\EventsGET200ResponseInnerImage;
use DanskernesDigitaleBibliotek\CMS\Api\Service\SerializerInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Image\ImageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\dpl_event\Services\EventRestMapper;
use Drupal\file\FileInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\image\Plugin\Field\FieldType\ImageItem;
use Drupal\media\MediaInterface;
use Drupal\pathauto\AliasCleanerInterface;
use Drupal\recurring_events\Entity\EventInstance;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Drupal\node\Entity\Node;

class EventsController extends ControllerBase {

/**
* {@inheritdoc}
*/
public function __construct(
protected FileUrlGeneratorInterface $fileUrlGenerator,
) {}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): static {
return new static(
$container->get('file_url_generator'),
);
}

public function getFeed() {
$items = $this->getItems();

$rss_content = $this->buildRss($items);

$response = new Response();
$response->setContent($rss_content);
$response->headers->set('Content-Type', 'application/rss+xml');
return $response;
}

private function getItems() {

$storage = $this->entityTypeManager()->getStorage('eventinstance');
$query = $storage->getQuery()
->condition('status', TRUE)
->accessCheck(TRUE)
->sort('date.value');

$ids = $query->execute();

$events = EventInstance::loadMultiple($ids);

$items = [];

foreach ($events as $event) {
/** @var \Drupal\node\NodeInterface[] $branches */
$branches = $event->get('branch')->referencedEntities();
$branch = reset($branches);
$date = $event->get('date')->getValue();

$items[] = [
'title' => $event->label(),
'description' => $this->getEventDescription($event),
'author' => $event->getOwner()->get('field_author_name')->getString(),
'id' => $event->id(),
'date' => $event->getChangedTime(),
'promoted' => FALSE,
'subtitle' => $event->get('event_description')->getString(),
'start_time' => $date[0]['value'] ?? NULL,
'end_time' => $date[0]['end_value'] ?? NULL,
'media' => $this->getEventImageFields($event, 'redia_feed_large'),
'media_thumbnail' => $this->getEventImageFields($event, 'redia_feed_small'),
'branch' => [
'label' => $branch ? $branch->label() : NULL,
'id' => $branch ? $branch->id() : NULL
],
];
}

return $items;
}

private function getEventImageFields(EventInstance $event, string $image_style) {
$media_field = $event->get('event_image');

if (!($media_field instanceof FieldItemListInterface)) {
return NULL;
}

$media = $media_field->referencedEntities()[0] ?? NULL;
$file_field_name = 'field_media_image';

if (!($media instanceof MediaInterface) || !$media->hasField($file_field_name)) {
return NULL;
}

$image = $media->get($file_field_name)->view($image_style)[0]['#item'];

if (!($image instanceof ImageItem)) {
return NULL;
}


if (true) {}

return [
'url' => '',
'size' => '',
'type' => '',
'width' => '',
'height' => '',
'md5' => '',
];
}

private function getEventDescription(EventInstance $event) {
/** @var \Drupal\paragraphs\ParagraphInterface[] $paragraphs */
$paragraphs = $event->get('event_paragraphs')->referencedEntities();

foreach ($paragraphs as $paragraph) {
if ($paragraph->bundle() === 'text_body') {
return $paragraph->get('field_body')->getValue()[0]['value'] ?? NULL;
}
}

return NULL;
}

private function buildRss($items) {

$site_url = "https://www.aakb.dk/";
$feed_url = 'https://www.aakb.dk/ding-redia-rss/event';
$site_title = 'Whatever';
$date = 'Thu, 12 Sep 2024 10:49:51 +0200';

$rss_feed = '<?xml version="1.0" encoding="UTF-8"?>';
$rss_feed .= '<rss version="2.0" xml:base="' . $site_url . '" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:content-rss="http://xml.redia.dk/rss">';
$rss_feed .= '<channel>';
$rss_feed .= "<title>$site_title</title>";
$rss_feed .= '<link>' . \Drupal::request()->getSchemeAndHttpHost() . '</link>';
$rss_feed .= '<language>da</language>';
$rss_feed .= "<pubDate>$date</pubDate>";
$rss_feed .= "<lastBuildDate>$date</lastBuildDate>";







foreach ($items as $item) {
$rss_feed .= '<item>';
$rss_feed .= "<title>{$item['title']}</title>";
$rss_feed .= "<description>{$item['description']}</description>";
$rss_feed .= "<author>{$item['author']}</author>";
$rss_feed .= "<guid isPermaLink=\"false\">{$item['id']}</guid>";
$rss_feed .= "<pubDate>{$item['date']}</pubDate>";
$rss_feed .= "<source url=\"$feed_url\">$site_title</source>";
$rss_feed .= "<media:content url=\"{$item['media']['url']}\" fileSize=\"{$item['media']['size']}\"
type=\"{$item['media']['type']}\" contentmedium=\"image\"
width=\"{$item['media']['width']}\" height=\"{$item['media']['height']}\">
<media:hash algo=\"md5\">{$item['media']['md5']}</media:hash>
</media:content>";

$rss_feed .= "<media:thumbnail url=\"{$item['media_thumbnail']['url']}\"
width=\"{$item['media']['width']}\" height=\"{$item['media']['height']}\" />";


$rss_feed .= "<content-rss:subheadline>{$item['subtitle']}</content-rss:subheadline>";
$rss_feed .= "<content-rss:arrangement-starttime>{$item['start_time']}</content-rss:arrangement-starttime>";
$rss_feed .= "<content-rss:arrangement-endtime>{$item['end_time']}</content-rss:arrangement-endtime>";
$rss_feed .= "<content-rss:arrangement-location>{$item['branch']['label']}</content-rss:arrangement-location>";
$rss_feed .= "<content-rss:library-id>{$item['branch']['id']}</content-rss:library-id>";

$promoted_title = $item['promoted'] ? 'Sandt' : 'Falsk';
$rss_feed .= "<content-rss:promoted>$promoted_title</content-rss:library-id>";

$rss_feed .= '</item>';
}

$rss_feed .= '</channel>';
$rss_feed .= '</rss>';

return $rss_feed;
}
}
Loading

0 comments on commit f2ed819

Please sign in to comment.