Modules in Modules?

Hi there…

I’m busily learning all I can about Kirby but I have a question from the end users that I’m not sure how to solve.

We’re using the modules plugin and are wondering if we can use a module with a module to solve the following?

We have a module that generates an accordion, complete with a header. We’d like to make the number of items in the accordion dynamic so that users can add additional subheaders and lines to the accordion.

Our module looks like this at present:

<section class="o-accordion">
    <div class="container">

		<?php pattern( '01-atoms/typography/heading-two', [ 'text' => kirbytext($module->heading()) ] ) ?>
		<?php pattern( '02-molecules/accordion', [
			'subheading1' => kirbytext($module->subheading1()),
			'content1'    => kirbytext($module->content1()),
			'subheading2' => kirbytext($module->subheading2()),
			'content2'    => kirbytext($module->content2()),
			'subheading3' => kirbytext($module->subheading3()),
			'content3'    => kirbytext($module->content3()),
		] ) ?>

    </div>

</section>

Our blueprint looks like this:

title: Accordion Module
pages: false
files: false

fields:
  heading:
    label: Accordion heading
    type: text
  subheading1:
    label: Heading for the first accordion
    type: text
  content1:
    label: Text for first accordion
    type: textarea
  subheading2:
    label: Heading for the second accordion
    type: text
  content2:
    label: Text for the second accordion
    type: textarea
  subheading3:
    label: Heading for the third accordion
    type: text
  content3:
    label: Text for the third accordion
    type: textarea

We’d like to make subheadingX and contentX be variable.

I tried adding a module to the module blueprint, but I couldn’t get it to work.

  1. So my questions are, can modules have submodules?
  2. Is that the way to solve this problem?
  3. Any other idea of how to solve this?

I thank you all in advance.

I answer with a question: Why don’t you use a structure field instead of these separate fields?

Hi Sonja,

Thank you for your quick answer. I did try using a structure field, but
couldn’t seem to make it work. Can you give me an example of how to set
the blueprint?

I’m all for simple straightforward solutions!

m.

Sure:

fields:
  heading:
    label: Accordion heading
    type: text
  accordions:
    label: Accordions
    type: structure
    style: table #optional
    fields:
      subheading:
        label: Heading for accordion
        type: text
      content:
        label: Text for accordion
        type: textarea

You would have to adapt your pattern as well, of course, to reflect the new structure.

Um. Yeah. Somebody is a genius!

Thanks so much for this. It’s exactly what I was looking for!