eZ\Publish\Core\FieldType\Page\PageService::loadBlock PHP Method

loadBlock() public method

Loads Block object for the given $id.
public loadBlock ( integer | string $id ) : Block
$id integer | string
return eZ\Publish\Core\FieldType\Page\Parts\Block
    public function loadBlock($id)
    {
        if (isset($this->blocksById[$id])) {
            return $this->blocksById[$id];
        }
        $contentId = $this->getStorageGateway()->getContentIdByBlockId($id);
        $content = $this->contentService->loadContent($contentId);
        foreach ($content->getFields() as $field) {
            if (!$field->value instanceof Value) {
                continue;
            }
            foreach ($field->value->page->zones as $zone) {
                foreach ($zone->blocks as $block) {
                    if ($block->id === $id) {
                        return $this->blocksById[$id] = $block;
                    }
                }
            }
        }
        throw new NotFoundException('Block', $id);
    }

Usage Example

 /**
  * Detects if there is a custom controller to use to render a Block.
  *
  * @param FilterControllerEvent $event
  *
  * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
  */
 public function getController(FilterControllerEvent $event)
 {
     $request = $event->getRequest();
     // Only taking page related controller (i.e. ez_page:viewBlock or ez_page:viewBlockById)
     if (strpos($request->attributes->get('_controller'), 'ez_page:') === false) {
         return;
     }
     try {
         if ($request->attributes->has('id')) {
             $valueObject = $this->pageService->loadBlock($request->attributes->get('id'));
             $request->attributes->set('block', $valueObject);
         } elseif ($request->attributes->get('block') instanceof Block) {
             $valueObject = $request->attributes->get('block');
             $request->attributes->set('id', $valueObject->id);
         }
     } catch (UnauthorizedException $e) {
         throw new AccessDeniedException();
     }
     if (!isset($valueObject)) {
         $this->logger->error('Could not resolve a page controller, invalid value object to match.');
         return;
     }
     $controllerReference = $this->controllerManager->getControllerReference($valueObject, 'block');
     if (!$controllerReference instanceof ControllerReference) {
         return;
     }
     $request->attributes->set('_controller', $controllerReference->controller);
     $event->setController($this->controllerResolver->getController($request));
 }
All Usage Examples Of eZ\Publish\Core\FieldType\Page\PageService::loadBlock