web-dev-qa-db-ja.com

JSubmenuHelperが廃止されたので、JHtmlSidebarに移行する方法は?

これについて私が見つけた唯一のソースはこの投稿からです: JSubmenuHelperをJHtmlSidebar/Joomla 4の管理メニューで置き換える

ここでの問題は、私が試してみたことであり、JHtmlSidebarで試してもサイドバーが表示されません。

ここにコードがあります:

_public function render($config = array())
{
    $config = new KObjectConfigJson($config);
    $config->append(array(
        'toolbar' => null
    ));

    foreach ($config->toolbar->getCommands() as $command)
    {
        if(!empty($command->href)) {
            $command->href = $this->getTemplate()->route($command->href);
        }

        JSubmenuHelper::addEntry($this->getObject('translator')->translate($command->label), $command->href, $command->active);

    }

    return '';
}
_

この場合、_JSubmenuHelper::addEntry_をJHtmlSidebar関数に置き換える必要があります。

行を次のように変更しようとしました

_JHtmlSidebar::addEntry($this->getObject('translator')->translate($command->label), $command->href, $command->active);
_

そして

_JHtmlSidebar::render($this->getObject('translator')->translate($command->label), $command->href, $command->active);
_

しかし、addEntryではサイドバーは非表示になり、render()ではコンテンツがありません。

私がすでに試みた試みの何が問題になっていますか?

1
YoKoGFX

最初に、次のようにContentHelperを拡張するヘルパークラスを作成する必要があります。

administrator/components/com_mycomponent/Helper/MyComponentHelper.php

_use Joomla\CMS\Helper\ContentHelper;

class MyComponentHelper extends ContentHelper
{
    protected $config = null;

    public function __construct($config)
    {
        $this->config = new KObjectConfigJson($config);
        $this->config->append(array(
            'toolbar' => null
        ));
    }

    public static function addSubmenu()
    {
        foreach ($this->config->toolbar->getCommands() as $command)
        {
            if (!empty($command->href))
            {
                $command->href = $this->getTemplate()->route($command->href);
            }

            \JHtmlSidebar::addEntry(
                $this->getObject('translator')->translate($command->label),
                $command->href,
                $command->active
            );

        }
    }
}
_

次に、次のディレクトリで:

管理者/コンポーネント/com_mycomponent/controller.php

次のように、JLoaderを使用してクラスをロードします。

_JLoader::register('MyComponentHelper', JPATH_ADMINISTRATOR . '/components/com_mycomponent/helpers/contact.php');
_

完了したら、view.html.phpでクラスをインスタンス化する必要があります。

_MyComponentHelper::addSubmenu();
_

メモ:

  • 私の知る限り、_\JHtmlSidebar_の名前空間付きバージョンはないため、J4でサポートされるか、置き換えられるかはわかりません。
  • コンポーネントを実際に操作しないため、これをテストしていません
  • getObject()メソッドをクラスに追加するか、別のクラスからの場合は$this->getObject()参照を変更する必要があります。

お役に立てれば

1
Lodder