Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v1.x' into v1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldwight committed Jan 4, 2024
2 parents 8d9e933 + 5c9da95 commit e98f46e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
21 changes: 12 additions & 9 deletions src/Models/Behaviours/HasMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
namespace CwsDigital\TwillMetadata\Models\Behaviours;

use A17\Twill\Facades\TwillAppSettings;
use A17\Twill\Models\Behaviors\HasBlocks;
use A17\Twill\Models\Behaviors\HasMedias;
use CwsDigital\TwillMetadata\Models\Metadata;

trait HasMetadata
{
public bool $hasMetadata = true;

public function metadata()
{
return $this->morphOne('CwsDigital\TwillMetadata\Models\Metadata', 'meta_describable');
return $this->morphOne(Metadata::class, 'meta_describable');
}

public function getSocialImageAttribute()
Expand All @@ -22,9 +25,9 @@ public function getSocialImageAttribute()
} elseif ($this->hasAnyImages()) {
return $this->getDefaultMetadataFallbackImage();
} else {
$hasMediaImage = TwillAppSettings::getGroupDataForSectionAndName('seo','metadata')->hasImage('default_social_image', 'default');
$hasMediaImage = TwillAppSettings::getGroupDataForSectionAndName('seo', 'metadata')->hasImage('default_social_image', 'default');
if ($hasMediaImage) {
return TwillAppSettings::getGroupDataForSectionAndName('seo','metadata')->image('default_social_image', 'default');
return TwillAppSettings::getGroupDataForSectionAndName('seo', 'metadata')->image('default_social_image', 'default');
}
}
}
Expand All @@ -33,10 +36,10 @@ public function hasSpecifiedMetaFallbackImage($key)
{
if (array_key_exists($key, $this->metadataFallbacks)) {
return
! empty($this->metadataFallbacks[$key]) &&
is_array($this->metadataFallbacks[$key]) &&
array_key_exists('role', $this->metadataFallbacks[$key]) &&
array_key_exists('crop', $this->metadataFallbacks[$key]);
! empty($this->metadataFallbacks[$key])
&& is_array($this->metadataFallbacks[$key])
&& array_key_exists('role', $this->metadataFallbacks[$key])
&& array_key_exists('crop', $this->metadataFallbacks[$key]);
} else {
return false;
}
Expand Down Expand Up @@ -90,14 +93,14 @@ public function usesTrait($trait)

public function hasAnyMedias()
{
$hasMedias = $this->usesTrait('A17\Twill\Models\Behaviors\HasMedias');
$hasMedias = $this->usesTrait(HasMedias::class);

return $hasMedias ? $this->medias()->count() : 0;
}

public function hasAnyBlockMedias()
{
$hasBlocks = $this->usesTrait('A17\Twill\Models\Behaviors\HasBlocks');
$hasBlocks = $this->usesTrait(HasBlocks::class);

return $hasBlocks ? $this->blocks()->has('medias')->count() : 0;
}
Expand Down
48 changes: 27 additions & 21 deletions src/Repositories/Behaviours/HandleMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@

namespace CwsDigital\TwillMetadata\Repositories\Behaviours;

use A17\Twill\Models\Model;
use A17\Twill\Models\Contracts\TwillModelContract;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;

trait HandleMetadata
{
// Prefix for metadata fields in form
protected $metadataFieldPrefix = 'metadata';
protected string $metadataFieldPrefix = 'metadata';

// Fields with fixed default values that we want persisting to store if blank
// N.B. this does not include those fields that fallback to another field when blank
protected $withDefaultValues = ['card_type', 'og_type'];
protected array $withDefaultValues = ['card_type', 'og_type'];

/**
* Handle saving of metadata fields from form submission
* Handle saving of metadata fields from form submission.
*
* @param Model $object
* @param array $fields
* @param \A17\Twill\Models\Contracts\TwillModelContract $object
* @param array $fields
*/
public function afterSaveHandleMetadata(Model $object, array $fields)
public function afterSaveHandleMetadata(TwillModelContract $object, array $fields)
{
// Due to the way twill handles adding data to VueX store
// metadata will come through in individual fields metadata[title]... not in an array
Expand All @@ -37,15 +37,16 @@ public function afterSaveHandleMetadata(Model $object, array $fields)
}

/**
* Prepares the metadata fields for the admin form view
* Prepares the metadata fields for the admin form view.
*
* @param \A17\Twill\Models\Contracts\TwillModelContract $object
* @param array $fields
*
* @param Model $object
* @param array $fields
* @return array
*/
public function getFormFieldsHandleMetadata(Model $object, array $fields)
public function getFormFieldsHandleMetadata(TwillModelContract $object, array $fields)
{
//If the metadata object doesn't exist create it. Every 'meta_describable' will need one entry.
// If the metadata object doesn't exist create it. Every 'meta_describable' will need one entry.
$metadata = $object->metadata ?? $object->metadata()->create();

$metadata = $this->setFieldDefaults($object, $metadata);
Expand All @@ -66,9 +67,10 @@ public function getFormFieldsHandleMetadata(Model $object, array $fields)

/**
* Filters the full fields array down to just the metadata fields
* removes the field prefix and sets the keys correctly for persisting to store
* removes the field prefix and sets the keys correctly for persisting to store.
*
* @param array $fields
*
* @param array $fields
* @return array
*/
protected function getMetadataFields(array $fields)
Expand All @@ -86,13 +88,14 @@ protected function getMetadataFields(array $fields)
}

/**
* Set default values on fields that require it
* Set default values on fields that require it.
*
* @param \A17\Twill\Models\Contracts\TwillModelContract $object
* @param array $fields
*
* @param Model $object
* @param array $fields
* @return array
*/
protected function setFieldDefaults(Model $object, $fields)
protected function setFieldDefaults(TwillModelContract $object, $fields)
{
foreach ($this->withDefaultValues as $fieldName) {
if (empty($fields[$fieldName])) {
Expand All @@ -105,11 +108,14 @@ protected function setFieldDefaults(Model $object, $fields)
}

/**
* @param $key
* Determine if the field belongs to the metadata.
*
* @param string $key
*
* @return bool
*/
protected function isMetadataField($key)
protected function isMetadataField(string $key)
{
return substr($key, 0, strlen($this->metadataFieldPrefix)) === $this->metadataFieldPrefix;
return Str::startsWith($key, $this->metadataFieldPrefix);
}
}

0 comments on commit e98f46e

Please sign in to comment.