PHPackages                             d3yii2/d3labels - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. d3yii2/d3labels

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

d3yii2/d3labels
===============

Yii2 labels

1751[1 PRs](https://github.com/d3yii2/d3labels/pulls)PHP

Since Jul 15Pushed 5mo ago2 watchersCompare

[ Source](https://github.com/d3yii2/d3labels)[ Packagist](https://packagist.org/packages/d3yii2/d3labels)[ RSS](/packages/d3yii2-d3labels/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (6)Used By (0)

D3Labels
========

[](#d3labels)

Assign multiple labels to model

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist d3yii2/d3labels "*"

```

or add

```
"d3yii2/d3labels": "*"

```

to the require section of your `composer.json` file.

DB
--

[](#db)

[![DB strukture](https://github.com/d3yii2/d3labels/raw/master/doc/DbSchema.png)](https://github.com/d3yii2/d3labels/blob/master/doc/DbSchema.png)

Configuration
-------------

[](#configuration)

Add d3labels to migration path and run migration

```
  'migrate' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationPath' => [
                '@vendor/d3yii2/d3labels/migrations',
            ]
  ]
```

Add to modules d3labels

```
    'modules' => [
        'd3labels' => [
            'class' => 'd3yii2\d3labels\Module',
        ],
    ]
```

Define Labels by migration
--------------------------

[](#define-labels-by-migration)

Migration example for adding new label

```
use yii\db\Migration;
use \d3yii2\d3labels\logic\D3Definition;
use d3modules\lietvediba\models\RkInvoice;
use d3system\widgets\ThBadge;
class m190329_095047_invoice_labels extends Migration
{
    /**
    * {@inheritdoc}
    */
    public function safeUp()
     {
         $def = new D3Definition(RkInvoice::class);
         $def->setLabel('Warning label');
         $def->setColor(ThBadge::TYPE_INVERSE);
         $def->setCode('WarningLabel');
         //$def->setCompanyId(14);
         $def->save();
    }
    public function safeDown()
    {
        echo "m190329_095047_invoice_labels cannot be reverted.\n";
        return false;
    }

}
```

Migration example for removing label

```
use d3modules\d3accexport\logic\ExportRkInvoiceFormExtensions;
use d3modules\lietvediba\models\RkInvoice;
use d3yii2\d3labels\logic\D3LabelMaintenance;
use yii\db\Migration;

class m210426_100707_label_new_remove  extends Migration {

    public function safeUp() {
        $removedLabelsFromModelRecords = D3LabelMaintenance::removeLabel(ExportRkInvoiceFormExtensions::NEW,RkInvoice::class);
        echo 'Removed Labels FromModel Records: ' . $removedLabelsFromModelRecords .PHP_EOL;

    }

    public function safeDown() {
        echo "m210426_100707_label_new_remove cannot be reverted.\n";
        return false;
    }
}
```

Widget for creating labels
--------------------------

[](#widget-for-creating-labels)

Model controller
----------------

[](#model-controller)

Add actions d3labelsattach and d3labelsremove

Access rules

```
                  [
                        'allow' => true,
                        'actions' => [
                            'd3labelsattach',
                            'd3labelsremove',
                        ],
                        'roles' => [
                            'ModuleAdminRoleName',
                        ],
                    ],
```

Actions

```
    public function actions(): array
    {
        return [
            'd3labelsattach' => [
                'class' => AttachAction::class,
                'modelName' => D3pPerson::class,
            ],
            'd3labelsremove' => [
                'class' => DeleteAction::class,
                'modelName' => D3pPerson::class,
            ],
        ];
    }
```

Label admin Controller
----------------------

[](#label-admin-controller)

```
namespace cewood\cwstore\controllers;

use cewood\cwstore\models\CwbrProduct;
use ea\app\controllers\LayoutController;
use yii\filters\AccessControl;
use Yii;
use d3yii2\d3labels\components\CreateAction;
use d3yii2\d3labels\components\AttachAction;
use d3yii2\d3labels\components\DeleteAction;
use d3yii2\d3labels\components\DefinitionDeleteAction;

/**
 * Class RkInvoiceSettingsController
 * @package d3modules\lietvediba\controllers
 */
class SettingsController extends LayoutController
{
    /**
     * @inheritdoc
     */
    public function behaviors(): array
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => [
                            'labels',
                            'd3labelscreate',
                            'd3labelsdefinitionremove',
                        ],
                        'roles' => [
                            'ModuleAdminRoleName'
                        ]
                    ],
                ]
            ],
        ];

    }

    /**
     * @return array
     */
    public function actions()
    {
        return [
            'd3labelscreate' => [
                'class' => CreateAction::class,
                'modelName' => CwbrProduct::class,
                'sysCompanyId' => static function(){
                    return Yii::$app->SysCmp->getActiveCompanyId();
                }
            ],
            'd3labelsdefinitionedit' => [
                'class' => DefinitionEditAction::class,
                'showCode' => true,
            ],
            'd3labelsdefinitionremove' => [
                'class' => DefinitionDeleteAction::class,
                'modelName' => CwbrProduct::class,
                //'sysLabelsIdList' => [2]
            ],
        ];
    }

    /**
     * @return string
     */
    public function actionLabels(): string
    {
        return $this->render('labels', [
            'active' => true,
        ]);

    }

}
```

Label admin view

```
