How to change default elgg entity icon

How can I go about listing a custom icon alongside an entity in Elgg? With my approach so far, the icon gets uploaded fine, but I'm unable to retrieve it successfully. I only get the default 'Gray Question Mark Icon' to show?

A user uploads an entity while adding an entity object like so (The action file):


This is the form:

    <div>
        <label><?php echo elgg_echo("bread: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>

To retrieve the entity, I start with:

Start.php

    // Register URL handlers for object
    elgg_register_plugin_hook_handler('entity:icon:url', 'rentals', 'bread_type_set_icon_url');

    function bread_type_set_icon_url($hook, $type, $url, $params) {
      $bread = $params['entity'];
      $size = $params['size'];

      $icontime = $bread->icontime;
      // handle missing metadata (pre 1.7 installations)
      if (null === $icontime) {
        $file = new ElggFile();
        $file->owner_guid = $bread->owner_guid;
        $file->setFilename("bread/" . $bread->guid . "large.jpg");
        $icontime = $file->exists() ? time() : 0;
        create_metadata($bread->guid, 'icontime', $icontime, 'integer', $bread->owner_guid, ACCESS_PUBLIC);
      }
      if ($icontime) {
        // return thumbnail
        return "bread_types/$bread->guid/$size/$icontime.jpg";
      }

      return elgg_get_simplecache_url("bread/default{$size}.gif");
    }

then, in views/default/resourses/bread.php:

    $content = elgg_list_entities(array(
        'type' => 'object',
        'subtype' => 'bread',
        'full_view' => false,
        'no_results' => elgg_echo("bread:none"),
        'preload_owners' => true,
        'preload_containers' => true,
        'distinct' => false,
    ));

    $body = elgg_view_layout('content', array(
        'filter_context' => 'all',
        'content' => $content,
    ));

    echo elgg_view_page($title, $body);


and in views/default/object/bread.php:

    $bread = elgg_extract('entity', $vars, FALSE);

    $bread_icon = elgg_view_entity_icon($bread, 'small');

    $params = array(
        'entity' => $bread,
    );

    $params = $params + $vars;
    $list_body = elgg_view('object/elements/summary', $params);

    echo elgg_view_image_block($bread_icon, $list_body);
  • I haven't checked but you can try it:

    elgg_register_plugin_hook_handler('entity:icon:url', 'object', 'bread_set_icon_url');
    
    function bread_set_icon_url($hook, $type, $url, $params) {
        $bread = $params['entity'];
        $size = $params['size'];
        
        if (elgg_instanceof($bread, 'object', 'bread')) {
            $url = "mod/bread/graphics/icons/$size.png";
            return $url;
        }
    }

    All in start.php

    Don't forget about your own icons in folder mod/bread/graphics/icons/ (large.png, medium.png, small.png etc..)

  • Thanks for the reply RvR, but that didn't change anything.

  • here's a hook i just wrote in elgg 2.1 to do this:

    function user_icon_url_override($hook, $type, $return, $params) {

      if (!elgg_instanceof($params['entity'], 'user')) {
        return $return;
      }

      // get our icon url
      $icontime = $params['entity']->icontime;
      if ($icontime) {
        return $return;
      }
        else {
            return elgg_get_site_url() . '_graphics/icons/user/default' . $params['size'] . '.gif';
        }
    }

    and here's the call for the hook in start.php of my theme:

        // over-ride default icons
        elgg_register_plugin_hook_handler('entity:icon:url', 'user', 'user_icon_url_override');

  • Have a look in the groups plugin. 
    Because the user can upload a group picture the plugin must display it instead of the user image. That is what you want assume.
    Look how the group plugin handles the custom image and make it in your plugin the same way.

    Look in the start.php of the groups plug in. You find an hook:  
    elgg_register_plugin_hook_handler('entity:icon:url', 'group', 'groups_set_icon_url');
    In the hook is a pagehandler: 
    elgg_register_page_handler('groupicon', 'groups_icon_handler');
    Modify "group" with "bread"
    and
    copy/paste and modify the functions of the hook and pagehandler point to.

    - - - - - 
    Note:

    Don't forget to add to your "bread"-pagehandler:
             case 'profile':
                // Page owner and context need to be set before elgg_view() is
                // called so they'll be available in the [pagesetup, system] event
                // that is used for registering items for the sidebar menu.
                // @see groups_setup_sidebar_menus()
                elgg_push_context('bread_profile');
                elgg_set_page_owner_guid($page[1]); 

  • Change the hook NOT to:
    elgg_register_plugin_hook_handler('entity:icon:url', 'bread', 'groups_set_icon_url');
    change it to:
    elgg_register_plugin_hook_handler('entity:icon:url', 'object', 'groups_set_icon_url');
    and filter in the function like:

    if (elgg_instanceof($breadentity, 'object', 'bread')) {

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