Errors adding slider (snippet)

I am new to kirby. Keep getting errors when trying to add slides.

  • Invalid field type (“0”)
  • Field “sortBy”: The field type “…” does not exist
  • Field “split”: The field type “…” does not exist

See screenshot

This is my yml file

title: Your Page

columns:
  main:
    width: 2/3
    sections:
      fields:
        type: fields
        fields:
          text:
            type: textarea
            size: small
          slides:
            label: Slides
            type: structure
            fields:
              - slide
  sidebar:
    width: 1/3
    sections:
      pages:
        type: pages
        template: default
      files:
        type: files

Snippet code

<div class="swiper-container swiper-container--lyrics">
    <div class="swiper-wrapper">
        <?php foreach ($page->slides()->toStructure() as $slide): ?>
            <div class="swiper-slide">
                <figure class="blockquote-container">
                    <blockquote><?= $slide->content() ?></blockquote>
                    <figcaption><?= $slide->caption() ?></figcaption>
                </figure>
            </div>
        <?php endforeach ?>
    </div>
</div>

default.php

            <div class="container">        
                <?php snippet('slider') ?>
            </div>

Hi, welcome to the forum.

This is not how you define fields within a structure field, please see: Structure | Kirby CMS

I don’t get it. What am I doing wrong.
Sorry for bothering.

I want to use the snippet on multiple pages. but maintain it in one place in the CMS.
Should I do this in a different way?
If I use a collection it will be pages. But this is just a simple list of code for each slide.

Well, I don’t really understand what

slide refers to in this context. If that is a reference to your PHP snippet, then it just doesn’t make sense.

A structure field definition expects a set of fields to create the form for the structure field, as shown in the documentation I linked to above.

Then in your snippet, you call the values of these fields.

The purpose of a blueprint is to define fields for the forms shown in the Panel.

So in other words, you don’t add a snippet to your fields definition.

Blueprints are (basically) for creating forms for the Panel. Then you fill in the forms and the values are saved in txt files. Your templates and snippets are for rendering your content.

1 Like

I am still struggling. Sorry to bother further.

Can you give a example of the code. If I have a snippet on multiple pages.
With inside that snippets slides and the fields on my homepage?

So how would these files look:

  • default.yml
  • slider.php snippet
  • home.text

when I am using this in default.php:

<?php snippet('slider') ?>

What sort of fields do you want to have within your structure field? A content field and a caption field it seems.

A typical structure field looks like this:


columns:
  main:
    width: 2/3
    sections:
      fields:
        type: fields
        fields:
          text:
            type: textarea
            size: small
        fields:
          slides:
            label: Slides
            type: structure
            fields:
              content: # example field name
                label: Content
                type: textarea
                width: 1/2
              caption: # example field name
                label: Caption
                type: text
                width: 1/2

Then in your snippet (or template), you render this field like this:

<div class="swiper-container swiper-container--lyrics">
    <div class="swiper-wrapper">
        <?php foreach ($page->slides()->toStructure() as $slide): ?>
            <div class="swiper-slide">
                <figure class="blockquote-container">
                    <blockquote><?= $slide->content() ?></blockquote>
                    <figcaption><?= $slide->caption() ?></figcaption>
                </figure>
            </div>
        <?php endforeach ?>
    </div>
</div>

So the blueprint and your snippet are completely independent, but of course you have to use the field names you defined in the blueprint to render the fields.

1 Like

So the blueprint and your snippet are completely independent, but of course you have to use the field names you defined in the blueprint to render the fields.

Aha that seems logical.
Ive tried the structure field you provided and the snippet/template.
But for some reason I get allot of errors and the cannot view the homepage in the browser anymore.

See

What am I missing here that is causing this?

Sorry, I duplicated the fields key:

columns:
  main:
    width: 2/3
    sections:
      fields:
        type: fields
        fields:
          text:
            type: textarea
            size: small
          slides:
            label: Slides
            type: structure
            fields:
              content: # example field name
                label: Content
                type: textarea
                width: 1/2
              caption: # example field name
                label: Caption
                type: text
                width: 1/2
1 Like

I changed the fields file.

Still errors.
Also cleared the cache folder btw.

Is the error in the Panel (I copied the blueprint code from above and seems to work for me), or does this appear when you open the page on the frontend?

1 Like

Looks like the problem comes from the field name content. Please rename the field to something else, both in the blueprint and in the snippet.

1 Like

I changed the name to “quote” in both blueprint and snippet but I get the error when opening page on the frontend:

  • This page is currently offline due to an unexpected error. We are very sorry for the inconvenience and will fix it as soon as possible.

Advice for developers and administrators:
Enable debug mode to get further information about the error.*

and this one in my terminal:

