How to add new fields to e.g. the blog (or other) plugins?

Hello,

I'm sorry to bother you guys again with a surely very noobish problem, but I'm working hard on understanding the Elgg system without being a programmer (reading the manual, forum posts and lots of trial and error). I already got a better idea about how things work and are connected, but I lack the skills needed to easily change things to how I envision them.

With the help of Nikolai's hints in my other thread, I was able to "hide" or "delete" various buttons and other areas via "my_plugin" and through it I got a better idea about how to change things in Elgg. So far, so good.

Now I'm trying to learn how to add things. My learning project is: Add a field to the add/edit page in the "blog" plugin and make it show in the blog output page directly under the output of the description field in the "full view".

I found a "FieldsHandler.php" in the "blog" plugin folder which defines the various fields for the "blog" plugin. I copied the "description" field and changed the parameters. Let's call the new field "test". In the add/edit page, this looks exactly as I hoped and seems to work fine. Is this the correct method to add fields? If yes, can this file be copied to the "my_plugin" folder? If also yes, what would the correct folder be within the "my_plugin" folder? If no, what other option would I have to add a new blog-field to the add/edit pages via "my_plugin"? I read the concept of overriding views in the manual, but I basically would need once to read an actual full code for a specific example (like mine here) through which I can understand the pattern of it.

The new field is shown on the add/edit page, but isn't shown on the blog's output page. After going through "view.php" and "blog.php", I came to the conclusion that the answer might be in the "full view" function in "blog.php":

if (elgg_extract('full_view', $vars)) {
    $body = elgg_view('output/longtext', [
        'value' => $entity->description
        'class' => 'blog-post',
    ]);

I tried out various things, but to no avail. My train of thought is that the "test" field should be added here somewhere; something like 'value' => $entity->description,test

Is this the right idea? If yes, what would the correct method be? And what would be the correct folder for the mod/blog/views/default/object/blog.php file within the "my_plugin" structure? my_plugin/views/default/object/blog/blog.php? If no, where would I have add the "test" field, so that it shows in the "blog" posts?

Thank you for your help.

  • I copied the "description" field and changed the parameters. 

    This won't work.

    Don't forget that in addition to the fields in the form, you need to override this in the action.

  • OK. I will instead create a new blog plugin with the tutorial in the manual.

  • You can do it in your custom plugin.

    In the link I mentioned, I meant that you should customize both the form and action.

    Look at these examples: 1, 2, 3, 4, 5, 6

    It's just a very long tutorial to do in one reply.

    Try to learn the documentation and texamples provided, and then we'll help you if something goes wrong.

    Tip: deploy your code on GitHub/Gitlab so that developers can see your entire project.

  • Thanks a lot for your tips. I'm currently learning with the "My_Blog" tutorial (which is easy to follow) and read your examples and have a better understanding of how things are connected. I will have to test around a lot to see what I will be capable of doing. I won't post updates about this here for now, as it's just a lot of trial and error. Should my project be almost working, I will add the code to GitHub. For now it's too far away.

    Today I went a step back and instead of adding a new field, I played around with existing fields. As an example, I learned about the differences between text or longtext and how to change the CKEditor to the simple version. This all worked fine. Then I wanted to make the "excerpt" of a "blog" (from the original "blog" plugin) to also be shown in the "full view". I found this code (views/default/object/blog.php):

    if (elgg_extract('full_view', $vars)) {
        $body = elgg_view('output/longtext', [
            'value' => $entity->description
            'class' => 'blog-post',
        ]);
    
        $params = [
            'icon' => true,
            'show_summary' => true,
            'show_navigation' => true,
        ];
        $params = $params + $vars;
        
        echo elgg_view('object/elements/full', $params);
        
    } else {
        // brief view
        $params = [
            'content' => $entity->getExcerpt(),
            'icon' => true,
        ];
        $params = $params + $vars;
        echo elgg_view('object/elements/summary', $params);

    The "if" part defines what to show in the "full view" of the blog, the "else" part what to show in the "brief view" of e.g. the "all" list. My thought was to add the "excerpt" as a parameter or value to the "full view", so that it would also show on the blog page itself. Is this the right approach or is it more complicated than that?

  • It can be done, but I don't know what it will actually look like.

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