LoginSignup
2
1

More than 5 years have passed since last update.

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

Posted at

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

サンプルソースに準じてみたけど動かなかったので色々検証してついに実現。(朝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'],
];

おわり

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1