Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Key Value (one-dimensional JSON object) to Formfields #5872

Open
wants to merge 6 commits into
base: 1.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions publishable/lang/en/generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'add' => 'Add',
'add_folder' => 'Add Folder',
'add_new' => 'Add New',
'add_row' => 'Add Row',
'all_done' => 'All done',
'are_you_sure' => 'Are you sure',
'are_you_sure_delete' => 'Are you sure you want to delete',
Expand Down Expand Up @@ -81,6 +82,7 @@
'required' => 'Required',
'return_to_list' => 'Return to List',
'route' => 'Route',
'row' => 'Row',
'save' => 'Save',
'search' => 'Search',
'select_all' => 'Select All',
Expand All @@ -105,6 +107,7 @@
'updated_order' => 'Order updated',
'upload' => 'Upload',
'url' => 'URL',
'value' => 'Value',
'view' => 'View',
'viewing' => 'Viewing',
'whoopsie' => 'Whoopsie!',
Expand Down
3 changes: 3 additions & 0 deletions publishable/lang/pt/generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'add' => 'Adicionar',
'add_folder' => 'Adicionar Pasta',
'add_new' => 'Adicionar',
'add_row' => 'Adicionar Linha',
'all_done' => 'Concluído',
'are_you_sure' => 'Tem certeza',
'are_you_sure_delete' => 'Tem certeza de que deseja remover',
Expand Down Expand Up @@ -63,6 +64,7 @@
'required' => 'Requerido',
'return_to_list' => 'Voltar à lista',
'route' => 'Rota',
'row' => 'Linha',
'save' => 'Guardar',
'search' => 'Procurar',
'select_all' => 'Selecione Todos',
Expand All @@ -81,6 +83,7 @@
'update_failed' => 'atualização falhou',
'upload' => 'Upload',
'url' => 'URL',
'value' => 'Valor',
'view' => 'Ver',
'viewing' => 'Visualizando',
'yes' => 'Sim',
Expand Down
3 changes: 3 additions & 0 deletions publishable/lang/pt_br/generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'add' => 'Adicionar',
'add_folder' => 'Adicionar Pasta',
'add_new' => 'Adicionar',
'add_row' => 'Adicionar Linha',
'all_done' => 'Concluído',
'are_you_sure' => 'Tem certeza',
'are_you_sure_delete' => 'Tem certeza de que deseja remover',
Expand Down Expand Up @@ -95,13 +96,15 @@
'title' => 'Título',
'type' => 'Tipo',
'restore' => 'Restaurar',
'row' => 'Linha',
'unsigned' => 'Não assinado',
'unstick_sidebar' => 'Descolar a barra lateral',
'update' => 'Atualizar',
'update_failed' => 'Atualização falhou',
'updated_order' => 'Ordem atualizada',
'upload' => 'Upload',
'url' => 'URL',
'value' => 'Valor',
'view' => 'Ver',
'viewing' => 'Visualizando',
'whoopsie' => 'Oops!',
Expand Down
12 changes: 12 additions & 0 deletions resources/views/bread/browse.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@
<div>{{ mb_strlen( strip_tags($data->{$row->field}, '<b><i><u>') ) > 200 ? mb_substr(strip_tags($data->{$row->field}, '<b><i><u>'), 0, 200) . ' ...' : strip_tags($data->{$row->field}, '<b><i><u>') }}</div>
@elseif($row->type == 'coordinates')
@include('voyager::partials.coordinates-static-image')
@elseif($row->type == 'key_value')
@php
$currentKeyValue = isset($data->{$row->field}) ? $data->{$row->field} : '{}';
$currentKeyValue = json_decode($currentKeyValue, true);
@endphp
@if(isset($currentKeyValue) && (count($currentKeyValue)))
<ul>
@foreach($currentKeyValue as $key => $value)
<li>{{ $key }}: {{ $value }}</li>
@endforeach
</ul>
@endif
@elseif($row->type == 'multiple_images')
@php $images = json_decode($data->{$row->field}); @endphp
@if($images)
Expand Down
12 changes: 12 additions & 0 deletions resources/views/bread/read.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@
@elseif($row->type == "image")
<img class="img-responsive"
src="{{ filter_var($dataTypeContent->{$row->field}, FILTER_VALIDATE_URL) ? $dataTypeContent->{$row->field} : Voyager::image($dataTypeContent->{$row->field}) }}">
@elseif($row->type == 'key_value')
@php
$currentKeyValue = isset($dataTypeContent->{$row->field}) ? $dataTypeContent->{$row->field} : '{}';
$currentKeyValue = json_decode($currentKeyValue, true);
@endphp
@if(isset($currentKeyValue) && (count($currentKeyValue)))
<ul>
@foreach($currentKeyValue as $key => $value)
<li>{{ $key }}: {{ $value }}</li>
@endforeach
</ul>
@endif
@elseif($row->type == 'multiple_images')
@if(json_decode($dataTypeContent->{$row->field}))
@foreach(json_decode($dataTypeContent->{$row->field}) as $file)
Expand Down
94 changes: 94 additions & 0 deletions resources/views/formfields/key_value.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<br>
@php
$currentKeyValue = isset($dataTypeContent->{$row->field}) ? $dataTypeContent->{$row->field} : '{}';
$currentKeyValue = json_decode($currentKeyValue, true);
@endphp
<div>
<div id="keyValue-{{ $row->field }}">
<input type="hidden" id="key-value-{{ $row->field }}" name="{{ $row->field }}" data-name="{{ $row->display_name }}">
<div id="keyValueContainer-{{ $row->field }}">
<table class="table table-bordered key-value-table">
<thead>
<tr>
<th>{{ __('voyager::generic.key') }}</th>
<th>{{ __('voyager::generic.value') }}</th>
<th>{{ __('voyager::generic.action') }}</th>
</tr>
</thead>
<tbody>
@if(isset($currentKeyValue) && (count($currentKeyValue)))
@foreach($currentKeyValue as $key => $value)
<tr>
<td contenteditable="true">{{ $key }}</td>
<td contenteditable="true">{{ $value }}</td>
<td><button type="button" class="btn btn-danger btn-sm key-value-delete-row-{{ $row->field }}">{{ __('voyager::generic.delete') }}</button></td>
</tr>
@endforeach
@else
<tr>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td><button type="button" class="btn btn-danger btn-sm key-value-delete-row-{{ $row->field }}">{{ __('voyager::generic.delete') }}</button></td>
</tr>
@endif
</tbody>
</table>
</div>
<button type="button" class="btn btn-success" onclick="keyValueAddRow{{ $row->field }}()">{{ __('voyager::generic.add_row') }}</button>
</div>
</div>

