Isral Bustami
Isral Bustami

Reputation: 137

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..

Upvotes: 1

Views: 1530

Answers (2)

Raja Khoury
Raja Khoury

Reputation: 3195

It is NOT getName[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] )

Upvotes: 1

Hardik Satasiya
Hardik Satasiya

Reputation: 9693

@Raja Khoury - thanks for making him understand how dropdown works ...

we can use this method for normal fields like manufacturer, but for complex fields we need to use different approach

We need to define this methods in respective model:

First for normal fields there is simple approach get[fieldname]Options

public function get[fieldname]Options($value, $formData)
{
    return ['all' => 'All', ...];
}

Second specific method name approach

fields.yaml

status:
    label: Blog Post Status
    type: dropdown
    options: listBacaan

inside your model

public function listBacaan($fieldName, $value, $formData)
{
    return ['key1' => 'data1', ...];
}

Third General Approach

inside your model

public function getDropdownOptions($fieldName, $value, $formData)
{
    if ($fieldName == 'temakebum[bacaan]') {
        return ['all' => 'All', ...];
    }
    else {
        return ['' => '-- none --'];
    }
}

and for making field as JSON and storing it as an array inside single column @Raja Khoury already answered it in different question you can take reference from there: Octobercms: How can I make a repeater field jsonable because I am creating this repeater field into a different plugin

And if it work for you please up-vote his answer as well :)

Upvotes: 1

Related Questions