How to add an image icon to an Elgg entity

**Elgg  Version 2.0**

How can I go about creating an Elgg entity with an image icon, just like how one can do when creating an Elgg group?

I've tried implementing the following, but no image gets uploaded:

Action file:

    // 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);
    } 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");
        $filename = $filehandler->getFilenameOnFilestore();

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

        $thumbs = array();
        foreach ($sizes as $size) {
            $thumbs[$size] = get_resized_image_from_existing_file(

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

            foreach ($sizes as $size) {

            $blog->icontime = time();

The Form:

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

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

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

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

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

  • If possible, I recommend you to wait until Elgg 2.2.0 gets released. It will have a lot of enhancements related to entity icons.

    Compare for example these two:

  • You are creating a new entity of subtype "nganya" using "new ElggObject". I haven't looked closely at every line of your code but I think the problem is already starting here. You might better createa specific class for your subtype and you should derive this class from the ElggFile class. Then your class (and therefore every entitiy of this class) will have all the functions from the original ElggFile class.

    You might want to take a look at some existing plugins as a starting point to see how you can re-use already existing code for your plugin. Examples are the core file plugin, and you could also check out for example my Gifts plugin ( and my Elggx Badges plugin ( The Gifts plugin works without a class and the Badges plugin uses a class derived from ElggFile (though it does not create thumbnails for the uploaded image files). The BlogTools plugin ( might also have some example code.

    I would suggest to study existing plugins that have some corresponding file upload functionality and then create your plugin step-by-step. Most likely you will need to do a lot of testing if you haven't much experience with coding on Elgg plugins yet. But it is also very difficult / nearly impossible for others to guide you with only some code snippets available that can't be tested. It can help to add some debug output (for example using elgg_dump or var_dump to log for example input values the action retrieves to see if the values are what is expected).

  • @Juho you mean each entity has the option to have its own (not user) icon? :)

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking