Skip to content

Instantly share code, notes, and snippets.

@YuzuruSano
Last active May 22, 2017 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YuzuruSano/4b86434c2bf95054462cfde2f9255b1d to your computer and use it in GitHub Desktop.
Save YuzuruSano/4b86434c2bf95054462cfde2f9255b1d to your computer and use it in GitHub Desktop.
【concrete5 5.7系】 画像をファイルマネージャーに登録し、指定ページのページ属性に登録する
<?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