0
votes

I try to create simple module in pyrocms, and now I create public form where we input name and description, but slug is skipField and is generated from name. At this moment I need to change the "slug generator" to add suffix for example '-'.uniqid().

My code view like this:

public function save(AdvertFormBuilder $form)
{


    $form->skipField('slug');
    $form->setOption('redirect', 'dodaj-ogloszenie/potwierdzenie');
    $form->setOption('redirect_error', 'dodaj-ogloszenie');
    $form->build();
    $form->validate();


    if($form->hasFormErrors()){
        return redirect($form->getOption('redirect_error'));
    }else{
        $form->saveForm();
        return redirect($form->getOption('redirect'));
    }
    ...

Now I need to change this slug like $_POST['name'].'-'.uniqid(). I try this by:

    $form->getFormField('slug')->setValue($form->getFormField('name')->value.'-'.uniqid());
    dd($form->getFormField('slug'));exit;

but it not work :(


Update1

The stream file:

/**
 * The stream definition.
 *
 * @var array
 */
protected $stream = [
    'slug' => 'adverts',
     'title_column' => 'name',
     'translatable' => true,
     'trashable' => false,
     'searchable' => false,
     'sortable' => false,
];

/**
 * The stream assignments.
 *
 * @var array
 */
protected $assignments = [
    'name' => [
        'translatable' => true,
        'required' => true,
    ],
    'slug' => [
        'unique' => true,
        'required' => true,
    ],
    'category' => [
        'required' => true,
    ],

];

and form build file:

/**
 * The form fields.
 *
 * @var array|string
 */
protected $fields = [
    'name'        => [
        'translatable' => true,
        'required'     => true,
        'unique'       => true,
    ],
    'slug'        => [
        'required' => true,
        'unique'   => true,
        'config'   => [
            'slugify' => 'name',
        ],
    ],
    'category' => [
        'required' => true,
    ],
];
1
Show the migration of the adverts stream, please. And also, that your code, is it from the controller? I mean the save public method. And where from is the second small part? - Piterden
Honestly, you rock mah! )) Do you want to say you have the form builder and not render it? Show please where do you render the formbuilder. Also almost all instances of pyro classes are supporting the methods chaining. - Piterden
Yes I dont use the form render because previously i'm using cakephp and on this framework form builder is not good ;) On this moment I use a normal $this->view and simple html form with {{form.fields.name.input_name}}. - bradley546994
save function is funcion in controller, I update the question to add code from stream ;) - bradley546994
Migration is wrong! Do you have a gitlab account? Please register if not, and give me your username. - Piterden

1 Answers

0
votes

Module migration:

use Anomaly\Streams\Platform\Database\Migration\Migration;
use Defr\AdvertsModule\Category\CategoryModel;

class DefrModuleAdvertsCreateAdvertsFields extends Migration
{
    /**
     * The addon fields.
     *
     * @var array
     */
    protected $fields = [
        'name'     => 'anomaly.field_type.text',
        'slug'     => [
            'type'   => 'anomaly.field_type.slug',
            'config' => [
                'slugify' => 'name',
                'type'    => '-',
            ],
        ],
        'category' => [
            'type'   => 'anomaly.field_type.relationship',
            'config' => [
                'mode'    => 'lookup',
                'related' => CategoryModel::class,
            ],
        ],
    ];
}

Adverts stream migration:

class DefrModuleAdvertsCreateAdvertsStream extends Migration
{
    /**
     * The stream definition.
     *
     * @var array
     */
    protected $stream = [
        'slug'         => 'adverts',
        'title_column' => 'name',
        'searchable'   => true,
        'trashable'    => true,
    ];

    /**
     * The stream assignments.
     *
     * @var array
     */
    protected $assignments = [
        'name' => [
            'required' => true,
        ],
        'slug' => [
            'required' => true,
            'unique'   => true,
        ],
        'category',
    ];
}

And categories stream migration:

class DefrModuleAdvertsCreateCategoriesStream extends Migration
{
    /**
     * The stream definition.
     *
     * @var array
     */
    protected $stream = [
        'slug'         => 'categories',
        'title_column' => 'name',
    ];

    /**
     * The stream assignments.
     *
     * @var array
     */
    protected $assignments = [
        'name' => [
            'required' => true,
        ],
        'slug' => [
            'required' => true,
            'unique'   => true,
        ],
    ];
}

The slug should automatically be typed when you type name.