sonata-project / SonataBlockBundle

Symfony SonataBlockBundle
https://docs.sonata-project.org/projects/SonataBlockBundle
MIT License
413 stars 142 forks source link

Compile Error: Declaration of Sonata\BlockBundle\Block\MyBlockService::buildEditForm(...) must be compatible with Sonata\BlockBundle\Block\Service\AbstractAdminBlockService::buildEditForm(...) #630

Closed simbu-g2t closed 5 years ago

simbu-g2t commented 5 years ago

Environment

Sonata packages

sonata-project/admin-bundle              3.52.0 3.52.0 The missing Symfony Admin Generator
sonata-project/block-bundle              3.17.0 3.17.0 Symfony SonataBlockBundle
sonata-project/cache                     1.1.1  2.0.1  Cache library
sonata-project/cache-bundle              2.4.2  3.1.0  This bundle provides caching services
sonata-project/core-bundle               3.17.0 3.17.0 Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.5.0  3.0.0  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.3.0  1.3.0  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.10.0 3.10.0 Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/easy-extends-bundle       2.5.0  2.5.0  Symfony SonataEasyExtendsBundle
sonata-project/exporter                  2.0.1  2.0.1  Lightweight Exporter library
sonata-project/formatter-bundle          4.1.3  4.1.3  Symfony SonataFormatterBundle
sonata-project/media-bundle              3.20.1 3.20.1 Symfony SonataMediaBundle
sonata-project/notification-bundle       3.6.2  3.6.2  Symfony SonataNotificationBundle
sonata-project/page-bundle               3.11.1 3.11.1 This bundle provides a Site and Page management through container and block services
sonata-project/seo-bundle                2.7.0  2.7.0  Symfony SonataSeoBundle

Symfony packages

symfony/asset                      v4.3.4  v4.3.4  Symfony Asset Component
symfony/browser-kit                v4.3.4  v4.3.4  Symfony BrowserKit Component
symfony/cache                      v4.3.4  v4.3.4  Symfony Cache component with PSR-6, PSR-16, and tags
symfony/cache-contracts            v1.1.5  v1.1.5  Generic abstractions related to caching
symfony/config                     v4.3.4  v4.3.4  Symfony Config Component
symfony/console                    v4.3.4  v4.3.4  Symfony Console Component
symfony/css-selector               v4.3.4  v4.3.4  Symfony CssSelector Component
symfony/debug                      v4.3.4  v4.3.4  Symfony Debug Component
symfony/debug-bundle               v4.3.4  v4.3.4  Symfony DebugBundle
symfony/debug-pack                 v1.0.7  v1.0.7  A debug pack for Symfony projects
symfony/dependency-injection       v4.3.4  v4.3.4  Symfony DependencyInjection Component
symfony/doctrine-bridge            v4.3.4  v4.3.4  Symfony Doctrine Bridge
symfony/dom-crawler                v4.3.4  v4.3.4  Symfony DomCrawler Component
symfony/dotenv                     v4.3.4  v4.3.4  Registers environment variables from a .env file
symfony/event-dispatcher           v4.3.4  v4.3.4  Symfony EventDispatcher Component
symfony/event-dispatcher-contracts v1.1.5  v1.1.5  Generic abstractions related to dispatching event
symfony/expression-language        v4.3.4  v4.3.4  Symfony ExpressionLanguage Component
symfony/filesystem                 v4.3.4  v4.3.4  Symfony Filesystem Component
symfony/finder                     v4.3.4  v4.3.4  Symfony Finder Component
symfony/flex                       v1.4.5  v1.4.5  Composer plugin for Symfony
symfony/form                       v4.3.4  v4.3.4  Symfony Form Component
symfony/framework-bundle           v4.3.4  v4.3.4  Symfony FrameworkBundle
symfony/http-client                v4.3.4  v4.3.4  Symfony HttpClient component
symfony/http-client-contracts      v1.1.6  v1.1.6  Generic abstractions related to HTTP clients
symfony/http-foundation            v4.3.4  v4.3.4  Symfony HttpFoundation Component
symfony/http-kernel                v4.3.4  v4.3.4  Symfony HttpKernel Component
symfony/inflector                  v4.3.4  v4.3.4  Symfony Inflector Component
symfony/intl                       v4.3.4  v4.3.4  A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/maker-bundle               v1.13.0 v1.13.0 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.
symfony/mime                       v4.3.4  v4.3.4  A library to manipulate MIME messages
symfony/monolog-bridge             v4.3.4  v4.3.4  Symfony Monolog Bridge
symfony/monolog-bundle             v3.4.0  v3.4.0  Symfony MonologBundle
symfony/options-resolver           v4.3.4  v4.3.4  Symfony OptionsResolver Component
symfony/orm-pack                   v1.0.6  v1.0.6  A pack for the Doctrine ORM
symfony/phpunit-bridge             v4.3.4  v4.3.4  Symfony PHPUnit Bridge
symfony/polyfill-intl-icu          v1.12.0 v1.12.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn          v1.12.0 v1.12.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-mbstring          v1.12.0 v1.12.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php72             v1.12.0 v1.12.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/polyfill-php73             v1.12.0 v1.12.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/process                    v4.3.4  v4.3.4  Symfony Process Component
symfony/profiler-pack              v1.0.4  v1.0.4  A pack for the Symfony web profiler
symfony/property-access            v4.3.4  v4.3.4  Symfony PropertyAccess Component
symfony/property-info              v4.3.4  v4.3.4  Symfony Property Info Component
symfony/routing                    v4.3.4  v4.3.4  Symfony Routing Component
symfony/security-acl               v3.0.2  v3.0.2  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle            v4.3.4  v4.3.4  Symfony SecurityBundle
symfony/security-core              v4.3.4  v4.3.4  Symfony Security Component - Core Library
symfony/security-csrf              v4.3.4  v4.3.4  Symfony Security Component - CSRF Library
symfony/security-guard             v4.3.4  v4.3.4  Symfony Security Component - Guard
symfony/security-http              v4.3.4  v4.3.4  Symfony Security Component - HTTP Integration
symfony/serializer                 v4.3.4  v4.3.4  Symfony Serializer Component
symfony/serializer-pack            v1.0.2  v1.0.2  A pack for the Symfony serializer
symfony/service-contracts          v1.1.6  v1.1.6  Generic abstractions related to writing services
symfony/stopwatch                  v4.3.4  v4.3.4  Symfony Stopwatch Component
symfony/swiftmailer-bundle         v3.2.8  v3.2.8  Symfony SwiftmailerBundle
symfony/templating                 v4.3.4  v4.3.4  Symfony Templating Component
symfony/test-pack                  v1.0.6  v1.0.6  A pack for functional and end-to-end testing within a Symfony app
symfony/translation                v4.3.4  v4.3.4  Symfony Translation Component
symfony/translation-contracts      v1.1.6  v1.1.6  Generic abstractions related to translation
symfony/twig-bridge                v4.3.4  v4.3.4  Symfony Twig Bridge
symfony/twig-bundle                v4.3.4  v4.3.4  Symfony TwigBundle
symfony/validator                  v4.3.4  v4.3.4  Symfony Validator Component
symfony/var-dumper                 v4.3.4  v4.3.4  Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter               v4.3.4  v4.3.4  A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/web-link                   v4.3.4  v4.3.4  Symfony WebLink Component
symfony/web-profiler-bundle        v4.3.4  v4.3.4  Symfony WebProfilerBundle
symfony/web-server-bundle          v4.3.4  v4.3.4  Symfony WebServerBundle
symfony/yaml                       v4.3.4  v4.3.4  Symfony Yaml Component

PHP version

PHP 7.2.21-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Aug  7 2019 09:53:51) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.21-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

Subject

I want to create a block for CTA(Call To Action) with HTML code customization. So I tried to create a new block with help of this officia doc. But I got the compatible error like below

Compile Error: Declaration of Sonata\BlockBundle\Block\MyBlockService::buildEditForm(Sonata\BlockBundle\Mapper\FormMapper $formMapper, Sonata\BlockBundle\Model\BlockInterface $block) must be compatible with Sonata\BlockBundle\Block\Service\AbstractAdminBlockService::buildEditForm(Sonata\AdminBundle\Form\FormMapper $form, Sonata\BlockBundle\Model\BlockInterface $block)

I searched it but didn't find any answers. https://github.com/sonata-project/SonataBlockBundle/issues/324 was not resolved my issue. Because I'm useing latest versions. So I'm asking to you, what should I do to solve it ?

Steps to reproduce

1.config/services.yaml

