PascaleBeier / filament-pages

A starting point to create a Page Resource and front routing with filament. It is the result of packaging our Page CMS functionality.
MIT License
50 stars 10 forks source link

Fields from other template on content array #10

Open RibesAlexandre opened 1 year ago

RibesAlexandre commented 1 year ago

Hi,

I have 2 templates, one is the home page and the other is a section template. When I save my second page, all the data from the home page is kept in the "data" column in DB, with a null value.

This is my homePage values :

{
  "content": {
    "title": "Les dernières boutiques",
    "content": "Lorem ipsum dolor sit amet consectetur. Risus est eu hendrerit nulla in ac cursus at. Porttitor non arcu platea sagittis imperdiet arcu sed. Arcu vel scelerisque quam sed consectetur elementum. Molestie mi mauris mattis in ultricies dignissim.",
    "categories": [
      "26",
      "51",
      "74",
      "124",
      "99",
      "135",
      "148",
      "1"
    ],
    "categories_title": "Les dernières catégories",
    "categories_content": "Lorem ipsum dolor sit amet consectetur. Risus est eu hendrerit nulla in ac cursus at. Porttitor non arcu platea sagittis imperdiet arcu sed. Arcu vel scelerisque quam sed consectetur elementum. Molestie mi mauris mattis in ultricies dignissim.",
    "shops_title": "Les dernières boutiques",
    "shops_content": "Lorem ipsum dolor sit amet consectetur. Risus est eu hendrerit nulla in ac cursus at. Porttitor non arcu platea sagittis imperdiet arcu sed. Arcu vel scelerisque quam sed consectetur elementum. Molestie mi mauris mattis in ultricies dignissim.",
    "image": {
      "0bcd37fd-a8f6-4fce-8ae6-1f260d4756cc": "images/home-intro-MIwPlmVacg.jpg"
    },
    "products_title": "Les dernières nouveautés",
    "intro_title": "Perpignan.com, c’est quoi ?",
    "intro_subtitle": "Lorem ipsum dolor sit amet consectetur.",
    "intro_content": "Lorem ipsum dolor sit amet consectetur. Tellus euismod vitae mi massa velit urna aenean donec. Risus et morbi egestas posuere dictum consequat sollicitudin. Pellentesque elementum pharetra dictum feugiat fermentum. Facilisis nisl et tortor gravida. In vulputate elementum et enim sed egestas vel. Eleifend praesent nisi vitae eu in. Ultrices nunc tristique lorem et pulvinar dui tincidunt venenatis ornare. Porttitor leo fringilla iaculis purus diam. Tempus mi imperdiet ipsum volutpat condimentum viverra senectus sed in. Tortor augue dignissim nunc eu. Tellus amet lorem turpis sed leo at cursus enim.\n\nGravida mattis posuere mollis elementum convallis quam maecenas. Dapibus et egestas massa scelerisque lectus habitasse est non adipiscing. Dapibus nulla semper enim in dignissim viverra adipiscing.",
    "offers_title": "Nos différentes offres",
    "offers_content": "Lorem ipsum dolor sit amet consectetur. Euismod vivamus adipiscing elit mauris diam enim vestibulum iaculis libero. At mauris in quisque ut viverra nibh a. Tristique ipsum sed faucibus vulputate arcu imperdiet. Massa morbi in ac et mattis. At eget interdum vel elementum ac tincidunt amet nisl mauris.",
    "faq_title": "Notre foire aux questions",
    "faq_subtitle": "Lorem ipsum dolor sit amet consectetur.",
    "faq_content": "Lorem ipsum dolor sit amet consectetur. Imperdiet sollicitudin metus donec cras urna. Sit quam ullamcorper gravida massa proin tellus. Velit amet facilisis amet at. Volutpat amet nunc nisl felis risus vitae consequat. Pulvinar molestie nisi non lorem eros orci tincidunt risus. Nullam amet.",
    "faq_questions": [
      {
        "question": "Question de Test",
        "response": "Réponse de Test"
      },
      {
        "question": "Question de Test 2",
        "response": "Réponse de test 2"
      }
    ],
    "intro_image": "images/home-intro-acEHnKqxtt.jpg",
    "h1": null,
    "subtitle": null,
    "component": null
  },
  "template": "App\\Filament\\Templates\\HomePage",
  "templateName": "home_page"
}

And this is my page section :

{
  "content": {
    "image": "9nqdSEQTkLNWVtajEQkWM85yTM0wr2-metaYmFubmVyLmpwZw==-.jpg",
    "h1": "Toutes nos boutiques",
    "content": null,
    "subtitle": "en un clic",
    "component": "shop-lists",
    "categories": [],
    "intro_image": [],
    "faq_questions": [],
    "categories_title": null,
    "categories_content": null,
    "shops_title": null,
    "shops_content": null,
    "products_title": null,
    "intro_title": null,
    "intro_subtitle": null,
    "intro_content": null,
    "offers_title": null,
    "offers_content": null,
    "faq_title": null,
    "faq_subtitle": null,
    "faq_content": null
  },
  "template": "App\\Filament\\Templates\\SectionPage",
  "templateName": "section_page"
}

We can see that all the fields of the home page have been kept.

Thanks for help,

PascaleBeier commented 1 year ago

I'll look into it!

MostafaAwady commented 11 months ago

@RibesAlexandre public static function getTemplateSchemas(): array { return static::getTemplateClasses() ->map(fn ($class) => Group::make($class::schema()) ->afterStateHydrated(fn ($component, $state) => $component->getChildComponentContainer()->fill($state)) ->statePath('data.content'. str_replace('\', '', $class)) ---------------> this is the solution ->visible(fn ($get) => $get('data.template') === $class) ) ->toArray(); }

-this line of code will solve it ( ->statePath('data.content'. str_replace('\', '', $class)) ) -you can find this function in vendor\beier\filament-pages\src\Filament\Resources\FilamentPageResource.php