Skip to content

Commit e47d4a9

Browse files
authored
Merge pull request #150 from FriendsOfREDAXO/copilot/fix-149
Add optional file attachments field to neues entries
2 parents 5d9bb5a + be3523f commit e47d4a9

File tree

5 files changed

+92
-6
lines changed

5 files changed

+92
-6
lines changed

install/tableset.json

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,23 @@
644644
"table_name": "rex_neues_entry",
645645
"prio": 19,
646646
"type_id": "value",
647+
"type_name": "be_media_preview",
648+
"db_type": "text",
649+
"list_hidden": 1,
650+
"search": 0,
651+
"name": "attachments",
652+
"label": "translate:neues_attachments",
653+
"not_required": "",
654+
"multiple": "1",
655+
"notice": "translate:neues_attachments_notice",
656+
"types": "pdf,doc,docx,xls,xlsx,zip,txt",
657+
"preview": "1",
658+
"category": ""
659+
},
660+
{
661+
"table_name": "rex_neues_entry",
662+
"prio": 20,
663+
"type_id": "value",
647664
"type_name": "html",
648665
"db_type": "none",
649666
"list_hidden": 0,
@@ -655,7 +672,7 @@
655672
},
656673
{
657674
"table_name": "rex_neues_entry",
658-
"prio": 20,
675+
"prio": 21,
659676
"type_id": "value",
660677
"type_name": "datestamp",
661678
"db_type": "datetime",
@@ -671,7 +688,7 @@
671688
},
672689
{
673690
"table_name": "rex_neues_entry",
674-
"prio": 21,
691+
"prio": 22,
675692
"type_id": "value",
676693
"type_name": "be_user",
677694
"db_type": "varchar(191)",
@@ -685,7 +702,7 @@
685702
},
686703
{
687704
"table_name": "rex_neues_entry",
688-
"prio": 22,
705+
"prio": 23,
689706
"type_id": "value",
690707
"type_name": "datestamp",
691708
"db_type": "datetime",
@@ -701,7 +718,7 @@
701718
},
702719
{
703720
"table_name": "rex_neues_entry",
704-
"prio": 23,
721+
"prio": 24,
705722
"type_id": "value",
706723
"type_name": "be_user",
707724
"db_type": "varchar(191)",
@@ -715,7 +732,7 @@
715732
},
716733
{
717734
"table_name": "rex_neues_entry",
718-
"prio": 24,
735+
"prio": 25,
719736
"type_id": "value",
720737
"type_name": "uuid",
721738
"db_type": "varchar(36)",
@@ -729,7 +746,7 @@
729746
},
730747
{
731748
"table_name": "rex_neues_entry",
732-
"prio": 25,
749+
"prio": 26,
733750
"type_id": "value",
734751
"type_name": "showvalue",
735752
"db_type": "varchar(191)",

lang/de_de.lang

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ neues_description = Inhalt
3131
neues_description_notice = Geben Sie den Inhalt Ihres News-Beitrags an.
3232
neues_image = Titelbild
3333
neues_images = Galerie
34+
neues_attachments = Dateianhänge
35+
neues_attachments_notice = Zusätzliche Dateien (z.B. PDFs) die zu diesem Beitrag gehören
3436
neues_canonical_url = Canonical URL
3537
neues_canonical_url_notice = Gibt die Original-URL des News-Beitrags an, falls dieser Beitrag auf mehreren Websites veröffentlicht wurde.
3638

@@ -82,6 +84,8 @@ neues_author_uuid = UUID
8284
# Settings
8385

8486
neues_editor = Editor
87+
neues_show_attachments = Dateianhänge anzeigen
88+
neues_show_attachments_notice = Wenn aktiviert, wird das Feld für Dateianhänge in den Beiträgen angezeigt
8589
neues_external_url_label = Label externer Link
8690
neues_no_entries_placeholder = Platzhalter-Text bei leerer Liste
8791

lang/en_gb.lang

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ neues_description = Content
3030
neues_description_notice = Enter the content of your news post.
3131
neues_image = Cover image
3232
neues_images = Gallery
33+
neues_attachments = File attachments
34+
neues_attachments_notice = Additional files (e.g. PDFs) that belong to this post
3335

3436
neues_external_url = External link
3537
neues_external_url_notice = If there is more information on another website, enter the address of an external website here, e.g. <code>http://www.example.org/foo-bar/</code>
@@ -67,6 +69,8 @@ neues_date_ids = Entries
6769
neues_date_id_notice = News posts assigned to this category
6870

6971
neues_editor = Editor
72+
neues_show_attachments = Show attachments
73+
neues_show_attachments_notice = When enabled, the file attachments field will be displayed in entries
7074

7175
neues_author = Authors
7276
neues_author_name = Name

lib/Entry.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ public function getForm(): rex_yform
7272
if ('textarea' === $e[0] && str_contains($e[5], $suchtext)) {
7373
$e[5] = str_replace($suchtext, rex_config::get('neues', 'editor'), $e[5]);
7474
}
75+
// Handle attachments field visibility
76+
if ('be_media_preview' === $e[0] && isset($e[2]) && 'attachments' === $e[2]) {
77+
$showAttachments = rex_config::get('neues', 'show_attachments', false);
78+
if (!$showAttachments) {
79+
// Remove the field from the form elements
80+
unset($elements[$k]);
81+
}
82+
}
7583
}
7684

