https://twill.io logo
Join Discord
Powered by
# 📝twill-transformers
  • a

    antonioribeiro

    08/12/2021, 12:25 PM
    To create a block transformer (if you really need it) you just have to create a class
    App/Transformers/Block/Slideshow
    , for example
  • a

    antonioribeiro

    08/12/2021, 12:25 PM
    If you name a block 'quote-author', class would be QuoteAuthor
  • a

    antonioribeiro

    08/12/2021, 12:26 PM
    You have an example here https://github.com/area17/twill-transformers#create-block-transformers
  • i

    ifox

    08/12/2021, 12:39 PM
    @antonioribeiro I think @Ivan Markov is looking to have a transformer to transform repeaters inside a block
  • i

    Ivan Markov

    08/12/2021, 12:39 PM
    yes ! @User
  • a

    antonioribeiro

    08/16/2021, 12:44 PM
    @User , you can still create the Block\QuoteAuthor (just my example) class, when youn call
    $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:
    Copy code
    <?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 @User
  • p

    patrick

    08/16/2021, 2:19 PM
    Hi @User ! I just began working a on
    twill-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)!
  • i

    Ivan Markov

    08/16/2021, 2:46 PM
    @User I don't understand how you get repeaters using the $this->paragraphs method . The only way I could get to them is through the path $ this->data->blocks
  • i

    Ivan Markov

    08/16/2021, 2:46 PM
    dd($this)
  • a

    antonioribeiro

    08/16/2021, 4:34 PM
    @User , it was really just an example to say they are somewhere inside
    $this
    , so yes, internally it will re-add all blocks back to the transformer if we have sub-blocks (as repeaters are subblocks)
  • a

    antonioribeiro

    08/16/2021, 4:36 PM
    Nice @User ! Yeah, sure, hit me whenever you want to discuss it! A call can be more productive
  • p

    patrick

    08/16/2021, 9:30 PM
    Ok, sounds great. I'll be in touch tomorrow or Wednesday!
  • i

    Ivan Markov

    08/19/2021, 2:23 PM
    Hi @User, please help me correctly connect the namespaces of the capsules for twill-transformers https://github.com/area17/twill-transformers/blob/ac1f36c8983f4dbedb490e3f88d70b5a83ee2987/src/Behaviours/HasBlocks.php#L99
  • i

    Ivan Markov

    08/19/2021, 2:24 PM
    i tried to use capsule model browsers in blocks but failed
  • i

    Ivan Markov

    08/19/2021, 3:39 PM
    not perfect solution but worked in HasBlocks.php
    Copy code
    protected 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', ], ``
  • a

    antonioribeiro

    08/24/2021, 1:57 PM
    Sorry, @User , a bit swamped out here with work. But glad to see you find yourself a solution!
  • i

    Ivan Markov

    10/26/2021, 4:23 PM
    Hi. Tell me please. How can I override the underlying structure output of all blocks without overriding the template for each one? At the moment I have found the file src/Transformers/Block/Raw.php it has a transform method that helped me with this.
  • a

    antonioribeiro

    10/28/2021, 9:43 AM
    Hey @User , not sure what you want to accomplish but the idea is if you want to change the behaviour of your blocks you have those methods: 1)
    transformData()
    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:
    Copy code
    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?
  • a

    antonioribeiro

    10/28/2021, 9:43 AM
    2)
    transform()
    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
    Copy code
    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!
  • i

    Ivan Markov

    10/28/2021, 12:49 PM
    Thank you for your message. I will try to understand. I needed to override the basic behavior and structure of the block.
  • i

    Ivan Markov

    10/28/2021, 12:51 PM
    I found that the current solution has problems getting all nesting blocks:
    Copy code
    $this->transformSubBlocks
  • i

    Ivan Markov

    10/28/2021, 12:52 PM
    also the browser output does not work for me.
  • a

    antonioribeiro

    10/28/2021, 4:01 PM
    As things with blogs can get very complex, @User , I would say create a class
    App\Transformers\Block\BannerSlide::class
    And render the block yourself.
  • a

    antonioribeiro

    10/28/2021, 4:02 PM
    Extend it from
    A17\TwillTransformers\Transformers\Block
  • i

    Ivan Markov

    10/28/2021, 8:36 PM
    Yes. Thanks. That's exactly what I did. I was wondering how it was supposed to work by default.
  • d

    dakaalim

    08/10/2022, 12:03 PM
    https://github.com/area17/twill-transformers#create-block-transformers The line
    use App\Transformers\Block;
    Is it referring to
    A17\TwillTransformers\Transformers\Block
    ?
  • i

    ifox

    08/10/2022, 12:57 PM
    In the example the app has its own but by default you would use the one you said, yes
  • d

    dakaalim

    08/10/2022, 1:14 PM
    I have gone through the source code for a few hours, can't get my head around on this part. In the example below, what data type is
    $portraits
    and
    $portrait
    ? https://github.com/area17/twill-transformers#reuse-blocks-as-components
    Copy code
    public function transformArtistPortraits($portraits)
    {
        return collect($portraits)->map(function ($portrait) {
            return $this->transformBlockArtistPortrait($portrait);
        });
    }
  • d

    dakaalim

    08/10/2022, 1:16 PM
    Below is my
    About
    singleton module, I had success transforming `About`'s data, but I'm not sure how I can transform the
    Content Alfa
    block.
  • i

    ifox

    08/10/2022, 1:25 PM
    Did you try what the default behavior is giving you?
    'blocks' => $this->transformBlocks(),