Joomla! の Plugin を自作する(3)デフォルトの静的データをメタタグに挿入する

Joomla! の Plugin を自作する(3)デフォルトの静的データをメタタグに挿入する

Joomla! の Plugin を自作する(2)初期設定ページ の続きです。

Open Graph protocol のメタタグを自動挿入するプラグインを作っています。前回で static なパラメータを設定できるようになりましたので、その値をメタタグに挿入して表示する過程でいろいろ分かったことがありますのでそれをメモしておきます。

static parameters を挿入する php ファイル

<?php
// No direct access
defined( '_JEXEC' ) or die;


class plgContentImzOpenGraph extends JPlugin {


    protected $autoloadLanguage = true;
    
    function onContentBeforeDisplay( $context, &$article, &$params ) {


        $document = JFactory::getDocument();
        $document->addCustomTag('<meta property="og:title" content="' . $this->params->get('imzopengraphtitle') . '"/>');
        $document->addCustomTag('<meta property="og:type" content="' . $this->params->get('imzopengraphtype') . '"/>');
        $document->addCustomTag('<meta property="og:image" content="' . JURI::base(false) . $this->params->get('imzopengraphimage') . '"/>');
        $document->addCustomTag('<meta property="og:url" content="' . $this->params->get('imzopengraphurl') . '"/>');
        $document->addCustomTag('<meta property="og:description" content="' . $this->params->get('imzopengraphdesc') . '"/>');
        $document->addCustomTag('<meta property="og:site_name" content="' . $this->params->get('imzopengraphsitename') . '"/>');


        return true;
    }
}

これで、プラグインの設定ページで指定したデフォルトデータはメタタグとして挿入できます。

で、いくつか分かったことや変更点などがあります。

3.1 以上では、protected $autoloadLanguage = true; が必要

protected $autoloadLanguage = true; を入れておかないと、プラグインで使用する言語定数が変換されません。たとえば、プラグインで echo JTEXT::_('PLG_IMZOPENGRAPH_TITLE'); とした場合、

  • $autoloadLanguage 指定なし ____ PLG_IMZOPENGRAPH_TITLE と言語定数のまま表示
  • $autoloadLanguage 指定あり ____ ページのタイトル と変換され表示

J3.x:Creating a Plugin for Joomla – Joomla! Documentation のサンプルファイルにコメントがありました。

/**

* Load the language file on instantiation. Note this is only available in Joomla 3.1 and higher.

* If you want to support 3.0 series you must override the constructor

*

* @var boolean

* @since 3.1

*/

3.1 以上では、この1行で言語ファイルは読み込まれますが、3.0 ではコンストラクタを上書きしなさいと言っています。

引数 $context, &$article, &$params の値

onContentBeforeDisplay が取るパラメータは、$context, &$article, &$params, $limitstart があり、それぞれ次の値を持っています。

  • context : The context of the content passed to the plugin.
  • article : A reference to the article that is being rendered by the view.
  • params : A reference to an associative array of relevant parameters. The view determines what it considers to be relevant and passes that information along.
  • limitstart : An integer that determines the “page” of the content that is to be generated. Note that in the context of views that might not generate HTML output, a page is a reasonably abstract concept that depends on the context.

下手に訳すよりも実際に値を見てみましょう。

$context

  • com_content.article ____ 記事ページ
  • com_content.category ____ カテゴリブログやカテゴリ一覧ページ
  • com_content.featured ____ 注目記事
  • com_tags.tag ____ タグページ

などの値を持っているようです。他にもあるかも知れませんが、そのページがどんなページかを現しているのだと思います。

$article

これは記事のあらゆるデータを持っています。

object(stdClass)#409 (50) {
[“id”]=>
string(3) “173”
[“asset_id”]=>
string(3) “279”
[“title”]=>
string(9) “テスト”
[“alias”]=>
string(19) “2017-05-10-04-54-50”
[“introtext”]=>
string(16) “

テスト


[“fulltext”]=>
string(0) “”
[“state”]=>
string(1) “1”
[“catid”]=>
string(2) “11”
[“created”]=>
string(19) “2017-05-10 04:54:50”

こんな感じですので、ここから記事のタイトル、url、ディスクリプションは取れそうです。

$params

これは記事のオプション設定データでしょう。

プラグインの static parameters は、$this->parames から取り出せます。

$limitstart

これは今のところよく分かりません。

onContentBeforeDisplay では記事表示以外はイベントが発生しない?

これはもう少し調べようと思います。

なかなか難しいですね。次回です。