123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <?php
- const PARAGRAPH = 'audio';
- const LINKFIELD = 'field_audio_link';
- function isArchive(string $url): bool {
- $pos = strpos($url, "archive.org/");
- if($pos === FALSE) {
- return FALSE;
- }
- return TRUE;
- }
- function normalizeArchive(string $url) {
- //assume il link sia di archive
- if(strpos($url, "https://archive.org/download") !== FALSE) {
- return $url;
- }
- if(strpos($url, "https://archive.org/details") !== FALSE) {
- // aagh qui in teoria non c'e' nulla da fare a meno che non guardiamo sul sito
- return $url;
- }
- // https://ia123456.us.archive.org/30/items/Bucket/filename.ogg
- $parts = explode("/", $url);
- $bucket = $parts[count($parts)-2];
- $fname = $parts[count($parts)-1];
- return "https://archive.org/download/" . $bucket . "/" . $fname;
- }
- /*
- * Implements hook_ENTITY_TYPE_presave
- */
- function audiofix_ror_paragraph_presave(Drupal\Core\Entity\EntityInterface $entity) {
- // per i paragraph, getType() === bundle().
- if($entity->bundle() !== PARAGRAPH) { // audio e' il nome del nostro paragraph type
- return;
- }
- // campi utili: field_audio_link e field_durata
- $link = $entity->get(LINKFIELD)->getValue();
- if(count($link) == 0) { return; }
- $link = $link[0]['uri'];
- if(is_string($link) && isArchive($link)) {
- $norm = normalizeArchive($link);
- if($norm !== $link) {
- \Drupal::logger('audiofix_ror')->notice('Aggiornato da :old a :new', array(":old" => $link, ":new" => $norm));
- $entity->set(LINKFIELD, $norm);
- }
- }
- }
|