/** @inheritdoc */ public function init() { parent::init(); if ($this->item instanceof Block) { $this->id = $this->item->id; $this->setAttributes($this->item->getAttributes()); $this->setIsNewRecord($this->item->getIsNewRecord()); } }
/** @inheritdoc */ public function bootstrap($app) { /** @var Module $module */ /** @var \yii\db\ActiveRecord $modelName */ if ($app->hasModule('content') && ($module = $app->getModule('content')) instanceof Module) { Yii::$container->setSingleton(ContentFinder::className(), ['categoryQuery' => \jarrus90\Content\Models\Category::find(), 'pageQuery' => \jarrus90\Content\Models\Page::find(), 'blockQuery' => \jarrus90\Content\Models\Block::find()]); if (!isset($app->get('i18n')->translations['content*'])) { $app->get('i18n')->translations['content*'] = ['class' => PhpMessageSource::className(), 'basePath' => __DIR__ . '/messages', 'sourceLanguage' => 'en-US']; } if (!$app instanceof ConsoleApplication) { $module->controllerNamespace = 'jarrus90\\Content\\Controllers'; $configUrlRule = ['prefix' => $module->urlPrefix, 'rules' => $module->urlRules]; if ($module->urlPrefix != 'content') { $configUrlRule['routePrefix'] = 'content'; } $configUrlRule['class'] = 'yii\\web\\GroupUrlRule'; $rule = Yii::createObject($configUrlRule); $app->urlManager->addRules([$rule], false); $app->params['admin']['menu']['content'] = function () use($module) { return $module->getAdminMenu(); }; } else { if (empty($app->controllerMap['migrate'])) { $app->controllerMap['migrate']['class'] = 'yii\\console\\controllers\\MigrateController'; } $app->controllerMap['migrate']['migrationNamespaces'][] = 'jarrus90\\Content\\migrations'; } } }
/** * Render block content * @return string */ public function run() { if ($this->key == NULL) { return false; } $key = $this->key; $lang = Yii::$app->language; $dependency = new TagDependency(['tags' => ["content_block_key_{$key}_{$lang}"]]); $block = Block::getDb()->cache(function ($db) use($key, $lang) { return Block::findOne(['key' => $key, 'lang_code' => $lang]); }, 3600, $dependency); if (!$block) { \Yii::error("Block '{$key}' not found", 'CMS/BlockWidget'); return false; } $this->_options = ArrayHelper::merge($this->_defaultOptions, $this->options); if ($block->content) { return $this->render($this->layout, ['block' => $block, 'config' => $this->_options]); } else { return false; } }