Ad

How To Define GetName[field]Options() Method In Octobercms

I got an error when trying to use

get*field*Options() method 

to

    field: name[field]

I tried to use:

getName[field]Options() method but it return an error.

How can I make this work?

fields.yaml

    temakebum[tema]:                    
            tab: 'Kebaktian Umum & Komisi'             
            label: Tema
            oc.commentPosition: ''
            span: full
            type: text
    temakebum[bacaan]:
            label: 'Bahan Bacaan'
            oc.commentPosition: ''
            span: full
            type: dropdown
            tab: 'Kebaktian Umum & Komisi' 
    temakebum[pujian]:
            label: Pujian
            oc.commentPosition: ''
            span: full
            type: text
            tab: 'Kebaktian Umum & Komisi' 

And in the models

    public function getTemakebum[bacaan]Options() { 
      $bacaan = Db::table('mismaiti_mywarta_jadwlibdh')->where('group','umumraya')->pluck('bacaan','bacaan');
      return $bacaan;
    }

I need to put this several fields in as array into database table.. it is more like the repeater widget.. but the repeater require user to hit add new item button.. i don't want user to hit add new button but i want it there by default

if i use repeater getnamefieldOptions method is work well.. so if i use repeater the method is

getBacaanOptions(){ }

hope i said it clear enough..

Ad

Answer

It is NOTgetName[field]Options() instead use get[FieldName]Options()

If your have a model Car and you have a field ( Column ) named Manufacturer then the method name is getManufacturerOptions()

The fields.yaml file of the Car Model should look like this;

  color:
      label: 'Color'  
      type: dropdown

  manufacturer:
      label: 'Manufacturer' 
      type: dropdown

then in Car mode add the method ;

public function getManufacturerOptions() {

    return [
        'volkswagen' => 'Volkswagen',
        'ford'       => 'Ford',
        'toyota'     => 'Toyota'
    ];

  // Or return ManufacturerModel::all()->pluck('name','id');
}

public function getColorOptions() {

    return [
        'black'    => 'Black', 
        'white'    => 'White'
    ];
}

Because the field type is dropdown the method should always return result as an array in the format : Value => Label

If there are no options return an empty array.

When you define the options in the fields.yaml then there's no need to add the method in your model

  color:
      label: 'Color' 
      type: dropdown
      options:
          black: Black
          white: White

UPDATE

1.Add a json column to your DB table $table->json('temakebum')->nullable();

2.Add protected $jsonable = [ 'temakebum '] in your Model Definition

3.Using the naming convention I mentioned above add getBacaanOptions() method to your model

4.Keep your fields.yaml file fields as they are, now the workaround is to change the field type from dropdown to partial for the temakebum[bacaan] field and populate the options there

5.Create a partial in your controller Directory and check the path matches the one in the fields.yaml file

So far fields.yaml looks like this

  temakebum[tema]:
          label: Tema 
          type: text
  temakebum[bacaan]:
          label: 'Bahan Bacaan' 
          type: partial
          path: $/authorName/pluginName/controllers/pluginControllerName/bacaan.htm
  temakebum[pujian]:
          label: Pujian 
          type: text

And your bacaan.htm partial like this :

<?php
$fieldOptions = $model->getBacaanOptions(); // See here we are fetching values
$Temakebum = json_decode($model->attributes['temakebum'], true)  ?: [];
?>
<select class="form-control custom-select" name="YourModelHere[temakebum][bacaan]">
    <?php foreach( $fieldOptions as $key=>$label) { ?>
        <option value="<?= $key ?>" <?php echo ( $Temakebum['bacaan'] == $key) ? "selected" : '';  ?> ><?= $label ?></option>
    <?php } ?>
</select>

( make sure to set the proper select name in the partial YourModelHere[temakebum][bacaan] )

Ad
source: stackoverflow.com
Ad