Skip to content

Commit

Permalink
craft 5: initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
matfish3 committed Apr 13, 2024
1 parent 11ea310 commit 2b75804
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Release Notes for Element Meta

## 5.0.0 - 2024-04-13
- Craft 5: Initial Release

## 4.1.0 - 2024-01-26
### Fixed
- Install migration: use `dateTime()->notNull()` instead of `timestamp()`
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ Get a specific key value:
$entry->getElementMetadata('foo');
```

Get nested values:
```php
$entry->getElementMetadata('foo.bar');
```

Or using Twig:

```twig
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"docs": "https://github.com/matfish2/craft-entry-meta/blob/master/README.md"
},
"require": {
"craftcms/cms": "^4.0"
"craftcms/cms": "^5.0"
},
"autoload": {
"psr-4": {
Expand Down
12 changes: 11 additions & 1 deletion src/behaviors/ElementBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,18 @@ public function addElementMetadata(array $data): void
public function getElementMetadata($key = null)
{
$meta = $this->_getElementMetadata();
// allow retrieval of nested data point by using a . separator, e.g foo.bar will return 'x' if ['foo'=> 'bar' => 'x']
if ($key) {
$keys = explode('.', $key);
$value = $meta;
foreach ($keys as $k) {
$value = $value[$k] ?? null;
}
return $value;
}

return $key ? ($meta[$key] ?? null) : $meta;
// return all metadata if no key is provided
return $meta;
}

/**
Expand Down

0 comments on commit 2b75804

Please sign in to comment.