/** * Hydrate block data for the page. * * @param array $blockData * @param SitePage $page * @param bool $append */ private function hydrateBlocks(array $blockData, SitePage $page, ErrorStore $errorStore, $append = false) { $blocks = $page->getBlocks(); $existingBlocks = $blocks->toArray(); $newBlocks = []; $position = 1; $fallbackBlock = ['o:layout' => null, 'o:data' => []]; foreach ($blockData as $inputBlock) { if (!is_array($inputBlock)) { continue; } $inputBlock = array_merge($fallbackBlock, $inputBlock); $block = current($existingBlocks); if ($block === false || $append) { $block = new SitePageBlock(); $block->setPage($page); $newBlocks[] = $block; } else { // Null out values as we re-use them $existingBlocks[key($existingBlocks)] = null; next($existingBlocks); } if (!is_string($inputBlock['o:layout']) || $inputBlock['o:layout'] === '') { $errorStore->addError('o:block', 'All blocks must have a layout.'); return; } if (!is_array($inputBlock['o:data'])) { $errorStore->addError('o:block', 'Block data must not be a scalar value.'); return; } $block->setLayout($inputBlock['o:layout']); $block->setData($inputBlock['o:data']); // (Re-)order blocks by their order in the input $block->setPosition($position++); $attachmentData = isset($inputBlock['o:attachment']) ? $inputBlock['o:attachment'] : []; // Hydrate attachments, and abort block hydration if there's an error if (!$this->hydrateAttachments($attachmentData, $block, $errorStore)) { return; } $handler = $this->getServiceLocator()->get('Omeka\\BlockLayoutManager')->get($inputBlock['o:layout'])->onHydrate($block, $errorStore); } // Remove any blocks that weren't reused if (!$append) { foreach ($existingBlocks as $key => $existingBlock) { if ($existingBlock !== null) { $blocks->remove($key); } } } // Add any new blocks that had to be created foreach ($newBlocks as $newBlock) { $blocks->add($newBlock); } }
/** * {@inheritDoc} */ public function setPage(\Omeka\Entity\SitePage $page) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setPage', array($page)); return parent::setPage($page); }