nystudio107 /
craft-similar
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Similar plugin for Craft CMS 3.x |
||
| 4 | * |
||
| 5 | * Similar for Craft lets you find elements, Entries, Categories, Commerce Products, etc, that are similar, based on... other related elements. |
||
| 6 | * |
||
| 7 | * @link https://nystudio107.com/ |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 8 | * @copyright Copyright (c) 2018 nystudio107.com |
||
|
0 ignored issues
–
show
|
|||
| 9 | */ |
||
|
0 ignored issues
–
show
|
|||
| 10 | |||
| 11 | namespace nystudio107\similar; |
||
| 12 | |||
| 13 | use Craft; |
||
| 14 | use craft\base\Element; |
||
| 15 | use craft\base\Plugin; |
||
| 16 | use craft\elements\db\ElementQuery; |
||
| 17 | use craft\events\PopulateElementEvent; |
||
| 18 | use craft\web\twig\variables\CraftVariable; |
||
| 19 | use nystudio107\similar\behaviors\CountBehavior; |
||
| 20 | use nystudio107\similar\services\ServicesTrait; |
||
| 21 | use nystudio107\similar\variables\SimilarVariable; |
||
| 22 | use yii\base\Event; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Class Similar |
||
| 26 | * |
||
| 27 | * @author nystudio107.com |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 28 | * @package Similar |
||
|
0 ignored issues
–
show
|
|||
| 29 | * @since 1.0.0 |
||
|
0 ignored issues
–
show
|
|||
| 30 | */ |
||
|
0 ignored issues
–
show
|
|||
| 31 | class Similar extends Plugin |
||
| 32 | { |
||
| 33 | // Traits |
||
| 34 | // ========================================================================= |
||
| 35 | |||
| 36 | use ServicesTrait; |
||
| 37 | |||
| 38 | // Static Properties |
||
| 39 | // ========================================================================= |
||
| 40 | |||
| 41 | /** |
||
|
0 ignored issues
–
show
|
|||
| 42 | * @var ?Similar |
||
| 43 | */ |
||
| 44 | public static ?Similar $plugin = null; |
||
| 45 | |||
| 46 | // Public Properties |
||
| 47 | // ========================================================================= |
||
| 48 | |||
| 49 | /** |
||
|
0 ignored issues
–
show
|
|||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | public string $schemaVersion = '1.0.0'; |
||
| 53 | |||
| 54 | /** |
||
|
0 ignored issues
–
show
|
|||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | public bool $hasCpSection = false; |
||
| 58 | /** |
||
|
0 ignored issues
–
show
|
|||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | public bool $hasCpSettings = false; |
||
| 62 | |||
| 63 | // Public Methods |
||
| 64 | // ========================================================================= |
||
| 65 | |||
| 66 | /** |
||
|
0 ignored issues
–
show
|
|||
| 67 | * @inheritdoc |
||
| 68 | */ |
||
|
0 ignored issues
–
show
|
|||
| 69 | public function init(): void |
||
| 70 | { |
||
| 71 | parent::init(); |
||
| 72 | self::$plugin = $this; |
||
| 73 | |||
| 74 | Event::on( |
||
| 75 | CraftVariable::class, |
||
| 76 | CraftVariable::EVENT_INIT, |
||
| 77 | static function(Event $event): void { |
||
|
0 ignored issues
–
show
|
|||
| 78 | /** @var CraftVariable $variable */ |
||
|
0 ignored issues
–
show
|
|||
| 79 | $variable = $event->sender; |
||
| 80 | $variable->set('similar', SimilarVariable::class); |
||
| 81 | } |
||
| 82 | ); |
||
| 83 | Event::on( |
||
| 84 | ElementQuery::class, |
||
| 85 | ElementQuery::EVENT_AFTER_POPULATE_ELEMENT, |
||
| 86 | static function(PopulateElementEvent $event): void { |
||
|
0 ignored issues
–
show
|
|||
| 87 | /** @var Element $element */ |
||
|
0 ignored issues
–
show
|
|||
| 88 | $element = $event->element; |
||
| 89 | $element->attachBehavior('myCountBehavior', CountBehavior::class); |
||
| 90 | }); |
||
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
Loading history...
|
|||
| 91 | |||
| 92 | Craft::info( |
||
| 93 | Craft::t( |
||
| 94 | 'similar', |
||
| 95 | '{name} plugin loaded', |
||
| 96 | ['name' => $this->name] |
||
| 97 | ), |
||
| 98 | __METHOD__ |
||
| 99 | ); |
||
| 100 | } |
||
| 101 | |||
| 102 | // Protected Methods |
||
| 103 | // ========================================================================= |
||
| 104 | } |
||
| 105 |