Skip to content

Commit

Permalink
Merge pull request #12 from workupsrl/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
rslanzi authored Feb 8, 2024
2 parents aea4025 + 05f5f51 commit 9631a26
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# CHANGELOG

## [Unreleased]

## [4.2.0] - 2024-02-08
### Added
- Translatable SEO attributes for images

## [4.1.7] - 2023-11-06
### Fixed
- Nova 4 issues
9 changes: 5 additions & 4 deletions src/Fields/HandlesCustomPropertiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
trait HandlesCustomPropertiesTrait
{
protected $customPropertiesFields = [];

protected $customProperties = [];

public function customPropertiesFields(array $customPropertiesFields): self
Expand All @@ -32,7 +33,6 @@ public function customProperties(array $customProperties): self
}

/**
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param string $requestAttribute The form attribute of the media field.
* @param \Spatie\MediaLibrary\HasMedia $model The model which has associated media.
* @param string $collection The selected media collection.
Expand Down Expand Up @@ -66,7 +66,6 @@ private function fillCustomPropertiesFromRequest(NovaRequest $request, string $r
/**
* Fills custom properties for a given Media model from the request.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Spatie\MediaLibrary\MediaCollections\Models\Media $media The Media model with custom properties.
* @param int $index The file's index in the corresponding Media collection, to retrieve its custom properties from the request.
* @param string $collection The selected media collection.
Expand All @@ -77,17 +76,19 @@ private function fillMediaCustomPropertiesFromRequest(NovaRequest $request, Medi
// prevent overriding the custom properties set by other processes like generating conversions
$media->refresh();

$originalRequestAttribute = $requestAttribute;

/** @var Field $field */
foreach ($this->customPropertiesFields as $field) {
// If we are dealing with nested resources or multiple panels, custom property fields are prefixed.
$key = str_replace($collection, '__media-custom-properties__.'.$collection, $requestAttribute);
$key = str_replace($collection, '__media-custom-properties__.'.$collection, $originalRequestAttribute);
$targetAttribute = "custom_properties->{$field->attribute}";
$requestAttribute = "{$key}.{$index}.{$field->attribute}";

$field->fillInto($request, $media, $targetAttribute, $requestAttribute);
}

if($this->translatable) {
if ($this->translatable) {
$media['custom_properties->locale'] = $request["__media-custom-properties__.{$collection}.{$index}.locale"];
}

Expand Down
2 changes: 2 additions & 0 deletions src/Fields/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public function __construct($name, $attribute = null, callable $resolveCallback
parent::__construct($name, $attribute, $resolveCallback);

$this->croppable(config('nova-media-library.default-croppable', true));

$this->seoAttributes();
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/Fields/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\FileAdder;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Workup\NovaTranslatableField\NovaTranslatableField;


class Media extends Field
Expand Down Expand Up @@ -421,4 +422,21 @@ private function makeMediaFromVaporUpload(array $file, HasMedia $model): FileAdd
->usingFilename($file['file_name'])
->withCustomProperties($this->customProperties);
}

/**
* Set SEO attributes for the element.
*
* @return $this
*/
public function seoAttributes(): self
{
$this->customPropertiesFields([
NovaTranslatableField::make(__('Alt attribute'), 'alt_attribute')
->singleLine(),
NovaTranslatableField::make(__('Title attribute'), 'title_attribute')
->singleLine()
]);

return $this;
}
}

0 comments on commit 9631a26

Please sign in to comment.