Skip to content

Commit

Permalink
Updated the how to add fixtures reference page. (#385)
Browse files Browse the repository at this point in the history
* Updated the how to add fixtures reference page. The text should be more selfexplaining and the code example is much cleaner now.

* Removed the "note" block because I don't know how to write it correctly and replaces it with a simple **bold**

* Fixed typo "is and" to "is an".

* Changing comment so it is more clear that setStructureType is for.

* Reworked and tested the code example how to implement a document fixture.

* Fixed indentation and changed #save to #flush as there is no save method.
  • Loading branch information
kfrohwein authored and danrot committed Aug 10, 2018
1 parent 578f3e2 commit 7974df2
Showing 1 changed file with 72 additions and 26 deletions.
98 changes: 72 additions & 26 deletions reference/components/document-manager/fixtures.rst
Expand Up @@ -7,74 +7,121 @@ you to load static data into your content repository.
Getting Started
---------------

Shown below is the simple data fixtures:
Shown below is an example that creates a simple data fixture.

**Note**
- The class name MUST end with `Fixture` for it to be recognized
- The class MUST be placed in `<your bundle>/DataFixtures/Document` in order
for it to be loaded automatically.

.. code-block:: php
<?php
// YourBundle/DataFixtures/Document/SomeFixture.php
namespace YourBundle\DataFixtures\Document;
use Sulu\Component\DocumentManager\DocumentManager;
use Sulu\Bundle\DocumentManagerBundle\DataFixtures\DocumentFixtureInterface;
use Sulu\Component\Content\Document\WorkflowStage;
use Sulu\Component\DocumentManager\Exception\MetadataNotFoundException;
class SomeFixture implements DocumentFixtureInterface
{
/**
* Simple local string with two chars.
*/
const LOCALE = 'en';
/**
* All fixtures will be sorted in regards of the returned integer. This
* "weight" will let a fixture run later if the integer is higher.
*
* @return int
*/
public function getOrder()
{
return 10;
}
/**
* Load fixtures.
*
* Use the document manager to create and save fixtures.
* Be sure to call DocumentManager#flush() when you are done.
*
* @param DocumentManager $documentManager
*
* @throws MetadataNotFoundException
*/
public function load(DocumentManager $documentManager)
{
/**
* "page" is the base content of sulu. "article" for example would be used be the Article bundle.
*
* @var \Sulu\Bundle\ContentBundle\Document\PageDocument $document
*/
$document = $documentManager->create('page');
/** @var \Sulu\Bundle\ContentBundle\Document\PageDocument $document */
$document = $documentManager->create('page');
$document->setLocale('en');
$document->setTitle('foo bar page');
// Set the local. Keep in mind that you have to save every local version extra.
$document->setLocale(static::LOCALE);
// The title of the page set in the template XML. Can not be set by getStructure()->bind();
$document->setTitle('foo bar page title');
// Use setStructureType to set the name of the page template.
$document->setStructureType('default');
// URL of the content with out any language prefix.
$document->setResourceSegment('/foo-bar-page');
$document->setWorkflowStage(WorkflowStage::PUBLISHED);
$document->getStructure()->bind(array(
'title' => 'foo bar page'
));
// Data for all content types that this template uses.
$document->getStructure()->bind(
[
'article' => '<strong>Lore Ipsum Dolor</strong>',
]
);
// Data for the "Excerpt & Taxonomies" tab when editing content.
$document->setExtension(
'excerpt',
[
'title' => 'foo title',
'description' => 'bar description',
'categories' => [],
'tags' => []
'tags' => [],
]
);
// Data for the "SEO" tab when editing content.
$document->setExtension(
'seo',
[
'title' => 'foo title',
]
);
$documentManager->persist($document, 'en', array(
'parent_path' => '/cmf/sulu_io/contents',
));
// parent_path uses your webspace name. In this case "sulu_io"
$documentManager->persist(
$document,
static::LOCALE,
[
'parent_path' => '/cmf/sulu_io/contents',
]
);
// Optional: If you don't want your document to be published, remove this line
$documentManager->publish($document, static::LOCALE);
# Optional: If you don't want your document to be published, remove this line
$documentManager->publish($document, 'en');
// Persist immediately to database.
$documentManager->flush();
}
}
Note that:

- The class name MUST end with `Fixture` for it to be recognized
- The class MUST be placed in `<your bundle>/DataFixtures/Document` in order
for it to be loaded automatically.

You can now execute your data fixture using the
``sulu:document:fixtures:load``
command.

.. code-block:: bash
$ php app/console sulu:document:fixtures:load
$ php bin/console sulu:document:fixtures:load
By default this command will purge and re-initialize the workspace before
loading all of the fixtures.
Expand Down Expand Up @@ -116,7 +163,6 @@ If you need the service container you can implement the `Symfony\Component\Depen
.. code-block:: php
<?php
// YourBundle/DataFixtures/Document/SomeFixture.php
namespace YourBundle\DataFixtures\Document;
Expand Down

0 comments on commit 7974df2

Please sign in to comment.