Adding a newssection or blog

Now that we've covered the main topics, let's have a look at some extra utilities bundles we've prepared. Since quite a lot of the sites we make need some kind of news section - with news authors, news pages and a news overview page - we decided to create a bundle to make implementing these a lot easier.

Generating the articles skeleton code

First up is the article generator :

app/console kuma:generate:article

This will first ask for the bundle namespace (you can accept the default - MyProject/WebsiteBundle), then it will ask for a name (enter News), and finally it will ask for the table name prefix, so enter myproject_websitebundle_ as before.

The basic code skeleton should now be generated, so go ahead and create (and apply) a migration for the database changes :

app/console doctrine:migrations:diff && app/console doctrine:migrations:migrate

As you can see, 3 new tables will be generated. One contains details for the authors, one will contain news overview pages and the last one will contain the actual news page.

When you have a look at the admin section (/app_dev.php/en/admin/), you'll notice that there 2 new menu options (News and News Authors) have been added to the Modules menu. These menu items will allow you to add news articles and authors respectively, but you will not be able to add articles as no NewsOverviewPage has been created yet.

Since we would like to be able to add a News page in the main navigation on the homepage, we first have to enable this, so go ahead and open src/MyProject/WebsiteBundle/Entity/Pages/HomePage.php and add the NewsOverviewPage to getPossibleChildTypes :

    /**
     * @return array
     */
    public function getPossibleChildTypes()
    {
    return array(
        ...
        ),
        array(
        'name' => 'News Overview Page',
        'class'=> 'MyProject\WebsiteBundle\Entity\News\NewsOverviewPage'
        )
    );
    }

After adding this snippet, you should be able to add a news overview page on the homepage in the backend, so go ahead and do that - and make sure you publish it after it is created.

That's it! Your news overview page is created, and when you add (and publish!) news articles in the backend, you should see them appearing on your news overview page.

If needed you can add extra fields to the generated entities as you see fit (don't forget to create and apply migrations afterwards!).

2) Summary

Adding support for news articles to your site is as simple as this :

app/console kuma:generate:article

3) Under the hood

Note: The following paths assume you entered 'News' as name in the generator.

  • src/YourVendor/YourWebsiteBundle/AdminList/News/ contains the admin lists for news authors and news pages (articles).
  • src/YourVendor/YourWebsiteBundle/Controller/News/ contains the controllers for the admin lists for news authors and news pages (articles).
  • src/YourVendor/YourWebsiteBundle/Entity/News/ contains all generated entities (ie. entities for the news overview page, news detail page and news author).
  • src/YourVendor/YourWebsiteBundle/Form/News/ contains all form AdminType definitions generated by the article generator (ie. for the news overview page, news detail page and news author).
  • src/YourVendor/YourWebsiteBundle/Helper/Menu/NewsMenuAdaptor.php contains the menu adaptor that adds the extra menu options to the Modules menu.
  • src/YourVendor/YourWebsiteBundle/PagePartAdmin/News/ contains the page part admin configurators for the news overview and news detail page (these determine the page parts that can be added to these pages).
  • src/YourVendor/YourWebsiteBundle/Repository/ contains repositories for the generated entities.
  • src/YourVendor/YourWebsiteBundle/Resources/views/AdminList/ contains a Twig view for the News page admin list.
  • src/YourVendor/YourWebsiteBundle/Resources/views/News/