web-dev-qa-db-ja.com

PHP=に同じコードをコンポーネントと各モジュールのコンテンツに適用します

PHPのようなコードがあります:

$oldstring = ...;
...
$pattern = ...;
$newstring = preg_replace_callback($pattern, ... {...}, $oldstring);

echo $newstring;

このコードを(クラスまたは何らかの方法で)コンポーネントのコンテンツとページ上の既存の各モジュールに適用する必要があります。

$doc = JFactory::getDocument();

$oldcomponent = $doc->getBuffer('component', '', array('class'=> ''));
// the same code for applying
echo $newcomponent;

$oldmodule1 = $doc->getBuffer('modules', 'position-1', array('style'=> ''));
// the same code for applying
echo $newmodule1;

$oldmodule2 = $doc->getBuffer('modules', 'position-2', array('style'=> ''));
// the same code for applying
echo $newmodule2;

...

$oldmoduleN = $doc->getBuffer('modules', 'position-N', array('style'=> ''));
// the same code for applying
echo $newmoduleN;

2番目のコードは、私が言っているようなものにすぎません。つまり古い文字列はコンポーネントおよび各モジュールである必要があり、それらはページ上のその位置に新しい文字列としてエコーされる必要があります。

誰かがこの目的のために簡単なJoomlaプラグインを作成する方法の例を書くことができますか?ありがとう

5
stckvrw

わかりましたコンポーネントとモジュールをシステムプラグインで変更する方法:

class PlgSystemSomename extends JPlugin
{
    public function onAfterDispatch()
    {
        $doc = JFactory::getDocument();
        $oldComponent = $doc->getBuffer('component');
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);
        $query->select($db->quoteName('position'))
            ->from($db->quoteName('#__modules'))
            ->where($db->quoteName('published')." = 1 AND ".$db->quoteName('client_id')." = 0");
        $db->setQuery($query);
        $list = $db->loadObjectList();
        ...
        $pattern = ...;
        $newComponent = preg_replace_callback($pattern, ... {...}, $oldComponent);
        if(count($list) > 0) {
            foreach($list as $module) {
                $newModule = preg_replace_callback($pattern, ... {...}, $doc->getBuffer('modules', $module->position, array('style'=> 'xhtml')));
                $doc->setBuffer($newModule, 'modules', $module->position, array('style'=> 'xhtml'));
            }
        }

        $doc->setBuffer($newComponent, 'component');
        return true;
    }
}
1
stckvrw

プラグインを使用してJoomlaコンテンツを変更する方法には、さまざまな方法があります。

  • コンポーネントはプラグインメカニズムを呼び出すことができます。たとえば、com_contentは「onContentPrepare」イベントを使用します。これを使用して、com_contentコンテンツを変更できます。このためのプラグインはplugins/contentにありますが、これは単なる一般的な動作であり、プラグインはどのフォルダーでも機能します。

  • システムプラグインは、任意のコンポーネントの出力を取得して、たとえば正規表現で変更できます。

  • システムプラグインはHTML全体を変更することもできます

  • モジュールは「onRenderModule」イベントを呼び出すため、これを使用してモジュール出力を変更できます。

したがって、コンポーネント用のプラグインとモジュール用のプラグインを作成する必要があり、たとえばライブラリをインストールすることでコードを共有できます。

より詳細な例:

まとめると、次のようになります。

public function onRenderModule($module, $attribs)
{
    $modulePosition = $attribs['name']
    $moduleStyle = $attribs['style']

    $module->content = str_replace('foo','bar',$module->content;

}

これにより、すべてのモジュールでfooのすべての出現箇所がbarに置き換えられます。

3
jdog