baserCMS 5にて、カスタムコンテンツのリストをホームなどに表示したく、以下のトピックを参考にテーマヘルパーを作成する方法を試していますが、行き詰まってしまいました。
テーマ名を MyTheme として、/plugins/MyTheme/src/View/Helper/MyThemeHelper.php を以下の内容で作成しました。
<?php
namespace MyTheme\View\Helper;
use BcCustomContent\Service\CustomContentsServiceInterface;
use BcCustomContent\Service\CustomEntriesServiceInterface;
use Cake\View\Helper;
class MyThemeHelper extends Helper
{
public function display()
{
echo "TESTTEST";
}
// 上記トピックの getCustomEntries() をコピペ
public function getCustomEntries($customContent, int $num = 5, array $options = [])
{
// $customContentが entityId だった場合
if (is_integer($customContent)) {
$customContent = $this->getService(CustomContentsServiceInterface::class)->get($customContent, [
'status' => 'publish'
]);
}
$this->entriesService = $this->getService(CustomEntriesServiceInterface::class);
$this->entriesService->setup($customContent->custom_table_id);
$params = array_merge([
'contain' => ['CustomTables' => ['CustomContents' => ['Contents']]],
'status' => 'publish',
'order' => $customContent->list_order,
'direction' => $customContent->list_direction,
'limit' => $num
], $options);
return $this->entriesService->getIndex($params)->toList();
}
}
テーマを MyTheme に設定し、ホームの固定ページテンプレートから以下のように呼び出そうとしました。
// plugins/MyTheme/templates/Pages/home.php
<?php
$entries = $this->MyTheme->getCustomEntries(1);
// $entriesを使ったコード: 略
?>
この時、ブラウザからホームを表示しようとすると以下のエラーが発生してしまいます。
Call to undefined method MyTheme\View\Helper\MyThemeHelper::getService()
getService() というメソッドが見つからないようなのですが、何か追加の宣言などが必要なのでしょうか。
なお、テーマヘルパー自体は認識されているようで
// plugins/MyTheme/templates/Pages/home.php
<?php
$this->MyTheme->display(); // TESTTEST が表示される
?>
は問題なく動作しています。
解決方法をご存知の方がいらっしゃいましたら、ご教示いただけますと幸いです。
よろしくお願いいたします。
【環境情報】
・baserCMSのバージョン:5.1.5
・レンタルサーバー名:ローカルにてDocker環境を構築
・使用テーマ:自作
・PHPスキル(自己評価):C