Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fork/craft-transform
Transform Craft CMS element and field data structures
https://github.com/fork/craft-transform
Last synced: 1 day ago
JSON representation
Transform Craft CMS element and field data structures
- Host: GitHub
- URL: https://github.com/fork/craft-transform
- Owner: fork
- License: mit
- Created: 2020-11-03T16:40:10.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-11-29T08:10:48.000Z (29 days ago)
- Last Synced: 2024-12-20T06:19:23.692Z (8 days ago)
- Language: PHP
- Size: 53.7 KB
- Stars: 6
- Watchers: 9
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Transform Plugin for Craft 5.x
**Table of contents**
- [Features](#features)
- [Requirements](#requirements)
- [Setup](#setup)
- [Usage](#usage)
- [Roadmap](#roadmap)---
## Features
- Transform Craft CMS contents to custom data structures
- Create custom Transformer classes for your components
- Cache contents on a Transformer basis (via providing `getCacheKey`)## Requirements
- Craft CMS >= 5.x
## Setup
**1. Install**
Install the package
```sh
cd /path/to/project
composer require fork/craft-transform
```**2. Configure namespace and create transformers**
- Copy the example `config.php` to your Craft config directory and rename it to `transform.php`
- Specify the namespace to your custom Transformer classes (in your project plugin/module). Here's an example:```php
[
'transformerNamespace' => '\company\project\transformers'
],
'dev' => [
'enableCache' => false,
],
'staging' => [
'enableCache' => true,
],
'production' => [
'enableCache' => true,
],
];
```In your project plugin/module create a `transformers` directory to put your transformers. Here is an example Transformer class:
```php
globals->getSetByHandle('footer');
$footerNavGlobals = $footer->footerNavigationElements->all();
$footerLinks = [];foreach ($footerNavGlobals as $linkEntry) {
$link = [
'href' => $linkEntry->navigationLink->getUrl(),
'name' => $linkEntry->navigationLink->getText(),
'slug' => $linkEntry->navigationLink->hasElement() ? $linkEntry->navigationLink->getElement()->slug : $linkEntry->navigationLink->getUrl(),
'target' => $linkEntry->navigationLink->getTarget()
];
$footerLinks[] = $link;
}return $footerLinks;
}}
```## Usage
In your templates you can use `craft.transform.getData()`. The first parameter is optional. It could be your `entry` to get the data from.
Also, it could be `null`. The second parameter must match with the corresponding Transformer class. E.g. pass `'Footer'` to use the `FooterTransformer`.```twig
{% set articleData = {
headline: entry.title,
contentBlocks: craft.transform.getData(entry, 'ContentBlocks')
} %}{% include '@components/templates/article-page/article-page.twig' with {
header: craft.transform.getData(null, 'Header'),
headline: articleData.headline,
contentBlocks: articleData.contentBlocks,
footer: craft.transform.getData(null, 'Footer')
} only %}
```## Roadmap
- [x] Caching
- [x] Logo
- [ ] Settings maybe (instead of config file)---