カスタムコンテンツのリストをホームに表示する方法

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

たぶん、あまり良くないとは思いますし、絶対に他のやり方があると思うんですが(汗)、力技でゴリっと・・・・。

use BcCustomContent\Service\CustomEntriesService;
use BcCustomContent\Service\CustomContentsService;

を呼んでおいて・・・

public function getCustomEntries($customContentId, int $num = 5)
    {
        $service = new CustomContentsService();
        $customContent = $service->get($customContentId);
        $entityService = new CustomEntriesService();
        $entityService->setup($customContent->custom_table_id);
        $params = array_merge([
            'conditions' => [
                'status' => true
            ]
        ]);
        $data = $entityService->getList($params); //getList()にはlimitやorderが通じない模様・・・
        $return = [];
        $c=0;
        if(!empty($data)){
            foreach ($data as $key => $value) {
                if($c > $num){ //仕方ないので取得上限をつける。
                    break;
                }
                $entity = $entityService->get($key)->toArray();
                $return[$c] = $entity;
                $c++;
            }
        }
        return array_reverse($return);//DESCできないので反対にして返す。
    }

として・・・
View側で

$this->XXXXX->getCustomEntries(1,10)

とかすると出ないですかね。

ご回答ありがとうございます。
ご提示いただいたコードで、カスタムコンテンツの内容が取得できることを確認できました。

$service = new CustomContentsService();
$entityService = new CustomEntriesService();

この方法でServiceを取得できるのだとしたら、最初の投稿のコードをベースに、$this->getService() の部分を置き換えることでより効率よく取得できそうに思い、試してみました。
結果、以下のコードで意図通りの動作を得ることができました。この方法であればlimitなども効くようです。

public function getCustomEntries($customContent, int $num = 5, array $options = [])
{
    // $customContentが entityId だった場合
    if (is_integer($customContent)) {
        $customContent = (new CustomContentsService())->get($customContent, [ // newに変更
                'status' => 'publish'
        ]);
    }
    $entriesService = new CustomEntriesService(); // newに変更
    $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 $entriesService->getIndex($params)->toArray(); //toArray()に変更
}

ということで、無事に解決いたしました。大変助かりました。
ありがとうございました。

「いいね!」 1