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

Update HasMeta entity to correct errors #440

Merged
merged 2 commits into from
Mar 20, 2024
Merged
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
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 = [];

dgvirtual marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
Loading