Last active
May 22, 2017 11:29
【concrete5 5.7系】 画像をファイルマネージャーに登録し、指定ページのページ属性に登録する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//もう少しエラーハンドリングちゃんと書いたほうが良い | |
//画像を登録したいページをIDで指定 | |
$target = Page::getByID(ページID); | |
//ファイルインポータクラスを準備 | |
$importer = new \Concrete\Core\File\Importer(); | |
//importメソッドでファイルを登録 | |
//下記例ではテーマファイル内の画像を指定 | |
//引数はファイルパス,ファイル名,「置き換えにするか」どうか? | |
//第三引数に既にアップしたファイルオブジェクトを渡すと「置き換え」になるみたい※未検証 | |
$newFile = $importer->import('/xxx/application/themes/test_theme/images/target_img.jpg', 'target_img.jpg',false); | |
//無事インポートできると$newFileに\Concrete\Core\File\Versionのインスタンスが入る | |
if (is_object($newFile)) { | |
//ファイルバージョンオブジェクトからファイルオブジェクトを取得 | |
$newFileOnject = $newFile->getFile(); | |
//ページ属性に登録 | |
//setAttributeメソッドはハンドル + 登録データを指定すれば大体動いてくれるので便利。 | |
$target->setAttribute('登録したいページ属性ハンドル', $newFileOnject); | |
} | |
//ページリストオブジェクトと組み合わせてアイキャッチの一括登録とか | |
//自動実行ジョブ化すると大量の画像登録 + ページとの関連付けもできる | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment