Search code examples
symfonysymfony-cmf

Adding page in SimpleCMSBundle programmatically


I am looking for a way to add pages programmatically in SimpleCMSBundle. I found the following command line.

php app/console doctrine:phpcr:migrator page --identifier=/cms/simple/test

What I need is first a programmatic equivalent for the above command, secondly the above command assumes that there exists a .yml file located at 'app/Resources/data/ pages/test.yml, I want to provide this information programmatically too.

I am using Symfony CMF Standard edition.


Solution

  • The SimpleCmsBundle provides 2 ways of creating pages:

    1. Creating a yaml file which will be parsed

    This is done by a DataFixture. The AcmeDemoBundle comes with such a yaml file, which you can use to add your own page:

    # src/Acme/MainBundle/Resources/data/page.yml
    static:
        # ...
    
        my_new_page:
            name: "my_page"
            label: "My Page"
            title: "My new Page"
            body: "I've added this when following instructions on SO"
    

    Now you need to execute php app/console doctrine:phpcr:fixtures:load, which will execute the DataFixtures, and the new page is created!

    2. Persisting the documents

    What's really done with the yaml data is that a Page document is created and persisted in the PHPCR tree. Inside a DataFixture, or in a controller/admin where you want to add a page, do the following:

    use Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr\Page;
    
    // ...
    $page = new Page();
    $page->setName('my_other_page');
    $page->setLabel('My other Page');
    $page->setTitle('My other Page');
    $page->setBody('I\'ve added this myself!!');
    
    $documentManager = ...; // get the document manager, the first argument of DataFixture::load or $container->get('doctrine_phpcr')->getManager()
    
    $root = $documentManager->find(null, '/cms/simple'); // get the root document
    $page->setParent($root); // add document to the root
    
    $documentManager->persist($page); // add the page in the queue for persisting
    $documentManager->flush(); // execute the queries
    

    For more information: http://symfony.com/doc/current/cmf/book/database_layer.html