October CMS backend list's column get array key value

Hi I'm new to October cms . I have defined the below shown method in my model class. The method is also used to show the select options in the backend form. This method returns an array where the key is a value similar to a field value in db. I have defined the method as static because it is also recommended in the front end where I get the function and process it with db record and iterate it to show the value of the array that matches the key. It all works fine. Thing is in my columns.yaml file, how do I list the method's array value which matches the db record as I did in front end.

public static function getSampleOptions()
{
    return[
          '1'=>'Sample1',
          '2'=>'Sample2'
          ];
}

Answers


Hello friends I found the answer with some help from October CMS Help/Support http://octobercms.com/index.php/forum/post/dropdown-shows-its-value-than-key-name-in-list-controller and referred few concepts of laravel.

Model Class Method

public static function getSampleOptions()
{
    return[
      '1'=>'Mobile App',
      '2'=>'Web  App'
      ];
} 

Columns.Yaml file

sample:
    label: Sample Column
    type: dropdown

Again back in the model, declare the attributes object and include the filed name as key with empty value

public $attributes = ['sample'=>''];

Define the getfield_nameAttribute() function to set the associated value for the appropriate key in the column

public function getSampleAttribute()
{
    $result = $this->attributes['sample'];
    $options = $this->getSampleOptions();

    foreach($options as $key=>$value)
    {
        if($key == $result)
        {
            return $value;
        }
    }
}

Updated The solution to rectify the problem while editing the record is simple. Create a partial and modify the fields. yaml

_sample_options.htm (partial) // file name should begin with_(underscore)

<?php
$fieldOptions = $model->getSampleOptions();
$sample = $model->attributes['sample'];
?>
<select id="<?= $field->getId() ?>" name="<?= $field->getName() ?>" class="form-control custom-select" <?= $field->getAttributes() ?>>
    <?php foreach($fieldOptions as $key=>$label)
    {
    ?>
        <option value="<?= $key ?>"  <?php echo ($sample == $key)?"selected":'';  ?>><?= $label ?></option>
    <?php
    } ?>
</select>

Here the $model and $field are the partial variables that are used to access the intended model's methods and properties. Documentation : https://octobercms.com/docs/backend/forms#field-partial

Fields.Yaml file

sample:
    label: Sample Field
    type: partial
    path: $/october/demo/controllers/sample/_sample_options.htm //path where the partial is located in the controller view

break a for loop in october cms template if condition not satisfied?

{% for key, pTest in pack.products %}
                                {{loop.length}}
                                <li>{{pTest.productable.name}} {{ key }}</li>
                                {% if key == 2 %}
                                <li class="more">...</li>
                                {% endif %}
                                {% endfor %}

Need Your Help

Magento header childhtml dropdown

php jquery html css magento

How do I add a child html block as a pop-up when I click a link in the header? In this link, I want to make "My Account" have a drop-down similar to the hover for the heart and bag icons.

Same query, same system, different execution time

mysql sql doctrine

The queries below all execute instantly on our development server where as they can take upto 2 minutes 20 seconds.