<script>
// Function to add a new row
function keyValueAddRow{{ $row->field }}() {
const table = document.querySelector('#keyValueContainer-{{ $row->field }} .key-value-table tbody');
const newRow = table.insertRow();
newRow.innerHTML = `
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td><button type="button" class="btn btn-danger btn-sm key-value-delete-row-{{ $row->field }}">{{ __('voyager::generic.delete') }}</button></td>
`;
attachCellEventListeners{{ $row->field }}(newRow.cells[0]); // Attach event listener to the new key cell
attachCellEventListeners{{ $row->field }}(newRow.cells[1]); // Attach event listener to the new value cell
updateJson{{ $row->field }}(); // Update JSON immediately after adding a row
}

// Function to update JSON data
function updateJson{{ $row->field }}() {
const json = {};
const table = document.querySelector('#keyValueContainer-{{ $row->field }} .key-value-table tbody');

// Iterate through rows to construct JSON object
table.querySelectorAll('tr').forEach(row => {
const key = row.cells[0].innerText.trim();
const value = row.cells[1].innerText.trim();
if (key !== '' && value !== '') {
json[key] = value;
}
});

// Update hidden input value with JSON string
document.getElementById('key-value-{{ $row->field }}').value = JSON.stringify(json, null, 2);
}

// Function to attach event listeners to editable cells
function attachCellEventListeners{{ $row->field }}(cell) {
cell.addEventListener('input', updateJson{{ $row->field }});
}

// Event listener for dynamically added delete buttons
document.addEventListener('click', function (event) {
if (event.target.classList.contains('key-value-delete-row-{{ $row->field }}')) {
const row = event.target.closest('tr');
row.parentNode.removeChild(row);
updateJson{{ $row->field }}();
}
});

// Attach initial event listeners to existing cells
document.querySelectorAll('#keyValueContainer-{{ $row->field }} .key-value-table tbody tr td').forEach(cell => {
attachCellEventListeners{{ $row->field }}(cell);
});

updateJson{{ $row->field }}();
</script>
18 changes: 18 additions & 0 deletions src/FormFields/KeyValueHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace TCG\Voyager\FormFields;

class KeyValueHandler extends AbstractHandler
{
protected $codename = 'key_value';

public function createContent($row, $dataType, $dataTypeContent, $options)
{
return view('voyager::formfields.key_value', [
'row' => $row,
'options' => $options,
'dataType' => $dataType,
'dataTypeContent' => $dataTypeContent,
]);
}
}
14 changes: 14 additions & 0 deletions src/Http/Controllers/ContentTypes/KeyValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace TCG\Voyager\Http\Controllers\ContentTypes;

class KeyValue extends BaseType
{
/**
* @return string
*/
public function handle()
{
return $this->request->input($this->row->field) ?? '{}';
}
}
4 changes: 4 additions & 0 deletions src/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use TCG\Voyager\Http\Controllers\ContentTypes\Coordinates;
use TCG\Voyager\Http\Controllers\ContentTypes\File;
use TCG\Voyager\Http\Controllers\ContentTypes\Image as ContentImage;
use TCG\Voyager\Http\Controllers\ContentTypes\KeyValue;
use TCG\Voyager\Http\Controllers\ContentTypes\MultipleCheckbox;
use TCG\Voyager\Http\Controllers\ContentTypes\MultipleImage;
use TCG\Voyager\Http\Controllers\ContentTypes\Password;
Expand Down Expand Up @@ -264,6 +265,9 @@ public function getContentBasedOnType(Request $request, $slug, $row, $options =
/********** CHECKBOX TYPE **********/
case 'checkbox':
return (new Checkbox($request, $slug, $row, $options))->handle();
/********** KEY VALUE TYPE **********/
case 'key_value':
return (new KeyValue($request, $slug, $row, $options))->handle();
/********** MULTIPLE CHECKBOX TYPE **********/
case 'multiple_checkbox':
return (new MultipleCheckbox($request, $slug, $row, $options))->handle();
Expand Down
1 change: 1 addition & 0 deletions src/VoyagerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ protected function registerFormFields()
'date',
'file',
'image',
'key_value',
'multiple_images',
'media_picker',
'number',
Expand Down