1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
   | <?php
 
 
namespace App\Block;
 
use App\Entity\BlogPost;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
use Sonata\BlockBundle\Mapper\FormMapper;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\Form\Validator\ErrorElement;
use Sonata\BlockBundle\Block\Service\BlockServiceInterface;
 
class UserProfileMenu extends AbstractBlockService implements BlockServiceInterface
{
    /**
     * @var EntityManagerInterface
     */
    protected $entityManager;
 
    public function __construct(
        EntityManagerInterface $entityManager,
        $name = null,
        EngineInterface $templating = null
    ) {
        $this->entityManager=$entityManager;
        parent::__construct($name, $templating);
    }
 
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'url'      => false,
            'title'    => 'Posts',
            'template' => 'bundles/IntranetBundle/Block/block_user_last_posts.html.twig',
        ));
    }
 
 
    public function execute(BlockContextInterface $blockContext, Response $response = null)
    {
        // merge settings
        $settings = $blockContext->getSettings();
        $posts = false;
 
 
        $posts = $this->entityManager->getRepository(BlogPost::class)->findAll();
 
        return $this->renderResponse($blockContext->getTemplate(), [
            'posts'     => $posts,
            'block'     => $blockContext->getBlock(),
            'settings'  => $settings
        ], $response);
    }
 
 
} | 
Partager