Help us understand the problem. What is going on with this article?

【CraftCMS】管理画面のソート機能拡張

More than 1 year has passed since last update.

タイトルと投稿日以外でソートしたい

サンプルソースに準じてみたけど動かなかったので色々検証してついに実現。(朝4時半)

Modules.php
namespace modules;

use craft\elements\Entry;
use craft\events\RegisterElementSortOptionsEvent;
use yii\base\Event;

class Module extends \yii\base\Module
{
    public function init()
    {
        parent::init();

        //↓ここに書いてあることまんまだけどどこに組み込めばいいか分からず...
        // https://github.com/craftcms/cms/issues/2818
        // https://docs.craftcms.com/v3/extend/updating-plugins.html#plugin-hooks
        Event::on(Entry::class, Entry::EVENT_REGISTER_SORT_OPTIONS, function (RegisterElementSortOptionsEvent $event) {
            $event->sortOptions[] = [
                'orderBy' => 'field_XXXXXX',
                'label' => "セレクタで表示したいラベル",
                'attribute' => 'field:XXX',
            ];

            $event->sortOptions[] = [
                'orderBy' => 'field_teriyaki',
                'label' => "TERIYAKI",
                'attribute' => 'field:54',
            ];
        });
    }
}

要素解説

key 役割
orderBy ソートのキーにしたいfieldを指定。
「field_」 + ハンドル名の形式で記述
label ソートセレクタに表示する名称。何でもOK。
Craft::t()で翻訳も可。
attribute 対象ハンドルのfield_id。
「field:」 + id の形式で記述。

これを CRAFT_PATH/modules/Module.phpへ組み込み
(今回は他にモジュール使ってないのでそのまま上書き)

さらにCRAFT_PATH/config/app.phpを↓のようにする

config/app.php
return [
    'modules' => [
        'control-panel' => \modules\Module::class,
    ],
    'bootstrap' => ['control-panel'],
];

おわり

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした