public function postFlushBuild(Fixture $fixture)
 {
     $params = $fixture->getParameters();
     $translations = $fixture->getTranslations();
     $original = $fixture->getEntity();
     if (!isset($params['page']) || empty($translations)) {
         throw new \Exception('No page reference and/or translations detected for pagepart fixture ' . $fixture->getName() . ' (' . $fixture->getClass() . ')');
     }
     $pageFixture = $params['page'];
     if (!$pageFixture instanceof Fixture) {
         throw new \Exception('Could not find a reference "' . $params['page'] . '"" for fixture ' . $fixture->getName() . ' (' . $fixture->getClass() . ')');
     }
     $additionalEntities = $pageFixture->getAdditionalEntities();
     $pp = $original;
     $first = null;
     foreach ($translations as $language => $data) {
         $key = $pageFixture->getName() . '_' . $language;
         if (!isset($additionalEntities[$key])) {
             continue;
         }
         if ($first !== null) {
             $pp = clone $original;
         }
         $page = $additionalEntities[$key];
         $this->populator->populate($pp, $data);
         $this->em->persist($pp);
         $this->em->flush($pp);
         // Find latest position.
         $position = array_key_exists('position', $params) ? $params['position'] : null;
         $context = isset($params['context']) ? $params['context'] : 'main';
         if (is_null($position)) {
             $pageParts = $this->pagePartRepo->getPagePartRefs($page, $context);
             $position = count($pageParts) + 1;
         }
         $this->pagePartRepo->addPagePart($page, $pp, $position, $context);
         $first = false;
     }
 }
 /**
  * Add a single pagepart to an existing page for a specific language, in an optional position.
  *
  * @param mixed(Node|string) $nodeOrInternalName
  *      A Node instance or the internal name.
  *      When the internal name is passed we'll get the node instance.
  *      Based on the language we'll locate the correct Page instance.
  * @param PagePartInterface $pagePart
  *      A completely configured pagepart for this language.
  * @param string $language
  *      The languagecode. nl|fr|en|.. . Just one.
  * @param string $context
  *      Where you want the pagepart to be.
  * @param mixed(integer\NULL) $position
  *      Leave null if you want to append at the end.
  *      Otherwise set a position you would like and it'll inject the pagepart in that position.
  *      It won't override pageparts but it will rather inject itself in that position and
  *      push the other pageparts down.
  */
 public function addPagePartToPage($nodeOrInternalName, PagePartInterface $pagePart, $language, $context = 'main', $position = null)
 {
     // Find the correct page instance.
     $node = $this->getNode($nodeOrInternalName);
     /** @var $translation NodeTranslation */
     $translation = $node->getNodeTranslation($language, true);
     $page = $translation->getRef($this->em);
     // Find latest position.
     if (is_null($position)) {
         $pageParts = $this->pagePartRepo->getPagePartRefs($page, $context);
         $position = count($pageParts) + 1;
     }
     $this->em->persist($pagePart);
     $this->em->flush();
     $this->pagePartRepo->addPagePart($page, $pagePart, $position, $context);
 }