Winter CMS resources and help articles

Simple and to the point. Optimized by the community.

Multilingual url's / slug's pre-filled by title field

0
by ImpactFactory, last modified on February 10th, 2022

It's a nice when URL is speaking about its content and is localized. Also for marketing puposes. But that doesn't work out of the box with the language selector templates out there.

In your Model.php add this

<?php namespace Yourname\Yourplugin\Models;
.
.
.
class Yourmodel extends Model
{
use \Winter\Rain\Database\Traits\Sluggable;

public $implement = ['RainLab.Translate.Behaviors.TranslatableModel'];

public $translatable = [
        ['slug', 'index' => true],
        ['title', 'index' => true],
    ];

    protected $slugs = ['slug' => 'title'];

        public $rules = [
        'slug' => 'required',
        'title' => 'required'
    ];

        /* needed for url translation */
    public static function translateParams($params, $oldLocale, $newLocale) {
        $newParams = $params;
        foreach ($params as $paramName => $paramValue) {
            $records = self::transWhere($paramName, $paramValue, $oldLocale)->first();
            if ($records) {
                $records->translateContext($newLocale);
                $newParams[$paramName] = $records->$paramName;
            }
        }
        return $newParams;
    }

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?