7785
return $yform;
@@ -315,6 +323,55 @@ public function setImages(array $images = []): self
315323
return $this;
316324
}
317325

326+
/**
327+
* Gibt die Dateianhänge des Eintrags zurück.
328+
* Returns the attachments of the entry.
329+
*
330+
* @return array<rex_media> Die Dateianhänge des Eintrags oder [], wenn keine Anhänge gesetzt sind. / The attachments of the entry or [] if no attachments are set.
331+
*
332+
* Beispiel / Example:
333+
* $attachments = $entry->getAttachments();
334+
* @api
335+
*/
336+
public function getAttachments(): array
337+
{
338+
if ($this->hasValue('attachments')) {
339+
$attachments = $this->getValue('attachments');
340+
$attachmentFiles = array_filter(explode(',', $attachments));
341+
$result = [];
342+
foreach ($attachmentFiles as $filename) {
343+
$media = rex_media::get($filename);
344+
if ($media) {
345+
$result[] = $media;
346+
}
347+
}
348+
return $result;
349+
}
350+
return [];
351+
}
352+
353+
/**
354+
* Setzt die Dateianhänge des Eintrags.
355+
* Sets the attachments of the entry.
356+
*
357+
* @param array<rex_media|string> $attachments Die neuen Dateianhänge des Eintrags als rex_media Objekte oder Dateinamen. / The new attachments of the entry as rex_media objects or filenames.
358+
*
359+
* @api
360+
*/
361+
public function setAttachments(array $attachments = []): self
362+
{
363+
$filenames = [];
364+
foreach ($attachments as $attachment) {
365+
if ($attachment instanceof rex_media) {
366+
$filenames[] = $attachment->getFileName();
367+
} elseif (is_string($attachment)) {
368+
$filenames[] = $attachment;
369+
}
370+
}
371+
$this->setValue('attachments', implode(',', $filenames));
372+
return $this;
373+
}
374+
318375
/**
319376
* Gibt das Medium des Eintrags zurück.
320377
* Returns the media of the entry.

pages/neues.settings.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
$field->setLabel(rex_i18n::msg('neues_editor'));
2323
$field->setNotice('z.B. <code>class="form-control redactor-editor--default"</code>');
2424

25+
$field = $form->addCheckboxField('show_attachments');
26+
$field->setLabel(rex_i18n::msg('neues_show_attachments'));
27+
$field->setNotice(rex_i18n::msg('neues_show_attachments_notice'));
28+
2529
$fragment = new rex_fragment();
2630
$fragment->setVar('class', 'edit', false);
2731
$fragment->setVar('title', $addon->i18n('neues_settings'), false);

0 commit comments

Comments
 (0)