Skip to content

Commit

Permalink
Basic implementation that changes the Blocks formwidget to extend MLR…
Browse files Browse the repository at this point in the history
…epeater and customizes the getLocaleSaveData to add the _group and _config to the save data. Also changes all blocks to use the Translate version (ml*)
  • Loading branch information
nmiyazaki-chapleau committed Jun 4, 2024
1 parent 4b7d476 commit 61a78dc
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 11 deletions.
2 changes: 1 addition & 1 deletion blocks/button.block
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fields:
label:
label: winter.blocks::lang.fields.label
span: full
type: text
type: mltext
tabs:
icons:
winter.blocks::lang.fields.actions: 'icon-arrow-pointer'
Expand Down
4 changes: 2 additions & 2 deletions blocks/image.block
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ icon: icon-picture-o
tags: ["pages"]
fields:
image:
type: mediafinder
type: mlmediafinder
span: full
mode: image
alt_text:
label: winter.blocks::lang.blocks.image.alt_text
span: full
type: text
type: mltext
config:
size:
label: winter.blocks::lang.fields.size
Expand Down
2 changes: 1 addition & 1 deletion blocks/plaintext.block
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fields:
content:
placeholder: winter.blocks::lang.fields.content
span: full
type: textarea
type: mltextarea
size: small
==
<p>
Expand Down
2 changes: 1 addition & 1 deletion blocks/richtext.block
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fields:
content:
placeholder: winter.blocks::lang.fields.content
span: full
type: richeditor
type: mlricheditor
==
<div class="prose lg:prose-xl">
{{ content | raw }}
Expand Down
2 changes: 1 addition & 1 deletion blocks/title.block
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fields:
content:
placeholder: winter.blocks::lang.blocks.title.name
span: full
type: text
type: mltext
config:
size:
label: winter.blocks::lang.fields.size
Expand Down
2 changes: 1 addition & 1 deletion blocks/video.block
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fields:
video:
label: winter.blocks::lang.blocks.video.name
span: full
type: mediafinder
type: mlmediafinder
mode: video
==
{% if video %}
Expand Down
2 changes: 1 addition & 1 deletion blocks/vimeo.block
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ tags: ["pages"]
fields:
vimeo_id:
label: winter.blocks::lang.blocks.vimeo.vimeo_id
type: text
type: mltext
==
{% if vimeo_id %}
<iframe
Expand Down
2 changes: 1 addition & 1 deletion blocks/youtube.block
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ tags: ["pages"]
fields:
youtube_id:
label: winter.blocks::lang.blocks.youtube.youtube_id
type: text
type: mltext
==
{% if youtube_id %}
<iframe
Expand Down
47 changes: 45 additions & 2 deletions formwidgets/Blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace Winter\Blocks\FormWidgets;

use Backend\FormWidgets\Repeater;
use Lang;
use Winter\Blocks\Classes\BlockManager;
use Winter\Storm\Exception\ApplicationException;
use Winter\Translate\FormWidgets\MLRepeater;
use Winter\Translate\Models\Locale;

/**
* "Blocks" FormWidget for defining and managing multiple blocks
*/
class Blocks extends Repeater
class Blocks extends MLRepeater
{
/**
* List of blocks to ignore for this specific instance
Expand Down Expand Up @@ -370,4 +371,46 @@ protected function getBestInspectorField(string $type): string

return $type;
}

/**
* Returns an array of translated values for this field
* @return array
*/
public function getLocaleSaveData()
{
$values = [];
$data = post('RLTranslate');

if (!is_array($data)) {
if ($this->translationMode === 'fields') {
foreach (Locale::listEnabled() as $code => $name) {
// force translations removal from db
$values[$code] = [];
}
}
return $values;
}

$fieldName = $this->getLongFieldName();
$isJson = $this->isLocaleFieldJsonable();

foreach ($data as $locale => $_data) {
$i = 0;
$content = array_get($_data, $fieldName);
if (is_array($content)) {
foreach ($content as $index => $value) {
// we reindex to fix item reordering index issues
// This will add the _group & _config data with the localized content
$values[$locale][$i++] = array_replace_recursive(
json_decode(json_encode($this->formWidgets[$i - 1]->data), true),
$value
);
}
} else {
$values[$locale] = $isJson && is_string($content) ? json_decode($content, true) : $content;
}
}

return $values;
}
}

0 comments on commit 61a78dc

Please sign in to comment.