antonioribeiro
08/12/2021, 12:25 PMApp/Transformers/Block/Slideshow
, for exampleantonioribeiro
08/12/2021, 12:25 PMantonioribeiro
08/12/2021, 12:26 PMifox
08/12/2021, 12:39 PMIvan Markov
08/12/2021, 12:39 PMantonioribeiro
08/16/2021, 12:44 PM$this->transformBlocks()
and it finds a block named QuoteAuthor, this class will be called having $this
as the all contents of your block (QuoteAuthor), so inside this class you get transform your block data, and can also loop over your repeaters and transform them, you can even create a new class just to transform your repeaters, if you like. Let's say your repeaters are the paragraphs of the QuoteAuthor:
<?php
namespace App\Transformers\Block;
class QuoteAuthor extends Transformer
{
public function transform($data = null)
{
return [
'title' => $this->title,
'author' => $this->author,
'paragraphs' => $this->transformQuoteParagraphs($this->paragraphs),
];
}
}
/cc @Userpatrick
08/16/2021, 2:19 PMtwill-api
package in order to have a REST API ready and extendable. I am starting to dig into twill-transformers
to hopefully bring in the work into the api package. Any words of wisdom or advice? 🙂 I'm not so far into it yet, but would love to discuss it with you (and everyone interested)!Ivan Markov
08/16/2021, 2:46 PMIvan Markov
08/16/2021, 2:46 PMantonioribeiro
08/16/2021, 4:34 PM$this
, so yes, internally it will re-add all blocks back to the transformer if we have sub-blocks (as repeaters are subblocks)antonioribeiro
08/16/2021, 4:36 PMpatrick
08/16/2021, 9:30 PMIvan Markov
08/19/2021, 2:23 PMIvan Markov
08/19/2021, 2:24 PMIvan Markov
08/19/2021, 3:39 PMprotected function getModelFromBrowserName($browserName, $id)
{
$browserName = Str::beforeLast($browserName, ':');
// $class =
// $this->config('namespaces.app.models') .
// '\\' .
// Str::singular(Str::studly($browserName));
foreach ($this->config('namespaces.app.models') as $namespace)
{
$class = $namespace . '\\' . Str::singular(Str::studly($browserName));
if (class_exists($class)) {
break;
}
}
return $class::find($id);
}
in config twill-transformers.php
```
'app' => [
// 'models' => 'App\Models',
'models' => ['App\Models','App\Twill\Capsules\Products\Models'],
'transformers' => 'App\Transformers',
],
``antonioribeiro
08/24/2021, 1:57 PMIvan Markov
10/26/2021, 4:23 PMantonioribeiro
10/28/2021, 9:43 AMtransformData()
method: page
transformers will usually have this method, it's used to get the data from the transformer and it should be called from your base transformer. So usually my App\Transformers\Transformer::class
looks something like this:
public function transform(): array
{
return $this->sanitize([
'template_name' => $this->makeTemplateName(),
'page' => $this->transformData($this),
'seo' => $this->transformSeo($this),
'locale' => locale(),
]);
}
See the transformData()
being called?antonioribeiro
10/28/2021, 9:43 AMtransform()
method: this is the root transformer method and if you don't override this method on a new transformer class, it will use the base transformer method. Usually we create a Transformer class on our application and we have a transform()
method on it, so all my page
transformers will return the same structure every time, for for example I have a Home Page, a Post Page, and an Author Page, they will all descend from the same base transformer, will all use the same base transform() method, and the method responsible for transforming data for that page is actually transformData()
.
When we have a partial transformer let's say a Promotion, I will override the transform()
method because now I don't need ALL that data that we are generating on the base transformer, I don't need page, seo, locale, what I need is my transformer to only return an array with the Promotion, like this
class Promotion extends Transformer
{
public function transform(): array
{
$promotion = $this->getData();
return [
'id' => $promotion->ProductId,
'name' => $promotion->ProductName,
'code' => $promotion->ProductCode,
'quantity' => $promotion->Quantity,
'saving' => $promotion->Saving,
];
}
}
So for blocks if you need to override the base block transformer because you need more, you just have to create the App\Transformers\Block\Quote::class
, override the transform()
and generate the array for your block.
Hope it helps,
Cheers!Ivan Markov
10/28/2021, 12:49 PMIvan Markov
10/28/2021, 12:51 PM$this->transformSubBlocks
Ivan Markov
10/28/2021, 12:52 PMantonioribeiro
10/28/2021, 4:01 PMApp\Transformers\Block\BannerSlide::class
And render the block yourself.antonioribeiro
10/28/2021, 4:02 PMA17\TwillTransformers\Transformers\Block
Ivan Markov
10/28/2021, 8:36 PMdakaalim
08/10/2022, 12:03 PMuse App\Transformers\Block;
Is it referring to A17\TwillTransformers\Transformers\Block
?ifox
08/10/2022, 12:57 PMdakaalim
08/10/2022, 1:14 PM$portraits
and $portrait
?
https://github.com/area17/twill-transformers#reuse-blocks-as-components
public function transformArtistPortraits($portraits)
{
return collect($portraits)->map(function ($portrait) {
return $this->transformBlockArtistPortrait($portrait);
});
}
dakaalim
08/10/2022, 1:16 PMAbout
singleton module, I had success transforming `About`'s data, but I'm not sure how I can transform the Content Alfa
block.ifox
08/10/2022, 1:25 PM'blocks' => $this->transformBlocks(),