Etienne Noël
Etienne Noël

Reputation: 6166

SonataAdmin - The custom block I'm trying to build is not found

I'm trying to add a custom block to the dashboard of SonataAdminBundle. I followed the instructinos here (How to add custom link or button to SonataAdminBundle Dashboard in Symfony2) and I'm getting the following error :

 RuntimeException: The block service `sonata.block.service.processManagement` does not exist 

Here's what I did. I have a file named "ProcessManagementBlockService.php" which contains the following:

<?php

namespace IMA\ProcessManagementBundle\Block;

use Symfony\Component\HttpFoundation\Response;

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Validator\ErrorElement;

use Sonata\BlockBundle\Model\BlockInterface;

use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Block\BaseBlockService;

class ProcessManagementBlockService extends BaseBlockService
{
    public function getName()
    {
        return 'My Newsletter';
    }

    public function getDefaultSettings()
    {
        return array();
    }

    public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
    {
    }

    public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
    {
    }

    public function execute(BlockContextInterface $block, Response $response = null)
    {
        // merge settings
        $settings = array_merge($this->getDefaultSettings(), $block->getSettings());

        return $this->renderResponse('IMAProcessManagement:Block:blockProcessManagement.html.twig', array(
            'block'     => $block,
            'settings'  => $settings
        ), $response);
    }
}

I also created a file (views/Block/blockProcessManagement.html.twig) that contains the template of the block I want to add to SonataAdmin's dashboard :

{% extends 'SonataBlockBundle:Block:block_base.html.twig' %}

{% block block %}
    <table class="table table-bordered table-striped sonata-ba-list">
        <thead>
        <tr>
            <th colspan="3">Newsletter - inviare</th>
        </tr>
        </thead>

        <tbody>
        <tr>
            <td>
                <div class="btn-group" align="center">
                    <a class="btn btn-small" href="#">Servizio Newsletter</a>
                </div>
            </td>
        </tr>
        </tbody>
    </table>
{% endblock %}

Also, I in the services.yml file of my bundle, I have the following

services:
#    ima_process_management.example:
#        class: %ima_process_management.example.class%
#        arguments: [@service_id, "plain_value", %parameter%]
    sonata.block.service.processManagement:
        class: IMA\ProcessManagementBundle\Block\ProcessManagementBlockService
        arguments: [ "sonata.block.service.processManagement", @templating ]
        tags:
            - { name: sonata.block }

I know this file is properly loaded because I tried to put the upper lines directly in config.yml and got the same result.

Finally, I added in the main config.yml file of my project

sonata_block:
    default_contexts: [cms]
    blocks:
        # Enable the SonataAdminBundle block
        sonata.admin.block.admin_list:
            contexts:   [admin]
        # Your other blocks
        sonata.block.service.text:
        sonata.block.service.rss:
        sonata.admin.block.search_result:
        sonata.block.service.processManagement: ~

and

sonata_admin:
    templates:
        dashboard: SonataAdminBundle:Core:dashboard.html.twig
    dashboard:
        blocks:
            - { position: left, type: sonata.admin.block.admin_list }
            - { position: left, type: sonata.block.service.processManagement}

I really don't know why I'm getting the error that the service does not exist...

Upvotes: 3

Views: 2902

Answers (1)

Geert Wille
Geert Wille

Reputation: 1654

The problem is how you did the letter casing in the config.yml. Always use lowercase for defining service names if you don't then Symfony converts it to lowercase.

Take a look at the coding standards for Symfony.

Service Naming Conventions:

  • A service name contains groups, separated by dots

  • The DI alias of the bundle is the first group (e.g. fos_user)

  • A service name contains groups, separated by dots

  • Use lowercase letters for service and parameter names

Upvotes: 3

Related Questions