2016-05-07 39 views
0

埃爾格2.0版如何將圖像圖標添加到埃爾格實體

我該如何去與影像圖標創建埃爾格實體,就像創建埃爾格組時,可以怎麼辦?

我試圖實現以下,但沒有圖像被上傳:

行動文件:

<?php 
// get the form inputs 
$title = get_input('title'); 
$body = get_input('body'); 
$tags = string_to_tag_array(get_input('tags')); 

// create a new my_blog object 
$blog = new ElggObject(); 
$blog->subtype = "nganya"; 
$blog->title = $title; 
$blog->description = $body; 

// for now make all my_blog posts public 
$blog->access_id = ACCESS_PUBLIC; 

// owner is logged in user 
$blog->owner_guid = elgg_get_logged_in_user_guid(); 

// save tags as metadata 
$blog->tags = $tags; 

// save to database and get id of the new my_blog 
$blog_guid = $blog->save(); 

// if the my_blog was saved, we want to display the new post 
// otherwise, we want to register an error and forward back to the form 
if ($blog_guid) { 
    system_message("Your nganya post was saved >>>>>>>>>> " . $blog_guid); 
    forward($blog->getURL()); 
} else { 
    register_error("The nganya post could not be saved"); 
    forward(REFERER); // REFERER is a global variable that defines the previous page 
} 

$has_uploaded_icon = (!empty($_FILES['icon']['type']) && substr_count($_FILES['icon']['type'], 'image/')); 

if ($has_uploaded_icon) { 

    $icon_sizes = elgg_get_config('icon_sizes'); 

    $prefix = "nganya/" . $blog->guid; 

    $filehandler = new ElggFile(); 
    $filehandler->owner_guid = $blog_guid; 
    $filehandler->setFilename($prefix . ".jpg"); 
    $filehandler->open("write"); 
    $filehandler->write(get_uploaded_file('icon')); 
    $filehandler->close(); 
    $filename = $filehandler->getFilenameOnFilestore(); 

    $sizes = array('tiny', 'small', 'medium', 'large', 'master'); 

    $thumbs = array(); 
    foreach ($sizes as $size) { 
     $thumbs[$size] = get_resized_image_from_existing_file(
      $filename, 
      $icon_sizes[$size]['w'], 
      $icon_sizes[$size]['h'], 
      $icon_sizes[$size]['square'] 
      ); 
    } 

    if ($thumbs['tiny']) { // just checking if resize successful 
     $thumb = new ElggFile(); 
     $thumb->owner_guid = $blog->owner_guid; 
     $thumb->setMimeType('image/jpeg'); 

     foreach ($sizes as $size) { 
      $thumb->setFilename("{$prefix}{$size}.jpg"); 
      $thumb->open("write"); 
      $thumb->write($thumbs[$size]); 
      $thumb->close(); 
     } 

     $blog->icontime = time(); 
    } 
} 

的形式爲:

<div> 
    <label><?php echo elgg_echo("groups:icon"); ?></label><br /> 
    <?php echo elgg_view("input/file", array("name" => "icon")); ?> 
</div> 

<div> 
    <label for="title"><?= elgg_echo("title"); ?></label><br /> 
    <?= elgg_view('input/text', ['name' => 'title', 'id' => 'title']); ?> 
</div> 

<div> 
    <label for="body"><?= elgg_echo("body"); ?></label><br /> 
    <?= elgg_view('input/longtext', ['name' => 'body', 'id' => 'body']); ?> 
</div> 

<div> 
    <label for="tags"><?= elgg_echo("tags"); ?></label><br /> 
    <?= elgg_view('input/tags', ['name' => 'tags', 'id' => 'tags']); ?> 
</div> 

<div> 
    <?= elgg_view('input/submit', ['value' => elgg_echo('save')]); ?> 
</div> 

回答

1

動作代碼如下正確。在埃爾格2.2,這將是輕鬆了許多,但在此期間:

  1. 您需要添加一個插件鉤子處理程序 'entity:icon:url',<entity_type>鉤,更換URL如果 $params['entity']是您要添加
  2. 的亞型
  3. 更新您的頁面處理程序以爲上述URL提供文件。