[Sat Dec  2 18:37:09 2023] 127.0.0.1:56930 Accepted
[Sat Dec  2 18:37:09 2023] TypeError: Kirby\Content\Content::__construct(): Argument #1 ($data) must be of type array, string given, called in /Users/michielvos/Documents/Development/drt-cms/kirby/src/Cms/StructureObject.php on line 38 and defined in /Users/michielvos/Documents/Development/drt-cms/kirby/src/Content/Content.php:55
Stack trace:
#0 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Cms/StructureObject.php(38): Kirby\Content\Content->__construct('aaa', Object(Kirby\Cms\Page))
#1 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Cms/Item.php(55): Kirby\Cms\StructureObject->__construct(Array)
#2 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Cms/Items.php(78): Kirby\Cms\Item::factory(Array)
#3 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Cms/Structure.php(47): Kirby\Cms\Items::factory(Array, Array)
#4 /Users/michielvos/Documents/Development/drt-cms/kirby/config/methods.php(258): Kirby\Cms\Structure::factory(Array, Array)
#5 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Content/Field.php(78): Kirby\Cms\Core->{closure}(Object(Kirby\Content\Field))
#6 /Users/michielvos/Documents/Development/drt-cms/site/snippets/slider.php(3): Kirby\Content\Field->__call('tostructure', Array)
#7 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Filesystem/F.php(425): include('/Users/michielv...')
#8 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Filesystem/F.php(364): Kirby\Filesystem\F::loadIsolated('/Users/michielv...', Array)
#9 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Filesystem/F.php(372): Kirby\Filesystem\F::Kirby\Filesystem\{closure}()
#10 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Toolkit/Tpl.php(36): Kirby\Filesystem\F::load('/Users/michielv...', NULL, Array)
#11 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Template/Snippet.php(172): Kirby\Toolkit\Tpl::load('/Users/michielv...', Array)
#12 /Users/michielvos/Documents/Development/drt-cms/kirby/config/components.php(301): Kirby\Template\Snippet::factory('slider', Array, false)
#13 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Cms/App.php(1553): Kirby\Cms\Core->{closure}(Object(Kirby\Cms\App), 'slider', Array, false)
#14 /Users/michielvos/Documents/Development/drt-cms/kirby/config/helpers.php(531): Kirby\Cms\App->snippet('slider', Array, false, false)
#15 /Users/michielvos/Documents/Development/drt-cms/site/templates/default.php(120): snippet('slider')
#16 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Filesystem/F.php(425): include('/Users/michielv...')
#17 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Filesystem/F.php(364): Kirby\Filesystem\F::loadIsolated('/Users/michielv...', Array)
#18 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Filesystem/F.php(372): Kirby\Filesystem\F::Kirby\Filesystem\{closure}()
#19 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Toolkit/Tpl.php(36): Kirby\Filesystem\F::load('/Users/michielv...', NULL, Array)
#20 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Template/Template.php(163): Kirby\Toolkit\Tpl::load('/Users/michielv...', Array)
#21 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Cms/Page.php(1017): Kirby\Template\Template->render(Array)
#22 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Cms/App.php(777): Kirby\Cms\Page->render()
#23 /Users/michielvos/Documents/Development/drt-cms/kirby/src/Cms/App.php(1191): Kirby\Cms\App->io(Object(Kirby\Cms\Page))
#24 /Users/michielvos/Documents/Development/drt-cms/index.php(5): Kirby\Cms\App->render()
#25 /Users/michielvos/Documents/Development/drt-cms/kirby/router.php(14): require('/Users/michielv...')
#26 {main}
[Sat Dec  2 18:37:09 2023] 127.0.0.1:56930 Closing
[Sat Dec  2 18:37:09 2023] 127.0.0.1:56932 Accepted
[Sat Dec  2 18:37:09 2023] 127.0.0.1:56932 Closing

I used a new install! its working!!

THANX SO SO MUCH :smiley:

Great!

1 Like

I more question. The snippet is not displaying on other pages.
I dont want to have the fields on the other pages also (that way I have to maintain 3 sliders but its the same)

Am I missing something in my code?

Well, $page always refers to the current page, so you would have to change the code slightly. Assuming your structure is on the homepage, use the page helper to fetch that page by passing the id of the page as argument:

<?php foreach (page('home')->slides()->toStructure() as $slide): ?>

Aha I get it now!
This is amazing! And so are you!
Thnx again. I will use Kirby only from now on :slight_smile:

:heart_eyes: I like that!

1 Like

I have a harder one now:

<div class="swiper-container swiper-container--discography">
    <div class="swiper-wrapper">
        <?php foreach (page('home')->slidesdiscography()->toStructure() as $slide): ?>
            <div class="swiper-slide">
                <div class="swiper-slide__container">
                    <div class="discography__item">
                        <img class="discography__img" src="<?= $slide->albumcover() ?></strong>" alt="lorem">
                        <p class="discography__title"><strong><?= $slide->title() ?></strong></p>
                        <p class="discography__year"><?= $slide->year() ?></p>
                        <a class="discography__link" target="_blank" href="<?= $slide->anchorlink() ?>"><?= $slide->anchortext() ?></a>
                    </div>
                </div>
            </div>
        <?php endforeach ?>
    </div>
</div>

When I try this blueprint:

          slidesdiscography:
            label: Slider discography
            type: structure
            fields:
              title:
                label: Title
                type: text
              albumcover:
                label: Album cover
                type: files
              year:
                label: Year
                type: text
              anchorlink:
                label: Link
                type: text
              anchortext:
                label: Link text
                type: text

How can I fill the src field with a file thats uploaded?
And the href with link to some site?

You have to convert what is stored in the files field to an file object, and then get the url of this file

<?php if ($cover = $slide->albumcover()->toFile()): ?>
  <img class="discography__img" src="<?= $cover->url() ?>">
<?php endif ?>

The if is needed to make sure we have a file object (stored in $cover), so that we can then safely call the url() method without getting an error when the file does not exist.