Coder Social home page Coder Social logo

Multiple forms for a single module about twill HOT 9 CLOSED

area17 avatar area17 commented on May 20, 2024
Multiple forms for a single module

from twill.

Comments (9)

wratke avatar wratke commented on May 20, 2024 1

It works! Thanks.

from twill.

ifox avatar ifox commented on May 20, 2024

Hi @wratke!

I see 3 possibles approaches there, depending on the number of pages you'll need and their forms compositions:

  1. A single pages module, with all fields and relationships in the same Eloquent model. This can seem ugly, but the simplest way to implement if you don't have tons of pages with very different fields. In the form's Blade file, you can include a different file depending on a type you store in pages records (seeded upfront or created through the UI, with a dropdown to chose type inside the Add new modal, which you can add by overwriting create.blade.php in your module views).

  2. One module per page, which means a single record in each associated table, and no listing in the CMS, but direct access to the form through the navigation, by using a route name in twill-navigation.php instead of 'module' => true. This is working great for say big landing pages that requires a lot of copy fields, browsers, repeaters and so on. Approach 1 becomes quickly messy in those instances.

  3. A single pages module, with all fields saved in a JSON column. This would require some custom logic in your module's repository, but this is definitely possible. My comment about page's type in 1. also apply here.

Most of the time for us at AREA 17, projects use a combination of 1 and 2, as well as a "generic" page model where all records share the same form but users are benefiting from the block editor to create any composition on those.

from twill.

wratke avatar wratke commented on May 20, 2024

Thanks, this is helpful. Is it possible to use a route that points directly to a specific record in the database in the twill-navigation.php file? Say I create a module for a page and created a single record (which had an id of 1). Could I create a route that links directly to the edit page for that specific record using it's id?

from twill.

ifox avatar ifox commented on May 20, 2024

You could do it using the id, yes, by using the 'raw' option in a navigation entry:

'page' => [
    'raw' => true,
    'route' => /pages/1/edit
]

However, you can also register a custom route for it:

Route::module('pages');
Route::name('homepage')->get('homepage', 'PageController@homepage');

Then, in the pages module controller:

public function homepage()
{
    // find the page by type or anything you've decided to use to differentiate them
    abort_unless($page = Page::whereType('homepage')->first(), 500, 'Homepage missing');
    
    // this is so that Twill always goes back to that form's route directly, not pages listing
    Session::put("pages_back_link", route('admin.homepage'));
    
    // with this approach you can provide your own view for each page
    return view('admin.pages.homepage',  $this->form($page->id));
}

from twill.

wratke avatar wratke commented on May 20, 2024

Great, thanks!

from twill.

wratke avatar wratke commented on May 20, 2024

One quick question. Is it possible to access the data that populates the form in the blade template? Is that data accessible as a PHP variable ? I can see how to create conditional logic in the blade template in the Laravel docs, but I need to get a sense of how Twill handles the data behind the scenes before I can do that.

from twill.

wratke avatar wratke commented on May 20, 2024

I'm talking specifically about the form.blade.php template generated for a module. If I have a field in the database called type, I'm trying to figure out how to access that data from whatever Twill is doing when it populates the form in the template.

from twill.

ifox avatar ifox commented on May 20, 2024

You should be able to access the edited model using the $item variable in Blade.

from twill.

istvanvasil avatar istvanvasil commented on May 20, 2024
  1. One module per page, which means a single record in each associated table, and no listing in the CMS, but direct access to the form through the navigation, by using a route name in twill-navigation.php instead of 'module' => true. This is working great for say big landing pages that requires a lot of copy fields, browsers, repeaters and so on. Approach 1 becomes quickly messy in those instances.

Hi @ifox

I've been trying to wrap my head around how you create single pages in Twill. The above method sounds like the most logical in many cases when you want to keep your pages as separate from each other as possible. I just have a hard time figuring out what is the so to speak 'best practice' to go about it. I am new to Laravel so I tried to avoid writing obvious questions but I am slightly stuck on this one.

I basically need to create a simple About page and the way I did it is that I created a CRUD module twill:module -BSM abouts then in the twill-navigation.php i did the following:

'abouts' => [ 'title' => 'About', 'route' => 'admin.abouts.show', 'params' => [ 'about' => 1 ], ]

It definitely solves my problem but the question is is this the "right" way to do it or will it cause problems down the line? Is there a more elegant way to do this? My frontend will be consumed through graphql and completely detached from the twill/laravel part of the site.

Thank you!

from twill.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.