|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * GraphQL Srcset plugin for Craft CMS 3.x |
| 4 | + * |
| 5 | + * Adds the @srcset GraphQL directive for generating a comma-separated list of image transforms. |
| 6 | + * |
| 7 | + * @link https://tas.dev |
| 8 | + * @copyright Copyright (c) 2020 Jayden Smith |
| 9 | + */ |
| 10 | + |
| 11 | +namespace tasdev\graphqlsrcset; |
| 12 | + |
| 13 | +use Craft; |
| 14 | +use craft\base\Plugin; |
| 15 | +use craft\events\RegisterGqlDirectivesEvent; |
| 16 | +use craft\services\Gql; |
| 17 | +use craft\services\Plugins; |
| 18 | +use craft\events\PluginEvent; |
| 19 | +use craft\helpers\UrlHelper; |
| 20 | +use craft\web\twig\variables\CraftVariable; |
| 21 | + |
| 22 | +use yii\base\Event; |
| 23 | + |
| 24 | +use tasdev\graphqlsrcset\models\Settings; |
| 25 | +use tasdev\graphqlsrcset\gql\directives\SrcSet; |
| 26 | +use tasdev\graphqlsrcset\variables\GraphqlSrcset as GraphqlSrcsetVariable; |
| 27 | + |
| 28 | +/** |
| 29 | + * Class GraphqlSrcset |
| 30 | + * |
| 31 | + * @author Jayden Smith |
| 32 | + * @package GraphqlSrcset |
| 33 | + * @since 1.0.0 |
| 34 | + * |
| 35 | + */ |
| 36 | +class GraphqlSrcset extends Plugin |
| 37 | +{ |
| 38 | + // Static Properties |
| 39 | + // ========================================================================= |
| 40 | + |
| 41 | + /** |
| 42 | + * @var GraphqlSrcset |
| 43 | + */ |
| 44 | + public static $plugin; |
| 45 | + |
| 46 | + |
| 47 | + // Public Properties |
| 48 | + // ========================================================================= |
| 49 | + |
| 50 | + /** |
| 51 | + * @var string |
| 52 | + */ |
| 53 | + public $schemaVersion = '1.0.0'; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var bool |
| 57 | + */ |
| 58 | + public $hasCpSettings = true; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var bool |
| 62 | + */ |
| 63 | + public $hasCpSection = false; |
| 64 | + |
| 65 | + |
| 66 | + // Public Methods |
| 67 | + // ========================================================================= |
| 68 | + |
| 69 | + /** |
| 70 | + * @inheritdoc |
| 71 | + */ |
| 72 | + public function init() |
| 73 | + { |
| 74 | + parent::init(); |
| 75 | + |
| 76 | + $this->_registerEventHandlers(); |
| 77 | + $this->_registerVariable(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @inheritdoc |
| 82 | + */ |
| 83 | + public function getSettingsResponse() |
| 84 | + { |
| 85 | + return Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('graphql-srcset/settings')); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + // Protected Methods |
| 90 | + // ========================================================================= |
| 91 | + |
| 92 | + /** |
| 93 | + * @inheritdoc |
| 94 | + */ |
| 95 | + protected function createSettingsModel() |
| 96 | + { |
| 97 | + return new Settings(); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + // Private Methods |
| 102 | + // ========================================================================= |
| 103 | + |
| 104 | + /** |
| 105 | + * Register the event handlers. |
| 106 | + */ |
| 107 | + private function _registerEventHandlers() |
| 108 | + { |
| 109 | + Event::on(Gql::class, Gql::EVENT_REGISTER_GQL_DIRECTIVES, function (RegisterGqlDirectivesEvent $event) { |
| 110 | + $event->directives[] = SrcSet::class; |
| 111 | + }); |
| 112 | + |
| 113 | + // Redirect after plugin install |
| 114 | + Event::on(Plugins::class, Plugins::EVENT_AFTER_INSTALL_PLUGIN, function (PluginEvent $event) { |
| 115 | + if ($event->plugin === $this) { |
| 116 | + Craft::$app->getGql()->flushCaches(); |
| 117 | + |
| 118 | + if (Craft::$app->getRequest()->isCpRequest) { |
| 119 | + Craft::$app->getResponse()->redirect( |
| 120 | + UrlHelper::cpUrl('graphql-srcset/settings') |
| 121 | + )->send(); |
| 122 | + } |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Register Auctions template variable |
| 129 | + */ |
| 130 | + private function _registerVariable() |
| 131 | + { |
| 132 | + Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $event) { |
| 133 | + /** @var CraftVariable $variable */ |
| 134 | + $variable = $event->sender; |
| 135 | + $variable->set('graphqlSrcset', GraphqlSrcsetVariable::class); |
| 136 | + }); |
| 137 | + } |
| 138 | +} |
0 commit comments