How to add custom fields in Elgg entities

I am new to Elgg and trying to add some custom fields in Elgg Forms and trying to submit it to the database. I trie d adding new fields (example : tag2) to an example tutorial. I tried addding an attribute in views/pages and action similar to the one like tag but not able to see a text field for tag2.

http://docs.elgg.org/wiki/Tutorials/Blog

Is here any good reference you can point me in acheving the functionality or if someone has gone through this can shed some light on this would be really great.

Also, I am trying to understand from the data model perspective as to which tables does the extra attributes information will go in.

Once agin, thanks in advance for your help.

  • You already have found the reference needed ... maybe you're doing some small mistake, somewhere which prevents the action to be trigerred properly?

  • Thanks Shouvik. Would you be able to help out if I can send you the zip of the sample code? Also, is there any document explaning the data model of all the underlying MYSQL tables that it has created during the installation.

  • I am trying to add an attribute (tag2)  in the above example tutorial. Please let me know of any issues

    1. Add the following

    echo elgg_view('output/tags2', array('tags2' => $vars['entity']->tags2));
     
    2.  Add the following below ( at /mod/my_blog/views/default/forms/my_blog/save.php)

    <div>
    <label><?php echo elgg_echo("tags2"); ?></label><br />
    <?php echo elgg_view('input/tags2', array('name' => 'tags2')); ?>
    </div>


    3. Add the following below ( save.php in /mod/my_blog/actions/my_blog/)
    $tags2 = get_input('tags2');
    $blog->tags2 = $tags2; ( the tags2 is not accessible here)

    PLease let me know if I am missing anything here to be able to view the tags2 field and submit the data into the database.


  • Have you created views for 'input/tag2' and 'output/tag2'?

    If not then you will get no markup generated.  You can either create the views, or use one of the system views eg.

     

    elgg_view('input/tags', array('name' => 'tags2'));

  • Yes,  I have created the view. Not sure how/where the tag2 is getting linked with the entity. I am trying to understand the life cycle/process of adding a new field. I mean what are the places we need to modify in order to add a new field.

  • Please correct me if I am doing anything wrong here.

     

    1. Register the Entity in start.php

    <?php

    elgg_register_event_handler('init', 'system', 'my_blog_init');

    /**
    * Init blog plugin.
    */
    function my_blog_init() {

    elgg_register_library('elgg:my_blog', elgg_get_plugins_path() . 'bookmarks/lib/bookmarks.php');

    }

     

    2. Added the lib function in the lib/my_blog.php

    function my_blog_prepare_form_vars($my_blog = null) {
    echo "in lib";
    $values = array(
    'title' => get_input('title', ''), // bookmarklet support
    'description' => '',
    'tags' => '',
    'tags2' => '',
    'entity' => $my_blog,
    );

    ......

     

    }

     

    3. Added the entry in views/object/my_blog.php

    echo elgg_view('output/text', array('tags2' => $my_blog->tags2));

     

    4. Add the entry in the form save.php

    <div>
    <label><?php echo elgg_echo("tags2"); ?></label><br />
    <?php echo elgg_view('input/text', array('name' => 'tags2')); ?>
    </div>

     

    5. Added the following in the pages folder ( pages/my_blog/add.php)

    $vars = my_blog_prepare_form_vars();
    echo "vars = ".$vars;
    // add the form to this section
    $content .= elgg_view_form("my_blog/save" , array(), $vars);

     

    6. Added the following in actions ( actions/my_blog/save.php)

    $tags2 = get_input('tags2');

    $my_blog->tags2 = $tags2;  ( I cannot access tags2 here: tags2 not found in the Elgg Object).

     

    Not very clear on the life cycle or the process flow. Is there any good reference in understanding the process flow and any pointers on the above as to what is wrong?

     

    Any help is really appreciated.

     

  • sorry.. typo on the first step.

     

    elgg_register_library('elgg:my_blog', elgg_get_plugins_path() . 'my_blog/lib/my_blog.php');

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