fork/craft-transform

Transform Craft Element and field data structures

Installs: 2 742

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 8

Forks: 0

Open Issues: 1

Type:craft-plugin

2.1.1 2023-04-03 10:20 UTC

This package is auto-updated.

Last update: 2024-04-03 12:22:52 UTC


README

68747470733a2f2f6769746875622e666f726b2e64652f43726166745472616e73666f726d5f3033313132302e737667

Table of contents

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 >= 3.5.x

Setup

1. Install

Install the package

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

return [
    '*' => [
        '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

namespace company\project\transformers;

use Craft;
use League\Fractal\TransformerAbstract;

class FooterTransformer extends TransformerAbstract {

    public function transform()
    {
        $footer = Craft::$app->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.

{% 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

  • Caching
  • Logo
  • Settings maybe (instead of config file)
Fork Logo

Brought to you by Fork Unstable Media GmbH