Skip to content

Commit

Permalink
Fixed saving empty fields for main language
Browse files Browse the repository at this point in the history
  • Loading branch information
rslanzi committed Mar 31, 2021
1 parent 194ebcb commit 5e13208
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 87 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Refactoring
- Tests

## [0.2.3] - 2021-03-31
### Fixed
- Saving empty fields for main language

## [0.2.2] - 2021-03-14
### Changed
- Extract Sluggable target field in a separated component
Expand Down
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<p align="center"><img height="188" width="198" src="https://riccardoslanzi.com/img/nova-translatable.png"></p>
# Laravel Nova Translatable

[![Latest Version on Packagist](https://img.shields.io/packagist/v/rslanzi/nova-translatable.svg?style=flat-square)](https://packagist.org/packages/rslanzi/nova-translatable)
[![Packagist](https://img.shields.io/packagist/l/rslanzi/nova-translatable.svg)]()
[![StyleCI](https://styleci.io/repos/339990782/shield?branch=main)](https://styleci.io/repos/339990782)


This [Laravel Nova](https://nova.laravel.com/) field allows you to manage translated fields with [astrotomic/laravel-translatable](https://github.com/Astrotomic/laravel-translatable).

## Requirements
Expand Down Expand Up @@ -35,7 +41,7 @@ Install the package in a Laravel Nova project via Composer:

```bash
# Install nova-translatable
composer install rslanzi/nova-translatable
composer require rslanzi/nova-translatable

# Publish configuration (optional, but useful for setting default locales)
php artisan vendor:publish --tag="nova-translatable-config"
Expand Down Expand Up @@ -103,7 +109,7 @@ Automatically populate a slug field based on another field. Title in this case.
NovaTranslatable::make('Title')
->sluggable('Slug'),
NovaTranslatableSlug::make('Slug')
->singleLine()
->hideFromIndex(),
```

### Code Field
Expand Down Expand Up @@ -133,6 +139,15 @@ Pull requests are welcome. For major changes, please open an issue first to disc

Please make sure to update tests as appropriate.

## Support the development
**Do you like this project? Support it by donating**

- PayPal: [Donate](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=rslanzi%40gmail%2ecom&lc=CY&item_name=NovaTranslatable&no_note=0&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHostedGuest)

## Security Vulnerabilities

If you discover a security vulnerability within Nova Translatable, please send an e-mail to Riccardo Slanzi at rslanzi@gmail.com. All security vulnerabilities will be promptly addressed.

## License

This project is open-sourced software licensed under the [MIT license](LICENSE.md).
Nova Translatable is free software distributed under the terme of the [MIT license](LICENSE.md).
5 changes: 3 additions & 2 deletions config/ckeditor-field.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

return [
/*
|--------------------------------------------------------------------------------
Expand All @@ -21,8 +22,8 @@
['Link', 'Unlink', 'Anchor'],
'/',
['Format', 'FontSize'],
['Maximize', 'ShowBlocks', '-', 'About']
]
['Maximize', 'ShowBlocks', '-', 'About'],
],
],

/*
Expand Down
8 changes: 4 additions & 4 deletions src/FieldServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Rslanzi\NovaTranslatable;

use Laravel\Nova\Nova;
use Laravel\Nova\Events\ServingNova;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Laravel\Nova\Events\ServingNova;
use Laravel\Nova\Nova;

class FieldServiceProvider extends ServiceProvider
{
Expand All @@ -22,13 +22,13 @@ public function boot()

Nova::serving(function (ServingNova $event) {
Nova::style('nova-translatable', __DIR__.'/../dist/css/field.css');

Nova::script('nova-translatable', __DIR__.'/../dist/js/field.js');
Nova::script('ckeditor', config('nova.ckeditor-field.ckeditor_url', 'https://cdn.ckeditor.com/4.11.3/full-all/ckeditor.js'));
});

$this->publishes([
__DIR__ . '/../config/ckeditor-field.php' => config_path('nova/ckeditor-field.php'),
__DIR__.'/../config/ckeditor-field.php' => config_path('nova/ckeditor-field.php'),
], 'config');
}

Expand Down
44 changes: 26 additions & 18 deletions src/Http/Controllers/SlugController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@ class SlugController
{
protected $options = [
'generateUniqueSlugs' => false,
'maximumLength' => 255,
'slugSeparator' => '-',
'slugLanguage' => 'en',
'maximumLength' => 255,
'slugSeparator' => '-',
'slugLanguage' => 'en',
];

protected $attribute;
protected $model;
protected $slug = '';

protected $updating = false;
protected $initialValue = '';

/**
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*
* @throws \Exception
*
* @return \Illuminate\Http\JsonResponse
*/
public function getSlug(Request $request)
{
Expand All @@ -52,7 +54,7 @@ public function getSlug(Request $request)
if ($request->filled('options')) {
$this->mergeOptions($request->input('options'));
}

if ($this->options['generateUniqueSlugs'] && !$this->model) {
throw new \Exception('Slug model undefined! Use slugModel() on the slug field!');
}
Expand All @@ -69,7 +71,7 @@ protected function getModelOptions(string $modelClass)
return;
}

$model = new $modelClass;
$model = new $modelClass();
$this->model = $modelClass;

if (!method_exists($model, 'getSlugOptions')) {
Expand All @@ -79,16 +81,16 @@ protected function getModelOptions(string $modelClass)
if ($modelSlugOptions = $model->getSlugOptions()) {
$modelOptions = [
'generateUniqueSlugs' => $modelSlugOptions->generateUniqueSlugs,
'maximumLength' => $modelSlugOptions->maximumLength,
'slugSeparator' => $modelSlugOptions->slugSeparator,
'slugLanguage' => $modelSlugOptions->slugLanguage,
'maximumLength' => $modelSlugOptions->maximumLength,
'slugSeparator' => $modelSlugOptions->slugSeparator,
'slugLanguage' => $modelSlugOptions->slugLanguage,
];
$this->mergeOptions($modelOptions);
}
}

/**
* Validate and merge options from array
* Validate and merge options from array.
*
* @param $options
*/
Expand All @@ -100,9 +102,9 @@ protected function mergeOptions($options)

$validator = Validator::make($options, [
'generateUniqueSlugs' => 'boolean',
'maximumLength' => 'numeric',
'slugSeparator' => 'string',
'slugLanguage' => 'string',
'maximumLength' => 'numeric',
'slugSeparator' => 'string',
'slugLanguage' => 'string',
]);

$options = $validator->validate();
Expand All @@ -114,6 +116,7 @@ protected function mergeOptions($options)

/**
* @param string $value
*
* @return \Illuminate\Http\JsonResponse
*/
protected function generateSlug(string $value)
Expand All @@ -126,15 +129,18 @@ protected function generateSlug(string $value)

if (!$this->options['generateUniqueSlugs']) {
$this->slug = $slug;

return $this->sendResponse();
} else {
$this->slug = $this->generateUniqueSlug($slug);

return $this->sendResponse();
}
}

/**
* @param string $originalSlug
*
* @return string
*/
protected function generateUniqueSlug(string $originalSlug): string
Expand All @@ -143,23 +149,24 @@ protected function generateUniqueSlug(string $originalSlug): string
$i = 1;

while ($this->otherRecordExistsWithSlug($slug)) {
$slug = $originalSlug . $this->options['slugSeparator'] . $i++;
$slug = $originalSlug.$this->options['slugSeparator'].$i++;
}

return $slug;
}

/**
* @param string $slug
*
* @return bool
*/
protected function otherRecordExistsWithSlug(string $slug)
{
$initialValue = $this->initialValue;
$modelAttribute = $this->attribute;

$model = $this->model;

return (bool) $model::where($this->attribute, $slug)
->when($this->updating, function ($query) use ($modelAttribute, $initialValue) {
return $query->where($modelAttribute, '!=', $initialValue);
Expand All @@ -169,7 +176,7 @@ protected function otherRecordExistsWithSlug(string $slug)
}

/**
* Send JSON response with slug
* Send JSON response with slug.
*
* @return \Illuminate\Http\JsonResponse
*/
Expand All @@ -182,6 +189,7 @@ protected function sendResponse()

/**
* @param string $error
*
* @return mixed
*/
protected function sendErrorResponse(string $error)
Expand Down
Loading

0 comments on commit 5e13208

Please sign in to comment.