Skip to content

Commit

Permalink
Update HasMeta entity to correct errors (#440)
Browse files Browse the repository at this point in the history
* Update HasMeta entity to correct errors
preventing it working with custom classes

* Make property $configClass non-public;
Make syncMeta() deal gracefully with situations when meta array is not set
  • Loading branch information
dgvirtual authored Mar 20, 2024
1 parent 1537942 commit 0bd06e1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
9 changes: 6 additions & 3 deletions docs/users_and_security/user_meta.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ $post = [
$user->syncMeta($post);
```

**metaValidationRules(string $configClass, string $prefix=null)**
**metaValidationRules(string $prefix=null)**

This examines the specified config file and returns an array with the names of each field and their validation rules,
ready to be used within CodeIgniter's validation library. If your form groups the name as an array, (like `meta[website_url]`)
you may specify the prefix to append to the field names so that validation will pick it up properly.

```php
$rules = $user->metaValidationRules('\Config\Users', 'meta');
$rules = $user->metaValidationRules('meta');

var_dump($rules);

Expand All @@ -148,7 +148,8 @@ The meta solution is flexible enough to be used outside of Users in your own cla
required to make that work.

1. Create a Config file. This should include a `$metaFields` array, formatted as described above.
2. Add the `HasMeta` trait to the Entity class that represents your resource.
2. Add the `HasMeta` trait and a protected `$configClass` property with a string value, the name of the above-mentioned
Config class containing `$metaFields` array, to the Entity class that represents your resource.

```php
use Bonfire\Traits\HasMeta;
Expand All @@ -157,5 +158,7 @@ use CodeIgniter\Entity;
class CustomEntity extends Entity
{
use HasMeta;

protected string $configClass = 'CustomConfig';
}
```
13 changes: 9 additions & 4 deletions src/Core/Traits/HasMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function deleteMeta(string $key)
* doesn't exist, it's deleted, otherwise it is
* either inserted or updated.
*/
public function syncMeta(array $post)
public function syncMeta(array $post): void
{
$this->hydrateMeta();
helper('setting');
Expand All @@ -189,7 +189,12 @@ public function syncMeta(array $post)
$updates = [];
$deletes = [];

foreach (setting('Users.metaFields') as $group => $fields) {
$metaInfo = setting("{$this->configClass}.metaFields");
if (empty($metaInfo)) {
return;
}

foreach ($metaInfo as $group => $fields) {
if (! is_array($fields) || ! count($fields)) {
continue;
}
Expand Down Expand Up @@ -255,11 +260,11 @@ public function syncMeta(array $post)
*
* @param string|null $prefix // Specifies the form array name, if any
*/
public function metaValidationRules(string $configClass, ?string $prefix = null): array
public function metaValidationRules(?string $prefix = null): array
{
$rules = [];
helper('setting');
$metaInfo = setting("{$configClass}.metaFields");
$metaInfo = setting("{$this->configClass}.metaFields");

if (empty($metaInfo)) {
return $rules;
Expand Down
4 changes: 3 additions & 1 deletion src/Users/Libraries/UserCells.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ class UserCells
{
protected $viewPrefix = 'Bonfire\Users\Views\\';

protected string $configClass = 'Users';

/**
* Displays the form fields for user meta fields.
*/
public function metaFormFields()
{
return view($this->viewPrefix . 'meta/list', [
'fieldGroups' => setting('Users.metaFields'),
'fieldGroups' => setting("{$this->configClass}.metaFields"),
]);
}
}
8 changes: 7 additions & 1 deletion src/Users/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class User extends ShieldUser
{
use HasMeta;

/**
* Necessary property for classes using HasMeta trait,
* containing the name of the config class with $metaFields property.
*/
protected string $configClass = 'Users';

/**
* Renders out the user's avatar at the specified size (in pixels)
*
Expand Down Expand Up @@ -117,6 +123,6 @@ public function groupsList(): string
*/
public function validationRules(?string $prefix = null): array
{
return $this->metaValidationRules('Users', $prefix);
return $this->metaValidationRules($prefix);
}
}

0 comments on commit 0bd06e1

Please sign in to comment.