Skip to content

Commit c94686d

Browse files
committed
Added option for other content fields
1 parent 3979412 commit c94686d

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

index.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
'action' => function () {
1010

1111
$prefix = option('splorp.paperback-export.prefix', '');
12+
$fields = option('splorp.paperback-export.fields', []);
1213
$includeUnlisted = option('splorp.paperback-export.includeUnlisted', true);
1314
$includeChildren = option('splorp.paperback-export.includeChildren', []);
1415
$excludeTemplate = option('splorp.paperback-export.excludeTemplate', []);
1516

1617
if (! is_string($prefix)) {
1718
throw new Exception('The option “splorp.paperback-export.prefix” must be a string.');
1819
}
20+
if (! is_array($fields)) {
21+
throw new Exception('The option “splorp.paperback-export.fields” must be an array.');
22+
}
1923
if (! is_bool($includeUnlisted)) {
2024
throw new Exception('The option “splorp.paperback-export.includeUnlisted” must be a boolean.');
2125
}
@@ -51,7 +55,7 @@
5155
$pages = $pages->filterBy('intendedTemplate', 'not in', $excludeTemplate);
5256

5357
$template = __DIR__ . '/snippets/export.php';
54-
$paperback = tpl::load($template, compact('languages', 'pages', 'title', 'description', 'prefix', 'version', 'datestamp', 'filename'));
58+
$paperback = tpl::load($template, compact('languages', 'pages', 'title', 'description', 'prefix', 'fields', 'version', 'datestamp', 'filename'));
5559

5660
return new response($paperback, 'txt');
5761
}

readme.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,33 @@ return [
6565
];
6666
```
6767

68-
### Include unlisted pages
68+
### Include Other Content Fields
6969

70-
By default, text located in the `$page->title()` and `$page->text()` fields for every page on your Kirby site will be included in the exported data. The following options can be added to the `site/config/config.php` file, allowing you to filter which pages are included based on certain criteria.
70+
By default, text located in the `$page->title()` and `$page->text()` fields will be included in the exported data. The following option can be added to the `site/config/config.php` file, allowing you to specify other content fields to be included. These fields will be appended after the title and text fields.
71+
72+
This option is not set by default.
73+
74+
```php
75+
return [
76+
'splorp.paperback-export.fields' => [],
77+
];
78+
```
79+
80+
Specify one or more content fields and their type as an array.
81+
82+
```php
83+
return [
84+
'splorp.paperback-export.fields' => ['author' => 'text','posts' => 'related'],
85+
];
86+
```
87+
88+
Use the `text` content type for any field containing alphanumberic data, such as an author name or date.
89+
90+
Use the `related` content type for fields formatted using the YAML syntax for [related articles](https://getkirby.com/docs/cookbook/content/related-articles). The title field of each related article or page will be included in the exported data.
91+
92+
### Include Unlisted Pages
93+
94+
By default, every page on your Kirby site will be included in the exported data. The following options can be added to the `site/config/config.php` file, allowing you to filter which pages are included based on certain criteria.
7195

7296
This option is set to true by default.
7397

snippets/content.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,22 @@
1010
// Remove all remaining tags
1111
$buffer = html_entity_decode(strip_tags($buffer));
1212
echo $buffer . PHP_EOL . PHP_EOL;
13-
if($page->source()->exists()) {
14-
if($page->source()->toPages()->count() > 1) { echo 'Sources: ' . PHP_EOL; } else { echo 'Source: '; }
15-
$n=0; foreach($page->source()->toPages() as $source): $n++;
16-
echo $source->title() . PHP_EOL;
17-
endforeach;
18-
echo PHP_EOL;
19-
}
13+
// Determine other content fields
14+
foreach ($fields as $fieldname => $fieldtype) :
15+
if($page->content()->get($fieldname)->isNotEmpty()) {
16+
echo ucwords($fieldname) . ': ';
17+
if($fieldtype == 'related') {
18+
$n = false;
19+
foreach($page->content()->get($fieldname)->toPages() as $fieldpage):
20+
if ($n) : echo ', ';
21+
endif;
22+
echo $fieldpage->title();
23+
$n = true;
24+
endforeach;
25+
echo PHP_EOL . PHP_EOL;
26+
} else {
27+
echo $page->content()->get($fieldname) . PHP_EOL . PHP_EOL;
28+
}
29+
}
30+
endforeach;
2031
?>

snippets/export.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
if ($version != '') { echo ('Version ' . $version . ' (' . $datestamp . ')'); } else { echo ('Published ' . $datestamp); }
77
echo (PHP_EOL . PHP_EOL);
88
foreach ($pages as $page) :
9-
snippet('paperback-export/content', compact('languages', 'page', 'prefix'));
9+
snippet('paperback-export/content', compact('languages', 'page', 'prefix', 'fields'));
1010
endforeach;
1111
?>

0 commit comments

Comments
 (0)