This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

jimlab56999
jimlab56999

Hello, I'm extending plugin
I have some fields dropdown type
In the backend everything works correct
But in frontend, I get number
My code:

public function boot()
    {
...
RealtiesControllers::extendFormFields(function ($form, $model, $context) {
            if (!$model instanceof RealtyModel) {
                return;
            }
 $form->addTabFields([
'bathroom' => [
                    'label' => 'Bathroom',
                    'tab' => 'Standart Property',
                    'type' => 'dropdown',
                    'span' => 'storm',
                    'cssClass' => 'col-xs-4',
                    'options' => array('shared', 'separate'),
                ],
...

I found some solution:

public function getSampleAttribute()
{
    $value = array_get($this->attributes, 'sample');
    return array_get($this->getSampleOptions(), $value);
}

but how it can be applied to extending plugin ??? Thank you in advance.

mjauvin
mjauvin

please give more details, I don't understand your problem.

jimlab56999
jimlab56999

Hi mjauvin,
I have dropdown fields 'bathroom'
It has options: 'shared' and 'separate'
In the backend I get correct dropdown with two strings: 'shared' and 'separate'
But in the frontend with {{item.status}} I get Number: 0 or 1 or 2 not String

mjauvin
mjauvin

show some code, but it looks like you have the key/values reversed.

jimlab56999
jimlab56999

I have original model Realty and my extended AddProperties
In my AddProperties I add some fields in function boot()
One of them is Dropdown type

 public function boot()
    {
...
RealtiesControllers::extendFormFields(function ($form, $model, $context) {
            if (!$model instanceof RealtyModel) {
                return;
            }
 $form->addTabFields([
'bathroom' => [
                    'label' => 'Bathroom',
                    'tab' => 'Standart Property',
                    'type' => 'dropdown',
                    'span' => 'storm',
                    'cssClass' => 'col-xs-4',
                    'options' => array('shared', 'separate'),
                ],
...

That's all what I have now

jimlab56999
jimlab56999

I find a solution, but it breaks extending way
I delete options from Controllers

public function boot()
    {
...
RealtiesControllers::extendFormFields(function ($form, $model, $context) {
            if (!$model instanceof RealtyModel) {
                return;
            }
 $form->addTabFields([
'bathroom' => [
                    'label' => 'Bathroom',
                    'tab' => 'Standart Property',
                    'type' => 'dropdown',
                    'span' => 'storm',
                    'cssClass' => 'col-xs-4',

                ],
...

Add function directly to Realty model
for declaring options

  public function getBathroomOptions() {
               return [
              'shared', 'separate'];
               }

and add function for receiving attributes

 public function getBathroomAttribute() {
             $value = array_get($this->attributes, 'bathroom');
             return array_get($this->getBathroomOptions(), $value);
                       }

That's works but I broke general extending principales How I can solve this in extending model ??

mjauvin
mjauvin

use addDynamicMethod() when extending the controller.

See "Dynamically creating methods" under here:

https://octobercms.com/docs/services/behaviors#constructor-extension

jimlab56999
jimlab56999

Unfortunately I have a failure
First function 'getBuildingOprions' woks fine and I receive dropdown in backend
But second 'getBuildingAttribute' raise an error

"Undefined property: Eduard\AddProperties\Plugin::$attributes" on line 110 of /Applications/MAMP/htdocs/realtor_october/plugins/eduard/addproperties/Plugin.php

This code works perfect directly in parent model

 public function boot()
    {
        RealtyModel::extend(function($model){
        $model->addDynamicMethod('getBuildingOptions', function() use ($model){
        return ['кирпичный', 'монолит', 'панельный', 'кирпич - монолит'];
        });

        $model->addDynamicMethod('getBuildingAttribute', function() use ($model){
         $value = array_get($this->attributes, 'building');
            return array_get($this->getBuildingOptions(), $value);
        });

        });
Komak5740251
Komak5740251

You could try creating a global

$BuildingOptions = ['shared', 'separate'];

Then set

$form->addTabFields([
    'bathroom' => [
        'label' => 'Bathroom',
        'tab' => 'Standart Property',
        'type' => 'dropdown',
        'span' => 'storm',
        'cssClass' => 'col-xs-4',
        'options' => $BuildingOptions,
    ],

And finally, create a function akin to

public function getBathroomAttribute($id) {
    return $BuildingOptions[$id];
}

1-9 of 9

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.