2017 © Pedro Peláez
 

yii2-extension yii2-wavecms-news

News module for WaveCMS

image

mrstroz/yii2-wavecms-news

News module for WaveCMS

  • Tuesday, July 17, 2018
  • by mrstroz
  • Repository
  • 1 Watchers
  • 0 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 67 % Grown

The README.md

yii2-wavecms-news

News module for Yii 2 WaveCMS., (*1)

Please do all install steps first from Yii 2 WaveCMS., (*2)

Installation

The preferred way to install this extension is through composer., (*3)

Run, (*4)

composer require --prefer-source "mrstroz/yii2-wavecms-news" "~0.4.0"

or add, (*5)

"mrstroz/yii2-wavecms-news": "~0.4.0"

to the require section of your composer.json file., (*6)

Required

  1. Update backend/config/main.php (Yii2 advanced template)
'modules' => [
    // ...
    'wavecms-news' => [
        'class' => 'mrstroz\wavecms\news\Module',
        /*
         * Override classes
        'classMap' => [
            'News' => 'common\models\News',
        ]
        */
    ],
],

Form views can be overwritten by backend themes;, (*7)

  1. Update frontend/config/main.php (Yii2 advanced template)
'modules' => [
    'sitemap' => [
        'class' => 'himiklab\sitemap\Sitemap',
        'models' => [
            'mrstroz\wavecms\news\models\News',
        ],
        'urls' => [
            [
                'loc' => ['/'],
                'changefreq' => \himiklab\sitemap\behaviors\SitemapBehavior::CHANGEFREQ_DAILY,
                'priority' => 1,
            ]
        ],
        'cacheExpire' => 1
    ]
],
// ...
'components' => [
    'urlManager' => [
        'rules' => [
            // Add rule for sitemap.xml
            ['pattern' => 'sitemap', 'route' => 'sitemap/default/index', 'suffix' => '.xml'],
        ],
    ],
]
  1. Run migration

Add the migrationPath in console/config/main.php and run yii migrate:, (*8)

// Add migrationPaths to console config:
'controllerMap' => [
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationPath' => [
            '@vendor/mrstroz/yii2-wavecms-news/migrations',
        ],
    ],
],

Or run migrates directly, (*9)

yii migrate/up --migrationPath=@vendor/mrstroz/yii2-wavecms-news/migrations

Overriding classes

Classes can be overridden by: 1. classMap attribute for WaveCMS module, (*10)

'modules' => [
    // ...   
    'wavecms-news' => [
        'class' => 'mrstroz\wavecms\news\Module',
        'classMap' => [
            'News' => \common\models\News::class
        ]
    ],
],
  1. Yii2 Dependency Injection configuration in backend/config/main.php
'container' => [
    'definitions' => [
        mrstroz\wavecms\news\models\News::class => common\models\News::class
    ],
],

Overriding controllers

Use controllerMap attribute for WaveCMS module to override controllers, (*11)

'modules' => [
    // ...   
    'wavecms' => [
        'class' => 'mrstroz\wavecms\news\Module',
        'controllerMap' => [
            'news' => 'backend\controllers\NewsController'
        ]
    ],
],

Overriding views

Use themes for override views, (*12)

'components' => [
    // ...
    'view' => [
        'theme' => [
            'basePath' => '@app/themes/basic',
            'baseUrl' => '@web/themes/basic',
            'pathMap' => [
                '@wavecms_news/views' => '@app/themes/basic/wavecms-news',
            ],
        ],
    ],
    // ...
],

Usage in frontend

News

  1. Add new rules to your urlManager. You can do it in one of your Bootstrap classes
<?php
use mrstroz\wavecms\news\models\News;
use Yii;
// ...
//Parse request to set language before run ActiveRecord::find()
Yii::$app->urlManager->parseRequest(Yii::$app->request);

/** @var Settings $settings */
$settings = Yii::$app->settings;
$link = $settings->get('NewsSettings_' . Yii::$app->language, 'overview_link');

$model = Yii::createObject(News::class);

Yii::$app->getUrlManager()->addRules([
    $link => 'news/index'
]);

$news = $model::find()->select(['link'])->byAllCriteria()->byType(['news'])->column();

if ($news) {
    Yii::$app->getUrlManager()->addRules([
        $link . '/<link:(' . implode('|', $news) . ')>' => 'news/detail'
    ]);
}
  1. Get list of news on overview page
<?php

use mrstroz\wavecms\news\models\News;
use Yii;
use yii\data\Pagination;

public function actionIndex() {
    /** @var Settings $settings */
    $settings = Yii::$app->settings;
    $query = News::find()->byAllCriteria()->byType('news')->orderBy('create_date DESC');
    $count = $query->count();

    $pagination = new Pagination([
        'totalCount' => $count,
        'pageSize' => $settings->get('NewsSettings', 'news_on_page')
    ]);

    $news = $query->offset($pagination->offset)
        ->limit($pagination->limit)
        ->all();

    return $this->render('index', [
        'news' => $news,
        'pagination' => $pagination
    ]);
}
  1. Display news in view 'index.php'
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;

/** @var \mrstroz\wavecms\news\models\News[] $news */
/** @var \yii\data\Pagination $pagination */

/** @var \yii2mod\settings\components\Settings $settings */
$settings = Yii::$app->settings;
$link = $settings->get('NewsSettings_' . Yii::$app->language, 'overview_link');

if ($news) {
    foreach ($news as $oneNews) {
        echo Html::a($oneNews->title, [$link . '/' . $oneNews->link]);
        echo '<br />';
    }
}

echo LinkPager::widget([
    'pagination' => $pagination,
]);

  1. Get requested news by link in detail action
<?php
use mrstroz\wavecms\news\models\News;
// ...
public function actionDetail($link)
{
    $news = News::find()->getByLink($link)->one();

    return $this->render('detail', [
        'news' => $news
    ]);
}

Meta tags

See yii2-wavecms-metatags, (*13)

Add news to sitemap

According to Sitemap module, we need to add behaviour to our AR model and then add model to sitemap module configuration (see frontend/config/main.php), (*14)

use himiklab\sitemap\behaviors\SitemapBehavior;

public function behaviors()
{
    return [
        'sitemap' => [
            'class' => SitemapBehavior::className(),
            'scope' => function ($model) {
                /** @var \yii\db\ActiveQuery $model */
                $model->select(['url', 'lastmod']);
                $model->andWhere(['is_deleted' => 0]);
            },
            'dataClosure' => function ($model) {
                /** @var self $model */
                return [
                    'loc' => Url::to($model->url, true),
                    'lastmod' => strtotime($model->lastmod),
                    'changefreq' => SitemapBehavior::CHANGEFREQ_DAILY,
                    'priority' => 0.8
                ];
            }
        ],
    ];
}

Used packages

  1. CKEditor https://github.com/MihailDev/yii2-ckeditor
  2. ElFinder https://github.com/MihailDev/yii2-elfinder
  3. Slugify https://github.com/powerkernel/yii2-slugify
  4. Select2 https://github.com/kartik-v/yii2-widget-select2
  5. Datepicker https://github.com/kartik-v/yii2-widget-datepicker
  6. Switch widget https://github.com/2amigos/yii2-switch-widget
  7. Sitemap - https://github.com/himiklab/yii2-sitemap-module

INWAVE LOGO
INWAVE - Internet Software House
inwave.eu, (*15)

The Versions