LoginSignup
2
1

More than 5 years have passed since last update.

[concrete 8.2+] Expressオブジェクトとセレクトボックス属性をパッケージインストールで追加する

Last updated at Posted at 2017-10-20

この記事で書かれていること

  • concrete Ver.8 から実装されたExpressオブジェクトをパッケージからインストールするサンプルコード
  • Expressオブジェクトにセレクトボックス属性をコードで追加する方法

この記事で割愛されていること

  • Expressオブジェクトの操作、説明
  • concreteでパッケージインストールの操作、説明

サンプルコードの前に

concreteでは、独自に実装したコードをパッケージという単位でまとめてインストールできます。

GUIで同じ操作は可能なのですが、初期値の設定や大量の属性などは一括でインストールしたいですし、一元管理して保守性を高めたいところ。

Ver.8 から追加されたExpressオブジェクトは、O/RマッパーのためのデータモデルをGUIで作成できるものです。

Expressオブジェクトの基本操作はこちらにまとまっています。
http://c5blog.xross-cube.com/express%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%81%A8%E3%81%AF/

ただデータインポート機能はまだないので、データ移行でCSVなどから一括インポートしたいのであれば自前での実装が必要です。
https://concrete5-japan.org/community/forums/chat/post-14882

まだドキュメントが整備中のため、コメント欄などに実装例があったりする状況です。

ここではパッケージインストールからExpressオブジェクトを作成し、セレクトボックスの属性を追加する実装を例示します。
以下、コメント欄での内容を参考にしています。
https://documentation.concrete5.org/developers/attributes/working-with-attribute-keys/creating-attribute-keys-in-a-package

サンプルコード

packages/my/controller.php
use \Concrete\Core\Package\Package;
use \Concrete\Core\Block\BlockType\BlockType;
use \Concrete\Core\Entity\Attribute\Key\Settings\SelectSettings;
use Concrete\Core\Entity\Attribute\Value\Value\SelectValue;
use Concrete\Core\Entity\Attribute\Value\Value\SelectValueOptionList;
use Concrete\Core\Entity\Attribute\Value\Value\SelectValueOption;
use Express;

class Controller extends Package
{

...

    public function install()
    {
        $pkg = parent::install();
        $this->installExpressObjects();
    }

    private function installExpressObjects()
    {
        $object = Express::buildObject('object', 'objects', 'Expressオブジェクト', $pkg);

        $object->addAttribute('select', '性別', 'object_sex', $this->selectSettings(['男', '女']));
        $object->save();
    }

    private function selectSettings($options, $isMultiple=false)
    {
        $settings = new SelectSettings();
        $list = new SelectValueOptionList();
        $list->setOptions($this->selectOptions($options, $list));
        $settings->setOptionList($list);
        if($isMultiple)
        {
            $settings->setAllowMultipleValues(true);
        }
        return $settings;
    }

    private function selectOptions($optionArray, $list)
    {
        $options = [];
        $displayOrder = 0;
        foreach($optionArray as $option)
        {
            $o = new SelectValueOption();
            $o->setSelectAttributeOptionValue($option);
            $o->setDisplayOrder($displayOrder);
            $o->setOptionList($list);
            if(is_object($o))
            {
                $options[] = $o;
                ++$displayOrder;
            }
        }

        return $options;
    }

...

}

2
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
2
1