Select fields does not populate when creating programatically custom blocks

Hello there!

I’ve got an issue with Select fields when trying to creating programatically custom blocks for a page.

For some reasons, I want to populate some fields from an old Builder field to a new Block field. I followed this tutorial and I succeed to populate my new Block field with the data from the old Builder, except for select fields, where no data are added. :frowning_face:

Here is the data of the old Builder field :

# content/some-member/member.txt

Builder:

- 
  nom: Movie Name
  credits: John Doe
  realisation: John Doe and Michael Bay
  production: John Doe Production
  annee: 2021-01-01
  genre: Fiction
  poster: ""
  type: Long-Métrage
  _fieldset: cinema

Here is the code I use to create the new block field from the Builder data:

# member.php

<?php
  # I've added ->limit(1) only for testing purpose
  foreach($page->builder()->toBuilderBlocks()->limit(1) as $oldBlock) {
    $blocks = $page->filmographie()->toBlocks();
    
    # Get all the values from the old Builder
    $nom = $oldBlock->nom();
    $credits = $oldBlock->credits();
    $realisation = $oldBlock->realisation();
    $production = $oldBlock->production();
    $annee = $oldBlock->annee();
    $genre = $oldBlock->genre();
    $poster = $oldBlock->poster();
    $format = $oldBlock->type();
    $media = $oldBlock->_fieldset();

    # Create new Block using the old values from builder
    $newBlocks = new Kirby\Cms\Block([
      'content' => [
      # this one does not populate
      'format' => $format,
      # this one does not populate
      'media' => $media,
      'nom' => $nom,
      'credits' => $credits,
      'realisation' => $realisation,
      'production' => $production,
      'annee' => $annee,
      # this one does not populate
      'genre' => $genre,
      'poster' => $poster,
     ],
      # custom block type
      'type' => 'project',
    ]);

    $blocks = $blocks->add(new Kirby\Cms\Blocks([$newBlocks]));
    $kirby->impersonate('kirby');
            
    if($page->filmographie()->toBlocks()->isEmpty()) {
     $page->update([
       'filmographie' => $blocks->toArray(),
      ]);
     }
    }
 ?>

And this is what is populated to the new Block field:

Filmographie:

[
 {
   "content": {
     "format": "",
     "media": "",
     "nom": "Movie Name",
     "credits": "John Doe",
     "realisation": "John Doe and Michael Bay",
     "production": "John Doe Production ",
     "annee": "2021-01-01",
     "genre": "",
     "poster": []
    },
  "id": "0aecdcbc-8d36-4173-9425-2f9154b0d1aa",
  "isHidden": false,
  "type": "project"
 }
]

As you can see, format, media, and genre (all select field type) are empty while other fields are populated. Do you have any idea what I’m doing wrong here?

Thank you very much for any help!

(using Kirby 3.5.5)

Could you please post the blueprint for this custom block type?

Yes of course, here it is:

# blueprints/pages/member.yml

# ...
filmographie:
 label: Filmographie
 type: blocks
 pretty: true
 fieldsets:
   project:
     name: Projet
     icon: video
     fields:
       format:
         label: Format (Long ou Court)
         type: select
         options:
           Long-Métrage: Long-Métrage
           Court-Métrage: Court-Métrage
         required: true
         width: 1/2
       media:
         label: Type de projet (Cinéma, TV, Radio...etc)
         type: select
         options:
           cinema: Cinéma
           tv: TV
           radio: Radio
           edition: Édition
           nouveaux_medias: Nouveaux Médias
         required: true
         width: 1/2
        nom:
          label: Nom du projet
          type: text
          placeholder: "ex: Mon Oncle"
          required: true
          width: 1/1
        credits:
          label: Crédits Scénarios
          type: text
          placeholder: "ex: Jacques Lagrange et Jean L'Hôte"
          width: 2/3
        realisation:
          label: Réalisation
          type: text
          placeholder: "ex: Jacques Tati"
          width: 2/3
        production:
          label: Production
          type: text
          placeholder: "ex: Specta-Films"
          width: 2/3
        gap:
          type: gap
          width: 1/3
        annee:
          label: Année de sortie
          type: date
          display: YYYY
          placeholder: "ex: 1958"
          required: true
          width: 1/3
        genre:
          label: Genre
          type: select
          options:
            Animation: Animation
            Documentaire: Documentaire
            Fiction: Fiction
          width: 2/3
        poster:
          label: Affiche du film
          type: files
          multiple: false
          layout: cards
          image:
          ratio: 3/4
         width: 2/3

Ok, fixed. I needed to use the ->value() function to retrieve only the value and not the select field of object type.

# member.php
# ...
$format = $oldBlock->type()->value();
$media = $oldBlock->_fieldset()->value();
$genre = $oldBlock->genre()->value();

I was sure I tried it before but I guess I did it wrong. Anyway, problem solved, thanks!