IMUZA.com

Xserver<<WordPress(ConoHa)<<はてなブログ

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

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

2017/07/21 Joomla!

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

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

  • static parameters を挿入する php ファイル
    • 3.1 以上では、protected $autoloadLanguage = true; が必要
  • 引数 $context, &$article, &$params の値
    • $context
    • $article
    • $params
    • $limitstart
  • onContentBeforeDisplay では記事表示以外はイベントが発生しない?

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 では記事表示以外はイベントが発生しない?

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

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

Joomla! の Plugin を自作する(2)初期設定ページ
Joomla! 3.7.4 セキュリティフィックスがリリースされています
Twitter
Facebook
ブックマーク
LINEで送る

最初のサイドバー

最新記事

2023/09/26

IntersectionObserverで要素の位置を知る

2023/09/14

GDPRを設定する

2023/08/30

scroll-behavior:smoothは履歴に残る

2023/08/22

要素がトップにきたことを感知する-IntersectionObserver

2023/08/8

ホームページとは別に投稿一覧ページをつくる方法-WordPress

最新記事を一覧で見る

よく読まれている記事

よく読まれている記事を一覧で見る

カテゴリー

  • はてなブログ215
  • WebTips110
  • javascript101
  • Joomla!88
  • Wordpress79
  • Windows68
  • CSS64
  • Joomla!更新53
  • Linux49
  • はてなテーマ45
  • Google36
  • Plamo33
  • はてなプラグイン25
  • php23
  • Node.js18
  • Ubuntu16
  • SASS16
  • laravel415
  • Chrome11
  • cms-style10
  • iPhone9
  • Git入門6
  • ConoHa WING6
  • genesis6
  • Python5
  • Android5
  • PC全般4
  • Facebook4
  • スマートフォン4
  • Xserver3
  • Firefox3
  • 静的サイトジェネレーター3
  • SSD3
  • Docker3
  • Blankslate3
  • Twitter2
  • Mactype2
  • GitHub2
  • youtube1
  • はてなブクマ1
  • rails入門1
  • 映画1

Footer

My Web Sites

  • @半径とことこ60分
  • そんなには褒めないよ。映画評
  • IMUZA.com
  • GitHub

Related Sites

  • WordPress公式
  • WordPress関数リファレンス
  • PHPマニュアル

Contact Us

  • お問い合わせフォーム
  • Twitter
  • Facebook
  • Feedly

Copyright © 2023 · IMUZA.com