Skip to content
Advertisement

How to define a user (author) – OctoberCMS

I need help. I only need to be able to view one user for a post in OctoberCMS.

But all users will always show up to me.

Here’s the code that works, but I don’t know how to define it to work the way I need it.

idea: https://ibb.co/VM2Rrr3

public function getUserOptions()
{
    $result = [0 => 'indikator.news::lang.form.select_user'];
    $users = Db::table('backend_users')->orderBy('login', 'asc')->get()->all();

    foreach ($users as $user) {
        $name = trim($user->first_name.' '.$user->last_name);
        $name = ($name != '') ? ' ('.$name.')' : '';
        $result[$user->id] = $user->login.$name;
    }

    return $result;
}

Advertisement

Answer

Use this as user who is creating project is user who is currently logged in so we can use current logged-in user that.

use BackendAuth;

public function getUserOptions()
{
    // placeholder
    $result = [0 => 'indikator.news::lang.form.select_user'];

    // only back-end user which is currently logged in
    // it will be available always as we are in back-end :)
    $user = BackendAuth::getUser();
    $result[$user->id] = $user->login; // or $user->email;    

    return $result;
}

It will get logged in user and show in drop-down for selection [ or even you skip place-holder to have only 1 selected option]

if any doubts please comment.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement