Forum Widgets

License Latest Stable Version Total Downloads Buy Me a Coffee

Flarum Core Extension for Managing Forum Widgets.

Animated Screenshot

🪶 Introduction

Howdy 👋, while working on a Flarum theme I wanted to install some of the existing widgets in the community and customise them for that theme to spice it up a little, unfortunately the fact that every widget adds itself into the layout in its own way with its own custom structure and CSS classes, helps neither the admin of the forum to manage them, nor the theme designer to customise them. So I decided to build this little package to normalise things a bit and focus the role of placing the widgets and managing them here, while the actual widgets can be separate extensions focusing on what they actually provide.

This is still a relatively simple package however, right now it allows extension developers to register a widget per extension and select a default location out of 5, and allows the admin to override that and move them around. This already helps a ton by normalising the design of the widgets and giving a certain amount of control.

I also created 4 simple widgets that can currently be used, links are in the installation section below.

🏁 Future Goals

  • Options to override widget titles, icons and descriptions.
  • Support multi widgets (widgets that can be added multiple times, such as HTML widgets perhaps).
  • Allow widgets to be shown in more pages than the index (this might require Flarum 2.0).
  • Add more sections to insert widgets (might also require Flarum 2.0).
  • More widgets ? The community can also do that, this is the whole point.

Suggestions should be posted here and/or the github repository!

📥 Installation

Remember that this is just a forum widgets editor, it doesn't actually come with any widgets, if you wish to install it with the existing widgets listed below in one go, you can use the bundle:

$ composer require afrux/forum-widgets-bundle:"*"

When updating the bundle:

composer update afrux/forum-widgets-bundle --with-dependencies

Otherwise you can just install this package alone:

composer require afrux/forum-widgets-core:"*"

Here is a list of currently compatible widgets you can install:

⬆️ Updating

composer update afrux/forum-widgets-core --with-dependencies
php flarum migrate
php flarum cache:clear

🧑‍💻 Extend

Extension developers wanting to create widgets with this small framework, the following explains how you can register a new widget, for now you should only register one widget per extension.

  1. Require this extension in your extension's composer.json:

    "require": {
      "flarum/core": "^1.0.0",
      "afrux/forum-widgets-core": "^0.1.0"
    },
  2. Create your widget's component in common/components by extending the base Widget component provided with this package.

    import Widget from 'flarum/extensions/afrux-forum-widgets-core/common/components/Widget';
    
    export default class MyWidget extends Widget {
      className() {
        // Custom class name.// You can also use the class "AfruxWidgets-Widget--flat" for a flat widget (not contained in a block).
        // Please avoid strong custom styling so that it looks consistent in other themes.
        return 'MyWidget';
      }
    
      icon() {
        // Widget icon.
        return 'fas fa-cirlce';
      }
    
      title() {
        // Widget title.
        // Can return empty for a titleless widget.
        return app.translator.trans('afrux-online-users-widget.forum.widget.title');
      }
    
      content() {
        return (
          <div className="MyWidget-content">
            // ...
          </div>
        );
      }
    }
  3. Register your widget in the admin and forum frontends:

  4. Create a new registerWidget.js file in common:

    import Widgets from 'flarum/extensions/afrux-forum-widgets-core/common/extend/Widgets';
    
    import MyWidget from './components/MyWidget';
    
    export default function(app) {
      (new Widgets).add({
        key: 'onlineUsers',
        component: MyWidget,
        
        // Can be a callback that returns a boolean value.
        // example: () => app.forum.attribute('myCustomExtension.mySetting')
        isDisabled: false,
        
        // Is this a one time use widget ? leave true if you don't know.
        isUnique: true,
        
        // The following values are default values that can be changed by the admin.
        placement: 'end',
        position: 1,
      }).extend(app, 'my-extension-id');
    };
  5. Register the widget in both frontends admin/index.js & forum/index.js:

    import registerWidget from '../common/registerWidget';
    
    app.initializers.add('my-extension-id', () => {
      registerWidget(app);
    });
  6. If you are using typescript, you can add the typings of this package by adding this to the paths key in your tsconfig.json file:

    "flarum/extensions/afrux-forum-widgets-core/*": ["../vendor/afrux/forum-widgets-core/js/dist-typings/*"]

You can also checkout other example widgets in the Afrux github org.

Links

    Amazing! I have no words to explain how much I'm excited to see Widgets in Flarum ecosystem. Sycho keeps on surprising us 🎉

    [deleted] btw, guests will most likely not be able to see the online users and top posters widget, because those two rely on permission to search the users API at the moment. And I believe guests can't search the list by default.

      • [deleted]

      SychO Fine with me. I have a "super sized" layout running on hostrisk.com so will need to play with this a bit to get the best out of it. The ability to drag and drop the widgets where you want them is amazing.

        @SychO in beast mode today, this is super cool... obviously a must have for every flarum install 🙂

        [deleted]

        One thing I noticed as guest, I couldn't see the forum stats widget for more than a second because it's placed under the discussion list which has autoload enabled. It's not the fault of the widget, maybe it should go above or perhaps allow a different placement for mobile? Not sure what you guys think the best solution there is?

          • [deleted]

          ctml yes, I noticed that also. I think the placement on mobile should be hidden probably. I think I'll just add CSS to handle that

          I haven't thoroughly checked things on mobile yet, so if you notice anything odd feel free to report here or in the github repo.

          Wow, @SychO congratulations for this release is a really big thing for Flarum customization, I love the pick and drop widgets to position them, reminds me of Blogger I really liked that feature. I thought that something like this wouldn't be possible until 2.0... Now if I was already looking forward to 2.0 now I can't even imagine what you developers will be able to do in the future with 2.0.

          SychO Is it works only in desktop mode? how can with mobile?

            Awesome extension. Installed the bundle but didn't want to use TopPosters so I disabled it again. After that, it broke. On the Widget extension nothing is displayed, getting this error:

            TypeError: Cannot read property 'icon' of undefined
            at e.o.widget (WidgetEditor.tsx:201)
            at e.o.layoutWidget (WidgetEditor.tsx:213)
            at WidgetEditor.tsx:65
            at Array.map (<anonymous>)
            at e.o.editor (WidgetEditor.tsx:65)
            at e.o.content (WidgetEditor.tsx:35)
            at e.n.sections (ExtensionPage.js:96)
            at e.forEach.t.<computed> [as sections] (extend.js:33)
            at e.n.view (ExtensionPage.js:52)
            at Function.s (render.js:30)
            s @ mount-redraw.js:15

              GreXXL This most likely occurs due to the widget still being used and saved to your current layout in the Forum Widgets extension settings page. If you re-enabled the Top Posters Widget extension, remove the widget from your layout by moving it back to the available widgets area, you should be okay to disable that widget again 🙂 in hindsight, I'm sure @SychO will add a way to check if the widget is still used in the layout or simply remove it from your saved layout once the specified widget extension is disabled.

                Ralkage tried - not coming back. Had to remove it altogether. Also re-installing it didn't help. Guess I would have to edit the database to get it fixed?

                🔔 v0.1.3

                • Fixes a bug with disabled widget extensions. @GreXXL this should fix your issue.
                • Mobile improvements.

                fakruzaruret that will look better when I add the option for you to overwrite widget titles.

                Gatsu It works on mobile as well, some sections are disabled on tablet because of the default layout change, but most sections show.