Help with adding a link

I looked around for a detailed introduction to working in the framework, but only found the tutorial which pretty much assumes that you know how to work in the framework or that you've already written a plugin.  I try to trace through the code, but usually find 5-line functions that call a series of other 5-line functions.

Here's the "simple" thing that I'm trying to do:  add another "Add discussion topic" link to the beginning of the list of topics in a discussion, so users don't have to scroll down to find the link.

I found the text is from the language string "add:object:discussion".

I find this value used twice in the code:

  • vendor/elgg/elgg/mod/discussions/elgg-plugin.php
  • vendor/elgg/elgg/mod/discussions/views/default/resources/discussion/add.php

The first is defining a list of routes, so I assume that's not it.

The second is using the value when calling elgg_view_page, passing it in as the $title value.  That doesn't seem to be the right place either.

So, my questions:

  1. how is the current "Add discussion topic" link generated?
  2. how would I add the link to the top of the topic list?
  3. what's the best way to properly learn the framework?  The Developer Overview is too high-level, but the Developer Tutorial doesn't speak to beginners.

Thanks in advance.

  • how is the current "Add discussion topic" link generated?

    echo elgg_view('output/url', [
       'text' => elgg_echo('add:object:discussion'),
       'href' => elgg_generate_url('add:object:discussion', [
            'guid' => elgg_get_logged_in_user_guid(),
        ]),
    ]);

    how would I add the link to the top of the topic list?

    You must to override this file:

    mod\discussions\views\default\resources\discussion\view.php

    Needed changes are:

    $my_link = elgg_view('output/url', [
       'text' => elgg_echo('add:object:discussion'),
       'href' => elgg_generate_url('add:object:discussion', [
           'guid' => elgg_get_logged_in_user_guid(),
        ]),
    ]);
    
    $content = elgg_view_entity($topic, [
        'full_view' => true,
        'show_responses' => true,
    ]);
    
    
    echo elgg_view_page($topic->getDisplayName(), [
        'content' => $my_link . $content,
        'entity' => $topic,
        'filter_id' => 'discussion/view',
    ]);
    

    Read this how to do it.

    what's the best way to properly learn the framework?

    Read code.

    Try to create your custom plugin.

  • Thanks for the quick reply.  Based on those specifics and the links you provided, I tried to create my own plugin to extend 'discussions' with the new link:

    • made the mods/discussions_local directory
    • added a manifest.xml in this directory, showing a dependency on 'discussions'.
    • copied the view.php you referenced from the discussions heirarchy into the smilar heirarchy in my plugin's directory.
    • made the changes you described in my view.php
    • enabled the plugin in the Admin dashboard (and to confirm, it's listed after the original discussions plugin).
    • invalidated and flushed the caches

    Unfortunately, the new link doesn't appear.

    I logged out, tried different users, restarted apache, all with no luck.  I looked for an elgg-related log file, but didn't find anything.

    Can you spot what I missed, or provide some hints on how to debug it?

    Thanks again.  I think this framework will really fit my needs, so I'm hoping to be sucessful with it.

  • Just tested and this works very well for me.

    Only one error is a wrong guid: you should use container guid instead of user guid:

    $my_link = elgg_view('output/url', [
       'text' => elgg_echo('add:object:discussion'),
       'href' => elgg_generate_url('add:object:discussion', [
           'guid' => $topic->container_guid,
        ]),
    ]);

    Regarding you plugin I assume you forgot about start.php, Bootstrap class, or something else.

    Anyway, I made the dumbest plugin (with the best solution even) and you can try it right now.

    Enjoy!

     

  • Yeah, the other instructions just said to install the view file, so I appreciate the extra details.

    I tired using your sample plugin as a guide, but I never got the results that I was expecting.  I deleted that work and installed your sample plugin (activated, flushed), but still don't see the extra link.

    I turned logging up to "Log Everything" and don't see anything that seems relevant.

    I saw a logging entry elsewhere in the view, so I added a debug line to the discussion view code in both plugins (the original and yours) but I don't see the line in the logfiles:

             elgg_log("In discussion", 'INFO');

     

    So, I guess the current questions are:

    • how can I made sure the new plugin is loaded?  (is the fact that it shows as Activated enough?)
    • can you think of a step that I'm missing in installing your plugin?
    • how can i get debug info to appear in the logfiles?

    I really do appreciate the help, and hope that I can turn the corner on this soon!

     

  • 1 - Download this plugin.

    2 - Unzip it. Be sure folder name of plugin is discussions_ext and haven't any child folder with same name.

    Shortly, you will have this structure of this plugin:

    \discussions_ext\autoloader.php                                                                                                            
    \discussions_ext\classes                                                                                                                   
    \discussions_ext\elgg-plugin.php                                                                                                           
    \discussions_ext\manifest.xml                                                                                                              
    \discussions_ext\views                                                                                                                     
    \discussions_ext\classes\DiscussionsExt                                                                                                    
    \discussions_ext\classes\DiscussionsExt\Bootstrap.php                                                                                      
    \discussions_ext\views\default                                                                                                             
    \discussions_ext\views\default\object                                                                                                      
    \discussions_ext\views\default\object\discussion.php  

    3 - Replace this unzipped plugin in /mod directory of your Elgg root installed folder.

    4 - Go to Administration -> Plugins and activate it.

    That's all.

    Now go to any existed discussion on your site and check this button:

     

  • Thank you for the screen shot - it shows that you were working on a different page than I was expecting.  Your plugin worked perfectly; it was my request that was flawed.

    I had hoped to change the example.com/groups/profile/100/test-group page, which currently displays the following:

     

    I don't like that the "Add discussion topic" moves down with every new topic, thus making it harder for people to start new topics.  So, I'd like to either move that link above the topic shown (between the grey bar and the "test topic title"), or maybe add it where "View All" is, or add it as an option on the "Joined" pulldown in the top-right.

    Can you point me to those locations?

     

  • Just exploring based on the url, I see that the page I'm looking at is not part of /discussions, but rather part of /groups, and the reason that I didn't find "add:object:discussion" here is because it's generated dynamically for different content types (e.g. discussions, pages).

    It looks like a lot of the magic comes from groups/views/default/groups/profile/module.php, where the current "Add discussion topic" is part of $footer, the "View All" is part of $menu, and I could add it (not as attractively) to the beginning of $content generated for the section.

    Since the pattern seems to be to have my own plugin override the existing behavior, it looks like I should copy module.php into my own plugin and it will override the default one.  Since it needs to be loaded later than the original, I assume it's overwriting some registration before the page is rendered.  Can you point out where in your plugin code that it identifies itself as replacing a 'discussion' element?

  •  it was my request that was flawed

    Of course, this is a groups/module view not discussion.

    Copy

    mod\groups\views\default\groups\profile\module.php

    To same directory of your custom plugin and change this code on this:

    if($entity_subtype == 'discussion') {
        $add_topic = elgg_view('output/url', [
              'text' => elgg_echo('add:object:discussion'),
              'href' => elgg_generate_url('add:object:discussion', [
                    'guid' => $group->guid,
              ]),
              'class' => 'mrm',
         ]);
    }
    
    if (!empty($all_link)) {
        $menu = elgg_format_element('span', [
               'class' => 'groups-widget-viewall',
       ], $add_topic . $all_link);
    }

    This adds your desired link on the module at the top before 'View all'.

    Keep in your minds, it was actually for Discussions only not other subtypes.

    If you want to use it globally then change my code on:

    $add_entity = elgg_view('output/url', [
              'text' => elgg_echo("add:{$entity_type}:{$entity_subtype}"),
              'href' => elgg_generate_url("add:{$entity_type}:{$entity_subtype}", [
                    'guid' => $group->guid,
              ]),
              'class' => 'mrm',
    ]);
    
    if (!empty($all_link)) {
        $menu = elgg_format_element('span', [
               'class' => 'groups-widget-viewall',
       ], $add_entity . $all_link);
    }

    Don't forget: Clean the caches after the changes

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