sonata.block.service.myblock:
        class: Sonata\BlockBundle\Block\Service\MyBlockService
        arguments:
            - ~
            - '@twig'
        tags:
            - { name: sonata.block }
  1. src/Application/Sonata/BlockBundle/Block/Service/MyBlockService.php
    
    <?php
    namespace Sonata\BlockBundle\Block;

use Symfony\Component\HttpFoundation\Response; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Sonata\BlockBundle\Block\BlockContextInterface; use Sonata\BlockBundle\Block\Service\AbstractBlockService; use Sonata\BlockBundle\Mapper\FormMapper; use Sonata\BlockBundle\Model\BlockInterface; use Sonata\Form\Validator\ErrorElement;

class MyBlockService extends BaseBlockService { public function getName() { return 'My Block'; }

public function configureSettings(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'url' => false,
        'title' => 'Insert the title',
        'template' => 'blocks/myblock.html.twig',
    ]);
}

public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
{
    $errorElement            
        ->with('settings.title')
            ->assertNotNull([])
            ->assertNotBlank()
            ->assertMaxLength(['limit' => 50])
        ->end()
    ;
}

public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
{
    $formMapper
        ->add('settings', 'sonata_type_immutable_array', [
            'keys' => [
                ['title', 'text', ['required' => false]],
            ]
        ])
    ;
}

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

    return $this->renderResponse($blockContext->getTemplate(), [
        'block'     => $blockContext->getBlock(),
        'settings'  => $settings
    ], $response);
}

}


3.templates/blocks/myblock.html.twig

{# blocks/myblock.html.twig #} {% extends sonata_block.templates.block_base %}

{% block block %}

{{ settings.title }}

{% endblock %}

4.config/sonata_block.yaml

sonata_block: default_contexts: [sonata_page_bundle] context_manager: sonata.page.block.context_manager blocks: sonata.block.service.text: ~ sonata.block.service.container: ~ sonata.media.block.feature_media: ~ sonata.media.block.media: ~ sonata.media.block.gallery: ~ sonata.formatter.block.formatter: ~ sonata.block.service.menu: ~ sonata.product.block.categories_menu: ~ sonata.block.service.myblock: ~


## Expected results
New block created and will be available in block section while creating pages

## Actual results
Compile Error: Declaration of Sonata\BlockBundle\Block\MyBlockService::buildEditForm(Sonata\BlockBundle\Mapper\FormMapper $formMapper, Sonata\BlockBundle\Model\BlockInterface $block) must be compatible with Sonata\BlockBundle\Block\Service\AbstractAdminBlockService::buildEditForm(Sonata\AdminBundle\Form\FormMapper $form, Sonata\BlockBundle\Model\BlockInterface $block)
core23 commented 5 years ago

you have a wrong import of the FormMapper class. Just look at the stack trace.

AFAIK this is not a general bug in this bundle.

-- writing from my mobile device

simbu-g2t commented 5 years ago

Hi @core23 Thanks for the immediate reply!

I just followed the officia doc The imports there are like this only. Can you please give me the working solutions

Thanks in advance

phansys commented 5 years ago

I think the issue here is that the documentation for the version you're using (3.x) is missing, the docs you're referring to are for the yet unreleased 4.x version (represented by master branch in this package). In this case you may try to check if the documentation for 2.x helps: https://sonata-project.org/bundles/block/2-0/doc/reference/your_first_block.html#first-namespaces IMO, in the meantime we should try to solve the situation.

/cc @core23.

simbu-g2t commented 5 years ago

@phansys Same issue with https://sonata-project.org/bundles/block/2-0/doc/reference/your_first_block.html this doc also. Is there any solution to overcome this issue?

Thanks in advance!

core23 commented 5 years ago

Can you have a look at the current docs? https://github.com/sonata-project/SonataBlockBundle/blob/3.x/docs/reference/your_first_block.rst

core23 commented 5 years ago

@sonata-project/contributors We should update the gitlab project and check all projects

simbu-g2t commented 5 years ago

@core23 This is the new issue with 3.x doc

Compile Error: Cannot declare class Sonata\BlockBundle\Block\MyBlockService, because the name is already in use

I don't know what mistake I made for this issue. Kindly give me the solution, Thanks

core23 commented 5 years ago

Sorry, but we try to keep github issues for feature requests and bugs only.

Compile Error: Cannot declare class Sonata\BlockBundle\Block\MyBlockService, because the name is already in use

Maybe your filename and class do not match our you have declared the same class (with same namespace) anywhere else.