Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enh: Make state badge customizable #6697

Merged
merged 1 commit into from Dec 7, 2023

Conversation

martin-rueegg
Copy link
Contributor

@martin-rueegg martin-rueegg commented Dec 2, 2023

Currently, the \humhub\modules\content\widgets\StateBadge does require the StateBadge::$model to be a ContentActiveRecord. However, states may also be used on other records.

This PR allows to override where the widget gets the state from. Two steps are required:

  1. Extend the StateBadge class, overriding the getState() method returning the value from the custom source, e.g.
     public function getState(): ?int
     {
         return $this->model->state;
     }
  2. Register an event to overwrite the class, e.g. in config.php add
    [
             'class' => \humhub\modules\content\widgets\StateBadge::class,
             'event' => \humhub\components\Widget::EVENT_CREATE,
             'callback' => [
                 MyStateBadge::class,
                 'onWidgetCreate',
             ]
    ]
    ```

A full example of the class, including additional states would be:

<?php

/**
 * @copyright    2023. metaworx resonare rüegg, Martin Rüegg, Switzerland
 * @license      https://gitlab.com/metaworx/open-source/humhub/metamarket/-/raw/master/LICENCE.md MIT
 * @package      metaworx\humhub\modules\metamarket
 */

namespace metaworx\humhub\modules\metamarket\widgets;

use humhub\libs\Html;
use humhub\libs\WidgetCreateEvent;
use humhub\modules\content\models\Content;
use humhub\modules\content\widgets\StateBadge;
use metaworx\humhub\modules\metamarket\models\AdPublication;
use metaworx\humhub\modules\metamarket\models\AdStatus;
use Yii;

/**
 * @property AdPublication $model
 */
class AdStateBadge
    extends StateBadge
{
    // protected properties
    protected ?int $state = null;

    public function getState(): ?int
    {
        return $this->state ??= $this->model->ad->status;
    }

    public function afterRun($result)
    {
        if ($result === '')
        {
            switch ($this->getState())
            {
            case Content::STATE_PUBLISHED:
                if ($this->model->isOwner())
                {
                    $result = Html::tag(
                        'span', Yii::t('ContentModule.base', 'Published'),
                        ['class' => 'label label-info label-state-published']
                    );
                }
                break;

            case AdStatus::AD_STATUS_EXPIRED:
                $result = Html::tag(
                    'span', Yii::t('MetamarketModule.base', 'Expired'),
                    ['class' => 'label label-warning label-state-expired']
                );
                break;

            case AdStatus::AD_STATUS_BLOCKED:
                $result = Html::tag(
                    'span', Yii::t('MetamarketModule.base', 'Blocked'),
                    ['class' => 'label label-danger label-state-blocked']
                );
                break;

            default:
                $result = 'test';
            }
        }

        return parent::afterRun($result);
    }

    public static function onWidgetCreate(WidgetCreateEvent $event)
    {
        $event->config['class'] = self::class;
    }

}

PR Admin

Could this still be added to v1.15 or even 1.14? Happy to rebase, if yes.

What kind of change does this PR introduce?

  • Feature

Does this PR introduce a breaking change?

  • No

###The PR fulfills these requirements

  • It's submitted to the develop branch, not the master branch if no hotfix
  • All tests are passing
  • New/updated tests are included
  • Changelog was modified

@martin-rueegg martin-rueegg changed the title Enh: make state badge cutsomizable Enh: Make state badge customizable Dec 2, 2023
@martin-rueegg martin-rueegg force-pushed the enh/make-state_badge-cutsomizable branch from 76e8997 to 31062f5 Compare December 2, 2023 18:10
@luke- luke- added this pull request to the merge queue Dec 7, 2023
Merged via the queue into humhub:develop with commit 79bab3b Dec 7, 2023
6 checks passed
@luke-
Copy link
Contributor

luke- commented Dec 7, 2023

@martin-rueegg Thanks. We can integrate this small change into 1.15.1. Unfortunately I saw it too late. If you want, open a new PR against master.

I have copied the documentation into the wiki as well.
https://community.humhub.com/s/contribution-core-development/wiki/States

@martin-rueegg
Copy link
Contributor Author

@martin-rueegg Thanks. We can integrate this small change into 1.15.1. Unfortunately I saw it too late. If you want, open a new PR against master.

Ok, done.

I have copied the documentation into the wiki as well. https://community.humhub.com/s/contribution-core-development/wiki/States

Awesome!

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants