src/Kunstmaan/PagePartBundle/Helper/Services/PagePartCreatorService.php
public function addPagePartsToPage($nodeOrInternalName, array $structure, $language)
    {
        $node = $this->getNode($nodeOrInternalName);

        // First instantiate all PageParts. This way no PageParts will be saved if there is an issue instantiating some of them.
        $instantiatedPageParts = array();
        foreach ($structure as $context => $pageParts) {
            $instantiatedPageParts[$context] = array();

            foreach ($pageParts as $pagePartOrFunction) {
                if (is_callable($pagePartOrFunction)) {
                    $pagePartOrFunction = $pagePartOrFunction();

                    if (!isset($pagePartOrFunction) || (is_null($pagePartOrFunction))) {
                        throw new \LogicException('A function returned nothing for a pagepart. Make sure you return your instantiated pageparts in your anonymous functions.');
                    }
                }
                if (!$pagePartOrFunction instanceof PagePartInterface) {
                    throw new \LogicException('Detected a supposed pagepart that did not implement the PagePartInterface.');
                }

                $instantiatedPageParts[$context][] = $pagePartOrFunction;
            }
        }

        // All good. We can start saving.
        foreach ($instantiatedPageParts as $context => $pageParts) {
            foreach ($pageParts as $pagePart) {
                $this->addPagePartToPage($node, $pagePart, $language, $context);
            }
        }
    }
public function addPagePartsToPage($nodeOrInternalName, array $structure, $language)
    {
        $node = $this->getNode($nodeOrInternalName);

        // First instantiate all PageParts. This way no PageParts will be saved if there is an issue instantiating some of them.
        $instantiatedPageParts = array();
        foreach ($structure as $context => $pageParts) {
            $instantiatedPageParts[$context] = array();

            foreach ($pageParts as $pagePartOrFunction) {
                if (is_callable($pagePartOrFunction)) {
                    $pagePartOrFunction = $pagePartOrFunction();

                    if (!isset($pagePartOrFunction) || (is_null($pagePartOrFunction))) {
                        throw new \LogicException('A function returned nothing for a pagepart. Make sure you return your instantiated pageparts in your anonymous functions.');
                    }
                }
                if (!$pagePartOrFunction instanceof PagePartInterface) {
                    throw new \LogicException('Detected a supposed pagepart that did not implement the PagePartInterface.');
                }

                $instantiatedPageParts[$context][] = $pagePartOrFunction;
            }
        }

        // All good. We can start saving.
        foreach ($instantiatedPageParts as $context => $pageParts) {
            foreach ($pageParts as $pagePart) {
                $this->addPagePartToPage($node, $pagePart, $language, $context);
            }
        }
    }
public function addPagePartsToPage ( $ nodeOrInternalName , array $ structure , $ language ) { $ node = $ this -> getNode ( $ nodeOrInternalName ) ; // First instantiate all PageParts. This way no PageParts will be saved if there is an issue instantiating some of them. $ instantiatedPageParts = array ( ) ; foreach ( $ structure as $ context => $ pageParts ) { $ instantiatedPageParts [ $ context ] = array ( ) ; foreach ( $ pageParts as $ pagePartOrFunction ) { if ( is_callable ( $ pagePartOrFunction ) ) { $ pagePartOrFunction = $ pagePartOrFunction ( ) ; if ( ! isset ( $ pagePartOrFunction ) || ( is_null ( $ pagePartOrFunction ) ) ) { throw new \ LogicException ( 'A function returned nothing for a pagepart. Make sure you return your instantiated pageparts in your anonymous functions.' ) ; } } if ( ! $ pagePartOrFunction instanceof PagePartInterface ) { throw new \ LogicException ( 'Detected a supposed pagepart that did not implement the PagePartInterface.' ) ; } $ instantiatedPageParts [ $ context ] [ ] = $ pagePartOrFunction ; } } // All good. We can start saving. foreach ( $ instantiatedPageParts as $ context => $ pageParts ) { foreach ( $ pageParts as $ pagePart ) { $ this -> addPagePartToPage ( $ node , $ pagePart , $ language , $ context ) ; } } }
A helper function to more easily append multiple pageparts in different manners.

@param mixed(Node|string) $nodeOrInternalName
The node that you'd like to append the pageparts to. It's also possible to provide an internalname.
@param array              $structure
The structure array is something like this:

array('main' => array(
function() { return new DummyPagePart('A') }, function() { return new DummyPagePart('B') }
), 'banners' => array($awesomeBanner));

So it's an array containing the pageparts per region. Each pagepart is returned by a function.
This is clean because we don't have to bother with variablenames which we have to remember to pass
to the pagecreatorservice at the right time. With this method it's impossible to assign a wrong pagepart to a page.
Unless you provide the incorrect page oviously ... .

You can also include variables in the pagepart arrays if you want.

Or optionally you can use the results of the getCreatorArgumentsForPagePartAndProperties function instead of an anonymous function.
@param string $language
The language of the translation you want to append to

@throws \LogicException
A helper function to more easily append multiple pageparts in different manners .