3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

concrete5 のトピックの配列の並び替えをきちんと行う

Last updated at Posted at 2016-06-08
  • 更新日: 2016/6/8

concrete5 のトピック機能は便利ですが、並び替えでハマったので。共有します。

PHP の配列 (array) の並び替えを、格納されているオブジェクト (object) の特定のプロパティー (property) で並び替える方法のサンプルでもあります。

前提条件

  • concrete5.7.5.6~8 で動作確認済み
  • [管理画面] - [システムと設定] - [属性] - [トピック] より、「お知らせ」トピックを作成
    • 「お知らせ」「プレスリリース」「採用情報」の順にトピックを登録する
    • ドラッグアンドドロップで並び順を変えて「採用情報」「お知らせ」「プレスリリース」とする
  • [管理画面] - [ページとテーマ] - [属性] より、「お知らせ」のトピック属性 (news_type)を作成
  • お知らせページタイプを作成
  • お知らせのトピック属性を登録したページを作る。
    • 「採用情報」「お知らせ」「プレスリリース」をすべて選択して保存する
  • ページタのテーマやページリストなどで、トピックが表示されるようなテーマテンプレートやブロックのカスタムテンプレート (下記サンプルコード参照) を作成する。

サンプルコード

ページテンプレートでトピックを表示させるサンプルコードは下記のようにします。

<?php
$topics = $c->getAttribute('news_type');
if (count($topics) && is_array($topics)) {
	foreach($topics as $topic) {
	    echo "「" . $topic->getTreeNodeDisplayName() . "」";
	}
}
?>

(これが、ページリストだったら、$c->getAttribute('news_type')$page->getAttribute('news_type') になったりします。)

問題

トピックの表示順を、管理画面で設定した並び順、「採用情報」「お知らせ」「プレスリリース」としたいのに、なぜか「お知らせ」「プレスリリース」「採用情報」と登録順で表示されてしまう。

解決策

$topics の中身を、設定した並び順で並び替えてから表示するようにします。

<?php
function orderNode($a, $b) {
    return strcmp($a->treeNodeDisplayOrder, $b->treeNodeDisplayOrder);
}


$topics = $c->getAttribute('news_type');
if (count($topics) && is_array($topics)) {

    usort($topics, "orderNode");

    foreach($topics as $topic) {
        $newscategory = $topic->getTreeNodeDisplayName();
    }
}
?>

usort というユーザー関数でソートします。 orderNode() という関数を追加し、表示の処理をしています。

原因

試しに

<?php
$topics = $c->getAttribute('news_type');
var_dump($topics);
?>

してみると、$topics は下記のように出てきます。

array(3) {
  [0]=>
  object(Concrete\Core\Tree\Node\Type\Topic)#1893 (13) {
    ["childNodes":protected]=>
    array(0) {
    }
    ["childNodesLoaded":protected]=>
    bool(false)
    ["treeNodeIsSelected":protected]=>
    bool(false)
    ["tree":protected]=>
    NULL
    ["error"]=>
    string(0) ""
    ["treeNodeID"]=>
    string(2) "10"
    ["treeNodeTypeID"]=>
    string(1) "3"
    ["treeID"]=>
    string(1) "1"
    ["treeNodeParentID"]=>
    string(2) "1"
    ["treeNodeDisplayOrder"]=>
    string(1) "1"
    ["treeNodeOverridePermissions"]=>
    string(1) "0"
    ["inheritPermissionsFromTreeNodeID"]=>
    string(2) "1"
    ["treeNodeTopicName"]=>
    string(12) "お知らせ"
  }
  [1]=>
  object(Concrete\Core\Tree\Node\Type\Topic)#1889 (13) {
    ["childNodes":protected]=>
    array(0) {
    }
    ["childNodesLoaded":protected]=>
    bool(false)
    ["treeNodeIsSelected":protected]=>
    bool(false)
    ["tree":protected]=>
    NULL
    ["error"]=>
    string(0) ""
    ["treeNodeID"]=>
    string(2) "11"
    ["treeNodeTypeID"]=>
    string(1) "3"
    ["treeID"]=>
    string(1) "1"
    ["treeNodeParentID"]=>
    string(2) "1"
    ["treeNodeDisplayOrder"]=>
    string(1) "2"
    ["treeNodeOverridePermissions"]=>
    string(1) "0"
    ["inheritPermissionsFromTreeNodeID"]=>
    string(2) "1"
    ["treeNodeTopicName"]=>
    string(12) "プレスリリース"
  }
  [2]=>
  object(Concrete\Core\Tree\Node\Type\Topic)#1888 (13) {
    ["childNodes":protected]=>
    array(0) {
    }
    ["childNodesLoaded":protected]=>
    bool(false)
    ["treeNodeIsSelected":protected]=>
    bool(false)
    ["tree":protected]=>
    NULL
    ["error"]=>
    string(0) ""
    ["treeNodeID"]=>
    string(2) "12"
    ["treeNodeTypeID"]=>
    string(1) "3"
    ["treeID"]=>
    string(1) "1"
    ["treeNodeParentID"]=>
    string(2) "1"
    ["≈"]=>
    string(1) "0"
    ["treeNodeOverridePermissions"]=>
    string(1) "0"
    ["inheritPermissionsFromTreeNodeID"]=>
    string(2) "1"
    ["treeNodeTopicName"]=>
    string(8) "採用情報"
  }
}

オブジェクトの treeNodeDisplayOrder で並び順通りになっていますが、array には、登録順で格納されてしまうからでした。

宣伝 & クレジット

この記事が役に立ったら、「 CMS は concrete5 が一番」と頭の中で10回唱えてください。

CMS は断然 concrete5で決まり。コンクリートファイブジャパン株式会社がサポートできます。

以上。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?