Skip to content

Commit 265463a

Browse files
committed
Add attributes and first body transformation
1 parent d701250 commit 265463a

File tree

4 files changed

+86
-30
lines changed

4 files changed

+86
-30
lines changed

config/webfactor/documents.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'sbt'
1111
],
1212

13-
// define the access to the documents (see backpack for mor information)
13+
// define the access to the documents (see backpack for more information)
1414
'access' => [
1515
// can users list documents
1616
'list' => true,
@@ -31,13 +31,30 @@
3131
// -> will present the simplemde markdown editor and store markdown to the database
3232
'body_type' => 'md',
3333

34+
// define additional field options here (see backpack for more information on this)
35+
'field_editor_attributes' => [
36+
'plaintext' => [],
37+
'html' => "",
38+
'md' => "'renderingConfig': {'singleLineBreaks': true}"
39+
],
40+
3441
// the model to use with this package
3542
// keep in mind: when changing the model, you most likely need to change the Transformer also
3643
'model_class' => \Webfactor\Laravel\Backpack\Documents\Models\Document::class,
3744

3845
// the transformer class used to transform the model for api responses
3946
'transformer_class' => \Webfactor\Laravel\Backpack\Documents\Transformers\DocumentTransformer::class,
4047

48+
// define some options for the transformer
49+
// currently supported:
50+
// * md
51+
// -> single_line_breaks
52+
'transform_options' => [
53+
'md' => [
54+
'single_line_breaks'
55+
]
56+
],
57+
4158
// customize your backend parameters
4259
'backend' => [
4360

src/Controllers/DocumentCrudController.php

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@
88

99
class DocumentCrudController extends CrudController
1010
{
11+
protected $types = [
12+
'plaintext' => [
13+
'field' => 'textarea',
14+
'attrLabel' => 'textareaAttributes'
15+
],
16+
'html' => [
17+
'field' => 'summernote',
18+
'attrLabel' => 'options'
19+
],
20+
'md' => [
21+
'field' => 'simplemde',
22+
'attrLabel' => 'simplemdeAttributesRaw'
23+
],
24+
];
25+
1126
public function setup()
1227
{
1328
/*
@@ -16,7 +31,7 @@ public function setup()
1631
|--------------------------------------------------------------------------
1732
*/
1833
$this->crud->setModel(config('webfactor.documents.model_class'));
19-
$this->crud->setRoute(config('webfactor.documents.backend.route_prefix').'/'.config('webfactor.documents.backend.route'));
34+
$this->crud->setRoute(config('webfactor.documents.backend.route_prefix') . '/' . config('webfactor.documents.backend.route'));
2035
$this->crud->setEntityNameStrings(trans('webfactor::documents.entity_name_singular'),
2136
trans('webfactor::documents.entity_name_plural'));
2237

@@ -29,23 +44,24 @@ public function setup()
2944
// ------ CRUD FIELDS
3045

3146
$this->crud->addField([
32-
'name' => 'type',
33-
'label' => 'Dokumentenart',
34-
'type' => 'select_from_array',
35-
'options' => $this->getTypeOptions(),
36-
'allows_null' => false,
37-
'allows_multiple' => false
47+
'name' => 'type',
48+
'label' => 'Dokumentenart',
49+
'type' => 'select_from_array',
50+
'options' => $this->getTypeOptions(),
51+
'allows_null' => false,
52+
'allows_multiple' => false,
3853
]);
3954

4055
$this->crud->addField([
41-
'name' => 'title',
42-
'label' => trans('webfactor::documents.title')
56+
'name' => 'title',
57+
'label' => trans('webfactor::documents.title'),
4358
]);
44-
59+
4560
$this->crud->addField([
46-
'name' => 'body',
47-
'label' => trans('webfactor::documents.body'),
48-
'type' => $this->bodyFieldType()
61+
'name' => 'body',
62+
'label' => trans('webfactor::documents.body'),
63+
'type' => $this->bodyFieldType(),
64+
$this->bodyFieldAttributesLabel() => $this->bodyFieldAttributes(),
4965
]);
5066

5167
// ------ CRUD COLUMNS
@@ -57,12 +73,12 @@ public function setup()
5773
],
5874
[
5975
'name' => 'title',
60-
'label' => trans('webfactor::documents.title')
76+
'label' => trans('webfactor::documents.title'),
6177
],
6278
[
6379
'name' => 'body',
64-
'label' => trans('webfactor::documents.body')
65-
]
80+
'label' => trans('webfactor::documents.body'),
81+
],
6682
]);
6783

6884
// ------ CRUD BUTTONS
@@ -95,19 +111,24 @@ public function getTypeOptions()
95111
$types = collect(config('webfactor.documents.types'));
96112

97113
return $types->mapWithKeys(function ($type) {
98-
return [$type => trans('webfactor::documents.types.'.$type)];
114+
return [$type => trans('webfactor::documents.types.' . $type)];
99115
});
100116
}
101117

102118
public function bodyFieldType()
103119
{
104-
$types = [
105-
'plaintext' => 'textarea',
106-
'html' => 'summernote',
107-
'md' => 'simplemde'
108-
];
120+
return $this->types[config('webfactor.documents.body_type', 'plaintext')]['field'];
121+
}
122+
123+
public function bodyFieldAttributesLabel()
124+
{
125+
return $this->types[config('webfactor.documents.body_type', 'plaintext')]['attrLabel'];
126+
}
109127

110-
return $types[config('webfactor.documents.body_type', 'plaintext')];
128+
public function bodyFieldAttributes()
129+
{
130+
$type = config('webfactor.documents.body_type', 'plaintext');
131+
return config('webfactor.documents.field_editor_attributes.'.$type, []);
111132
}
112133

113134
public function store(StoreRequest $request)

src/DocumentsServiceProvider.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ public function boot()
2727

2828
$this->loadTranslationsFrom(realpath(__DIR__.'/../resources/lang'), 'webfactor');
2929

30-
$this->mergeConfigFrom(
31-
__DIR__.'/../config/webfactor/documents.php', 'webfactor.documents'
32-
);
33-
3430
$this->publishFiles();
3531
}
3632

@@ -41,7 +37,9 @@ public function boot()
4137
*/
4238
public function register()
4339
{
44-
//
40+
$this->mergeConfigFrom(
41+
__DIR__.'/../config/webfactor/documents.php', 'webfactor.documents'
42+
);
4543
}
4644

4745
public function publishFiles()

src/Transformers/DocumentTransformer.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,33 @@
77

88
class DocumentTransformer extends TransformerAbstract
99
{
10+
protected $defaultIncludes = ['body'];
11+
1012
public function transform(Document $document)
1113
{
1214
return [
1315
'type' => $document->type,
1416
'title' => $document->title,
15-
'body' => $document->body,
1617
'updatedAt' => $document->updated_at->toIso8601String()
1718
];
1819
}
20+
21+
public function includeBody(Document $document) {
22+
$type = config('webfactor.documents.body_type');
23+
$options = config('webfactor.documents.transform_options.'.$type, []);
24+
$options = is_array($options)? $options : [$options];
25+
26+
$body = $document->body;
27+
28+
foreach($options as $option) {
29+
$body = method_exists($this, $option) ? $this->{$option}($body) : $body;
30+
}
31+
32+
return $this->primitive($body);
33+
}
34+
35+
// body transformations
36+
protected function single_line_breaks($body) {
37+
return str_replace("\r\n", " \r\n", $body);
38+
}
1939
}

0 commit comments

Comments
 (0)