Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139<?php/** * Splashing Images plugin for Craft CMS 3.x * * unsplash.com integration for Craft 3 * * @link https://studioespresso.co * @copyright Copyright (c) 2017 Studio Espresso */
namespace studioespresso\splashingimages;
use Craft;use craft\base\Model;use craft\base\Plugin;use craft\events\PluginEvent;use craft\events\RegisterUrlRulesEvent;use craft\helpers\UrlHelper;use craft\services\Plugins;use craft\web\UrlManager;use studioespresso\splashingimages\models\Settings;use studioespresso\splashingimages\services\UnsplashService;use yii\base\Event;
/** * Craft plugins are very much like little applications in and of themselves. We’ve made * it as simple as we can, but the training wheels are off. A little prior knowledge is * going to be required to write a plugin. * * For the purposes of the plugin docs, we’re going to assume that you know PHP and SQL, * as well as some semi-advanced concepts like object-oriented programming and PHP namespaces. * * https://craftcms.com/docs/plugins/introduction * * @author Studio Espresso * @package SplashingImages * @since 1.0.0 * * @property UnsplashService $unsplash * @property Settings $settings * @method Settings getSettings() */class SplashingImages extends Plugin{ // Static Properties // =========================================================================
/** * Static property that is an instance of this plugin class so that it can be accessed via * SplashingImages::$plugin * * @var SplashingImages */ public static $plugin;
public function init(): void { parent::init(); self::$plugin = $this; // Register our CP routes Event::on( UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) { $event->rules['splashing-images'] = 'splashing-images/default/index'; $event->rules['splashing-images/<page:\d+>'] = 'splashing-images/default/index'; $event->rules['splashing-images/find'] = 'splashing-images/default/find'; $event->rules['splashing-images/search'] = 'splashing-images/default/search'; $event->rules['splashing-images/search/<page:\d+>'] = 'splashing-images/default/search'; } );
Event::on( Plugins::class, Plugins::EVENT_AFTER_INSTALL_PLUGIN, function(PluginEvent $event) { if ($event->plugin->id === $this->id) { // Redirect to plugin settings $request = Craft::$app->getRequest(); if ($request->isCpRequest) { Craft::$app->getResponse()->redirect(UrlHelper::cpUrl( 'settings/plugins/splashing-images' ))->send(); } } } ); }
// Protected Methods // =========================================================================
/** * Creates and returns the model used to store the plugin’s settings. **/ protected function createSettingsModel(): Model { return new Settings(); }
/** * @inheritdoc */ public function getCpNavItem(): array { $navItem = [ 'label' => $this->getSettings()->pluginLabel ? $this->getSettings()->pluginLabel : 'Unsplash Images', 'url' => $this->id, ];
if (($iconPath = $this->cpNavIconPath()) !== null) { $navItem['icon'] = $iconPath; }
return $navItem; }
/** * Returns the rendered settings HTML, which will be inserted into the content * block on the settings page. * * @return string The rendered settings HTML */ protected function settingsHtml(): string { $volumes = Craft::$app->getVolumes(); foreach ($volumes->getAllVolumes() as $source) { $destinationOptions[] = array('label' => $source->name, 'value' => $source->handle); } return Craft::$app->view->renderTemplate( 'splashing-images/settings', [ 'settings' => $this->getSettings(), 'volumes' => $destinationOptions ?? null, ] ); }}