/** * @param ActiveRecord $model * @param null|string $language * @param array|null $metaTagsToFetch */ public static function register(ActiveRecord $model, $language = null, $metaTagsToFetch = null) { $metaTags = MetaTagContent::find()->where([MetaTagContent::tableName() . '.model_id' => $model->id])->andWhere([MetaTagContent::tableName() . '.model_name' => (new \ReflectionClass($model))->getShortName()])->joinWith(['metaTag']); if (is_array($metaTagsToFetch)) { $metaTags->andWhere([MetaTag::tableName() . '.name' => $metaTagsToFetch]); } else { $metaTags->andWhere([MetaTag::tableName() . '.is_active' => 1]); } if (!is_string($language)) { $language = Yii::$app->language; } $metaTags->andWhere([MetaTagContent::tableName() . '.language' => $language]); /** @var MetaTagContent[] $metaTags */ $metaTags = $metaTags->all(); foreach ($metaTags as $metaTag) { $content = $metaTag->content; if (!empty($content)) { if (strtolower($metaTag->metaTag->name) === MetaTag::META_TITLE_NAME) { Yii::$app->getView()->title = $content; } else { if ($metaTag->metaTag->name) { Yii::$app->view->registerMetaTag([$metaTag->metaTag->is_http_equiv ? 'http-equiv' : 'name' => $metaTag->metaTag->name, 'content' => $content], $metaTag->metaTag->name); } } } } }
/** * @param ActiveRecord $model * @param null|string $language * @param array|null $metaTagsToFetch */ public static function register(ActiveRecord $model, $language = null, $metaTagsToFetch = null) { $metaTags = MetaTagContent::find()->where([MetaTagContent::tableName() . '.model_id' => $model->id])->andWhere([MetaTagContent::tableName() . '.model_name' => $model->formName()])->joinWith(['metaTag']); if (is_array($metaTagsToFetch)) { $metaTags->andWhere([MetaTag::tableName() . '.name' => $metaTagsToFetch]); } else { $metaTags->andWhere([MetaTag::tableName() . '.is_active' => 1]); } if (!is_string($language)) { $language = Yii::$app->language; } $metaTags->andWhere([MetaTagContent::tableName() . '.language' => $language]); /** @var MetaTagContent[] $metaTags */ $metaTags = $metaTags->all(); foreach ($metaTags as $metaTag) { $content = $metaTag->getMetaTagContent(); foreach ($model->attributes as $name => $value) { $content = str_replace("[{$name}]", $value, $content); } if (!empty($content)) { if (strtolower($metaTag->metaTag->name) === MetaTag::META_TITLE_NAME) { Yii::$app->getView()->title = $content; } elseif (strtolower($metaTag->metaTag->name) === MetaTag::META_SEO_TEXT) { self::$seoText .= $content; } elseif (strtolower($metaTag->metaTag->name) === MetaTag::META_ROBOTS) { if ($content) { Yii::$app->view->registerMetaTag(['name' => 'robots', 'content' => 'noindex, FOLLOW'], 'robots'); } } else { if ($metaTag->metaTag->name) { Yii::$app->view->registerMetaTag([$metaTag->metaTag->is_http_equiv ? 'http-equiv' : 'name' => $metaTag->metaTag->name, 'content' => $content], $metaTag->metaTag->name); } } } } }
/** * Get list of active meta tags * * @return MetaTag[] */ protected function getTagList() { return MetaTag::find()->where('is_active = :is_active', [':is_active' => 1])->orderBy(['position' => SORT_DESC])->all(); }
/** * @return \yii\db\ActiveQuery */ public function getMetaTag() { return $this->hasOne(MetaTag::className(), ['id' => 'meta_tag_id']); }
/** * Finds the MetaTag model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return MetaTag the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = MetaTag::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }