Using "when" in a select field

Hello everyone!

Many things become clear and easy to solve. But there are tasks that are baffling (while I have little knowledge)

I have a page with PDF files, the files are placed in sections using the Structure field
From the current page (panel) I select a specific section (loctype field)
and if it is selected I get the file name (filename)

fields:
  loctype:
    type: select
    options:
      management: Charter A
      direction: Charter B
      features: Charter C
  management:
    type: select
    options: query
    query:
      fetch: site.find("svedeniya/dokumenty/lokalnye").management.toStructure()
      text: "{{structureItem.locname}}"
      value: "{{structureItem.locfile.tofile.filename}}"
    when:
      loctype: management
  direction:
    type: select
    options: query
    query:
      fetch: site.find("svedeniya/dokumenty/lokalnye").direction.toStructure()
      text: "{{structureItem.locname}}"
      value: "{{structureItem.locfile.tofile.filename}}"
    when:
      loctype: direction
  features:
    type: select
    options: query
    query:
      fetch: site.find("svedeniya/dokumenty/lokalnye").features.toStructure()
      text: "{{structureItem.locname}}"
      value: "{{structureItem.locfile.tofile.filename}}"
    when:
      loctype: features

I need this data to put it in the parameters of the called snippet
<?php snippet('locfiles/lokalnye-row', ['section' => 'The resulting VALUE loctype', 'files' => ['The resulting VALUE filename']]); ?>
I will figure out the PHP code for substitution of values.

I cannot figure out the correct composition of the blueprint file.
Now I get these values in the txt file

Locpage:

- 
  loctype: management
  management: polozhenie-upr-sovete.pdf
  direction: ""
  features: ""
- 
  loctype: management
  management: polozhenie-obshhem-sobranii.pdf
  direction: ""
  features: ""
- 
  loctype: direction
  management: ""
  direction: poryadok-deyatelnosti-dop.pdf
  features: ""

I want to achieve output to the file txt, or key: value
No nulls (ex. features: “” if possible)

Locpage:

- 
  management: polozhenie-upr-sovete.pdf
  management: polozhenie-obshhem-sobranii.pdf
  direction: poryadok-deyatelnosti-dop.pdf

Or output as

Locpage:

- 
  loctype: management
  management: polozhenie-upr-sovete.pdf
  management: polozhenie-obshhem-sobranii.pdf

- 
  loctype: direction
  direction: poryadok-deyatelnosti-dop.pdf

Is it possible to help with this in the forum? Or is this almost a technical task for the developer? :slightly_smiling_face:

You cannot change the structure of what the structure field stores in your content file.

But you can filter your structure field content, because the field value converted to a structure object is a collection and you can work with it. For example:

$loctypeManagement = $page->structurefieldname()->toStructure()->filterBy('loctype', 'management');

Same for direction or features.

The problem is different. I coped with the output in the template via PHP.
All right. I can’t do it nicely in the admin panel. In principle, and now it all works, it’s just not beautiful like that.
The structure section in the pane displays empty columns, etc.
I need to select one, sometimes several files from each section.

I can now hard-code in the template the choice of which section and which file to call with the snippet parameters.
But I wanted to do it through the panel.

Well, when I created the topic I already understood that this was a technical task, and not a question.
I will postpone this problem for now. Over time, I’ll come up with something.

I was going to suggest to use a files field for each section only to realise that the options already come from a structure field.

So without having the big picture of the whole setup, I don’t really know what to do with this.

1 Like

Thanks for the help.

Apparently I made a mistake in the architecture of the page with PDF files.
It was necessary not to divide it into sections by structure fields.
A place all files in a single structure file, assigning each a category. It would be easier now. So I’ll redo the page with the PDF files.
I almost every page of the site need to access this page with PDF files extracting from it one, two, maybe several files.
This is like a database of document files, which, depending on the category, are placed on certain pages of the site.

Thank you again for your attention to our questions.
When you sit and think, you don’t always come to a decision.
And when you write a question on the forum, the process of typing begins to make sense.
It’s like saying the problem out loud.

The good thing is, that there are often many ways to achieve a thing. The hard thing is finding the right way to do something in a specific use case ;).

1 Like

Please help me figure out the loop

fields:
  locacts:
    label: Local acts
    type: structure
    fields:
      locname:
        label: Document name
        type: text
      locfile:
        label: File
        type: files
      locpart:
        label: Section of local acts
        type: select
        options:
          - Section 1
          - Section 2
          - Section 3
          - Section 4
          - Section 5

Template ()

<?php $locacts = $page->locacts()->toStructure(); ?>
<?php foreach ($locacts as $locact) : ?>
    <h3><?= $locact->locpart()->text() ?></h3>
    <table>
        <thead>
        <tr>
            <th>Size</th>
            <th>Document name</th>
            <th>Link</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach ($locacts as $item) : ?>
            <?php if ($file = $item->locfile()->toFile()) : ?>
                <tr>
                    <td><?= $file->customSize(); ?></td>
                    <td><?= $item->locname()->text() ?></td>
                    <td><a href="<?= $file->url() ?>">Download</a></td>
                </tr>
            <?php endif; ?>
        <?php endforeach; ?>
        </tbody>
    </table>
<?php endforeach; ?>

Trying to get the output

Section 1 (This is the title used from the select field in h3)
  Table 1
    The file that is assigned the value "Section 1" in the locpart field
	The file that is assigned the value "Section 1" in the locpart field
	The file that is assigned the value "Section 1" in the locpart field
	etc..
Section 2 (This is the title used from the select field in h3)
  Table 2
    The file that is assigned the value "Section 2" in the locpart field
	The file that is assigned the value "Section 2" in the locpart field
	The file that is assigned the value "Section 2" in the locpart field
	etc..
Etc..

I think you should filter your structure items as I suggested in my first answer.

$locacts = $page->locacts()->toStructure();
$table1 = $locacts->filterBy('locpart', 'section1);

Then do this for every section.

The (better) alternative with less code would be to group the structure field, then loop through the groups:

$locacts = $page->locacts()->toStructure();
$groups = $locacts->groupBy('locpart');

Then you need a double loop to go the each group and inside the group through each item in the group.

1 Like

Wow. Thank you very much. Everything worked out.
Alternative option. With grouping. Thanks, works great.

P.s. The title displays in lowercase. Probably because of the select field. Somewhere I saw in the documentation how to change it. OK.

Str::ucfirst() to the rescue…

1 Like

Oh, ambush :bomb:
My section names start with a number.
I can, of course, remove them, but this is a crutch.
The numbers are important there.
Good. I’ll think.

You can either add a counter in the loop or add your numbers via CSS, which is probably the better option.

1 Like

I admire your experience and knowledge.

Of course I will add numbers through the css.
This is probably easier and more correct than adding a counter.
:+1: