How to override SonataBasketBundle vendor views - symfony

just a little question:
I have a vendor bundle e.g. SonataBasketBundle with some views.
I extended it by SonataEasyExtend in my src/Application/SonataBasketBundle folder.
PROBLEM: I would to override vendor views, and I use the classical two methods: copy all views files in src/Application/SonataBasketBundle/Resources/views or copy them in app/Resources/SonataBasketBundle/views.
But, unfortunately, both methods do not works. What's the possible problem?
I missed some configuration?
I made a little test:
my extended bundle is named "ApplicationSonataBasketBundle".
Now, if in the vendor basket index method, I change the view name this way
return $this->render('ApplicationSonataBasketBundle:Basket:index.html.twig',
array(
'basket' => $this->get('sonata.basket'),
'form' => $form->createView(),
));
the framework load the application bundle view, as I want.
But, if the application bundle extends the vendor one (SonataBasketBundle), doesn't should be loaded by default also with name SonataBasketBundle?
Thanks in advance.

Override view file by pasting correct view files in
app/Resources/bundle/views
then clearing cache afterward by console
php app/console ca:cl
or delete cache files manually from app/cache (Better)
symfony doc
when overriding child bundle, it is good to override the controllers as well as the templates
<?php
// src/Acme/UserBundle/AcmeUserBundle.php
namespace Acme\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}

Related

My custom block doesn't show up in the block library

I am developing a custom module in Drupal 8. It shows data regarding some organizations that make use of our service. For this I have created a Controller that shows data from the database, which is put there by another module. From the scarce information and tutorials available on Drupal 8 developement I've been able to create the following. In the .routing.yml file I have created a path to this overview table like so (it doesn't properly copy here but the indents are okay):
OrganizationOverview.world:
path: '/world'
defaults:
_controller: 'Drupal\OrganizationOverview\Controller\OrganizationOverviewController::overview'
_title: 'World'
requirements:
_role: 'administrator'
_permission: 'access content'
So now the overview is accessible with the URL site.com/world. But what we want is to show it on the frontpage or show it anywhere else on the site. For this it needs to be a Block. For this I have created an OrganizationOverviewBlock class in OrganizationOverview/src/Plugin/Block/OrganizationOverviewBlock.php which is the proper way according to the PSR-4 standard. The class looks like this:
<?php
namespace Drupal\OrganizationOverview\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Session\AccountInterface;
/**
* Provides a 'OrganizationOverviewBlock' block.
*
* #Block(
* id = "organization_overview_block",
* admin_label = #Translation("OrganizationOverviewBlock"),
* category = #Translation("Custom")
* )
*/
class OrganizationOverviewBlock extends BlockBase
{
public function build()
{
return array(
'#markup' => 'Hello World',
);
}
public function blockAccess(AccountInterface $account)
{
return $account->hasPermission('access content');
}
}
So now it should show up in the Blocks Layout page (after flushing cache, which I do consistently) at site.com/admin/structure/block/ as "Organization Overview Block" where I should enable it, according to plenty sources (Create custom Block, Block API Drupal 8). But it doesn't show up there. I've tried implementing ContainerFactoryPluginInterface with some of those methods but that changes nothing. It does not show up. I've tried making a new test module with a block with the same code but a simpler name and it does not show up. I've copied the code to another platform (the production site) but it also doesn't show up there. What am I doing wrong? Can someone help me? I know Drupal 8 is new but this module really needs to be published soon.
You'll find a working example of building custom block in the Drupal Examples Project. So:
Get the Drupal 8 examples project
Enable the Block Example Module
Double check the working code
With that, you should get your block available in your own module
You can also take advantage of what explained here, where a single php file do the all job. Check files and folders path also.
Not require routing file for custom block.
<pre>
class TestBlock extends BlockBase {
/*
** {#inheritdoc}
*/
public function build() {
return array(
'#markup' => $this->t('Welcome page!'),
);
}
}
</pre>
http://drupalasia.com/article/drupal-8-how-create-custom-block-programatically
You should respect the Drupal coding standard recommendations:
No camelCase naming convention in module name.
OrganizationOverview actually is an error, you should use organization_overview (lowercase/underscore) naming conventions.

Extend a DataObject in SilverStripe

I installed a photogallery module in SilverStripe. This module has a DataObject named PhotoItem.
The PhotoItem class contains some fields, but I want to add extra fields. The easiest way to do that is to edit the PhotoItem file, but then I lose my changes when updating the module.
How can I extend this DataObject with some more fields with a DataObject file under /mysite/code?
In Silverstripe 3.1 you can extend a class by creating a DataExtension and applying it to your class.
First you would create a CustomPhotoItem.php in mysite/code or mysite/code/extensions:
CustomPhotoItem.php
class CustomPhotoItem extends DataExtension {
private static $db = array(
'ExtraTextField' => 'Text'
);
public function updateCMSFields(FieldList $fields) {
$fields->push(TextField::create('ExtraTextField', 'Extra Text Field'));
}
}
In order to apply this extension to your class, you need to add the following to your config.yml:
config.yml
PhotoItem:
extensions:
- CustomPhotoItem
Your config.yml should be located in mysite/_config/config.yml.
Run dev/build?flush=1 and you should see your new variables added to your original object.
You are searching for DataExtension. Have a look to the documentation, there's evereything you need for adding some more fields to DataObjects.
In particular have a look to the section named Adding extra database fields

Symfony 2.3 FOSUserBundle.en.yml not registering override

I am trying to override the default FOSUserBundle.en.yml from the FOS user-bundle.
I have the user bundle working fine, i have registered a user and logged in.
However when I copy the FOSUserBundle.en.yml into my own UserBundle to override the wording Symfony doesn't seem to pick it up.
This is the path I have copied the transation file to:
src/Blogger/UserBundle/Resources/translations/FOSUserBundle.en.ym
But no joy.. I have cleared the caches tried another browser but the change in the override will not come through.
I can place the change the in the original and see the changes:
vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/translations/FOSUserBundle.en.yml
Am I missing something?
Thanks,
John
To override the bundle, you should add this :
// src/Blogger/UserBundle/BloggerUserBundle.php
namespace Blogger\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class BloggerUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
Here's a link from the docs : http://symfony.com/doc/current/cookbook/bundles/inheritance.html
All the steps you should follow are in there.

Overriding the FOSUserBundle controllers

So I read alot about the overriding of templates and such and overriding of bundles in Symfony.
I am using the new Symfony 2.3, I have not tried this in lower versions of Symfony.
I followed the tutorial about overriding bundles in Symfony:
http://symfony.com/doc/2.3/cookbook/bundles/inheritance.html
I followed the tutorial about overriding the controllers of FOSUserBundle, which is the same thing really:
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_controllers.md
I had a bundle named Acme/WebBundle.
Now I have done the following things:
Created a new bundle named Acme/UserBundle.
Created the file AcmeUserBundle.php in this bundle.
<?php
namespace Acme\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
Created the following file structure:
-src
-Acme
-UserBundle
-Controller
RegistrationController.php
-Entity
User.php
-Resources
-translations
-views
AcmeUserBundle.php
In RegistrationController.php I set the namespace to:
namespace Acme\UserBundle\Controller;
Copied the contents of the registration controller of FOSUserBundle to mine.
Added to the beginning of registerAction()
die("message");
Now when I go to the registration form, the default /register route, I don't get a die, everything works fine. It does not see my bundle as a child, nothing is overridden and I've been trying to get it to work for ages hence my question here.
Did I do something wrong?
Remember that you need to add any new bundle to AppKernel::registerBundles() in app/AppKernel.php like this:
$bundles = array(
...
new Acme\UserBundle()
);

Overriding the User Admin Form

I'm trying to override the SonataUser/Admin/Model/UserAdmin's configureFormFields() because I need to remove some default fields from the admin form.
So I have copied the file vendor/bundles/Sonata/UserBundle/Admin/Model/UserAdmin.php in my bundle app/Application/Sonata/UserBundle/Admin/Model/UserAdmin.php and modified it. Then declared it as a service:
# app/application/Sonata/UserBundle/Resources/config/services.yml
services:
application_user.registration.form.type:
class: Application\Sonata\UserBundle\Admin\Model\UserAdmin
arguments: [%sonata_user.model.user.class%]
tags:
- { name: form.type, alias: application_user_admin }
Now questions:
Am I doing right ? How can I tell sonata admin to use it ?
The overriding class should be set in config.yml:
# app/config/config.yml
sonata_user:
admin:
user:
class: MyCompany\UserBundle\Admin\Model\UserAdmin
Extend original UserAdmin:
namespace MyCompany\UserBundle\Admin\Model;
use Sonata\AdminBundle\Form\FormMapper;
class UserAdmin extends \Sonata\UserBundle\Admin\Model\UserAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
// new logic
}
}
Of course change class name MyCompany\UserBundle\Admin\Model\UserAdmin to reflect your bundle structure.
It is a better practice to keep your bundles in the src directory instead: (See Creating a bundle section).
In this case, if you are using easy extends, make sure to use --dest=src in order to generate the bundle inside an Application namespace in src/.
php app/console sonata:easy-extends:generate SonataUserBundle --dest=src
By creating your overriding bundle in src/Application/Sonata/UserBundle and registering the vendor bundle as a parent, you won't have to create a new service. This explains you how to override the bundle properly: overriding a bundle and should save you a lot of time.
Don't forget to create the file you want to override in the same location as your parent bundle.
In your case, you would have to copy paste SonataUser/Admin/Model/UserAdmin.php from the vendor into your bundle src/Application/Sonata/UserBundle/Admin/Model/UserAdmin.php and modify it as you wish.
That's why overriding bundles can be so useful.

Resources