How can I get custom routes inside sonata admin class - symfony

I configure knp menu inside sonata admin product class and I want to add link to another admin class (category)
my code is:
protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
{
$menu->addChild(
$this->trans('product.sidemenu.link_designer', array(), 'm2m'),
array('uri' => $admin->generateUrl('sonata.classification.admin.category.list'))
);
}
Unfortunatly route 'sonata.classification.admin.category.show' doesn't exist. Routes from app/console router debug also desnt work. I have no access to inject #router in servies becouse definition of service is inside vendors.
Any idea?

In your admin, you can override the configureRoutes method, as demonstrated in the documentation:
http://sonata-project.org/bundles/admin/2-2/doc/reference/routing.html#create-a-route
This will allow you to add custom routes for your admin.

I found simple solution. I was mistake because default menu item defined in sonata demo looks like this:
$menu->addChild(
$this->trans('product.sidemenu.view_variations'),
array('uri' => $admin->generateUrl('sonata.product.admin.product.variation.list', array('id' => $id)))
);
And to add custom routes I had to added route parameter instead uri.
$menu->addChild(
$this->trans('product.sidemenu.view_variations'),
array('route' => 'admin_sonata_classification_category_list' )
);
Now everything work .

Related

How can I allow the user to set a custom global value in the silverstripe CMS?

I would like to be able to set custom values in the CMS, such as with the site name and tagline. I can't currently find any way of doing this other than on individual pages.
You can do so by extending SiteConfig. Your Extension might look like this:
class CustomSiteConfig extends DataExtension
{
private static $db = array(
'CustomContent' => 'Varchar(255)'
);
public function updateCMSFields(FieldList $fields)
{
$fields->addFieldToTab('Root.Main',
TextField::create('CustomContent', 'Custom content')
);
}
}
Then you need to apply the extension to SiteConfig. Add the following to mysite/_config/config.yml
SiteConfig:
extensions:
- CustomSiteConfig
And that's it. Run dev/build and your new field should be editable in the CMS as well as accessible in the Template using: $SiteConfig.CustomContent

Symfony2 KnpMenuBundle - Following tutorial and came across this error

I followed this tutorial:
https://github.com/KnpLabs/KnpMenuBundle/blob/master/Resources/doc/index.md#installation
And have come across the following error:
An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "page_show" as such route does not exist.") in /var/www/bundles/src/Acme/DemoBundle/Resources/views/Default/index.html.twig at line 4.
Is there a step I am missing here to pass something to a controller?
From link:
use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
class Builder extends ContainerAware
{
public function mainMenu(FactoryInterface $factory, array $options)
{
$menu = $factory->createItem('root');
$menu->addChild('Home', array('route' => 'homepage'));
$menu->addChild('About Me', array(
'route' => 'page_show',
'routeParameters' => array('id' => 42)
));
// ... add more children
return $menu;
}
}
To actually render the menu, just do the following from anywhere in any Twig template:
{{ knp_menu_render('AcmeDemoBundle:Builder:mainMenu') }}
Do a ./app/console router:debug - it will show you all the routes registered in your application. I am guessing page_show is not one of them.
The documentation you are using probably expects you to add your own routes/pages to the menu like this:
$menu->addChild('Home', array('route' => 'homepage'));
Where 'homepage' has to already exist. So does 'show_page'. So you need a controller somewhere that handles a request to the show_page route, or exchange show_page for a route that you have already defined in your app. Hope I made sense.
Following the tutorial exactly, this error is caused by line 25 in the file
2 // src/Acme/MainBundle/Menu/MenuBuilder.php
...
25 $menu->addChild('Home', array('route' => 'homepage'));
The tutorial code assumes that you have a route called 'homepage'. Assuming you set this up inside a custom Bundle, then a quick way to solve this problem so you can get the tutorial up and running is to go to...
// src/Acme/MainBundle/Resources/config/routing.yml
...and copy the homepage route from there (will look something like acme_main_bundle_homepage)

Unable to find template "SonataAdminBundle:CRUD:list__action_show.html.twig"

Error showed :
Unable to find template "SonataAdminBundle:CRUD:list__action_show.html.twig" in SonataAdminBundle:CRUD:base_list_field.html.twig at line 23.
Configure entity for only show list(remove create, edit and delete routes) and showed this error, i find this template in sonata admin bundle but no exists, please help me with this issue.
There is really no template called: SonataAdminBundle:CRUD:list__action_show.html.twig
Your problem is not in routes, but in declaring wrong inline action in configureListFields method. The inline action should be called view so the SonataAdminBundle:CRUD:list__action_view.html.twig will be called.
The inline actions should be declared like this:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
//... some other fields ...
// add "show" link in each row of table
->add('_action', 'actions', array(
'actions' => array(
'view' => array(),
)
))
;
}

Silverstripe tumblr-like Post Types

I am trying to create a back-end interface for silverstripe that gives the CMS user the option to choose between a set of Post Types (like tumblr) in Silverstripe3. So they can choose to create a News Post, Video Post, Gallery Post, etc.
I initially started off giving all Posts the necessary fields for each Type and adding an enum field that allowed the user to choose the Post Type. I then used the forTemplate method to set the template dependent upon which Post Type was chosen.
class Post extends DataObject {
static $db = array(
'Title' => 'Varchar(255),
'Entry' => 'HTMLText',
'Type' => 'enum('Video, Photo, Gallery, Music')
);
static $many_many = array(
'Videos' => 'SiteVideo',
'Photos' => 'SitePhoto,
'Songs' => 'SiteMp3'
);
public function forTemplate() {
switch ($this->Type) {
case 'Video':
return $this->renderWith('VideoPost');
break;
case 'Photo':
return $this->renderWith('ImagePost');
break;
etc...
}
function getCMSFields($params=null) {
$fields = parent::getCMSFields($params);
...
$videosField = new GridField(
'Videos',
'Videos',
$this->Videos()->sort('SortOrder'),
$gridFieldConfig
);
$fields->addFieldToTab('Root.Videos', $photosField);
$photosField = new GridField(
'Photos',
'Photos',
$this->Photos()->sort('SortOrder'),
$gridFieldConfig
);
$fields->addFieldToTab('Root.Videos', $photosField);
return $fields;
}
}
I would rather the user be able to choose the Post Type in the backend and only the appropriate tabs show up. So if you choose Video, only the Video GridField tab would show up. If you choose Photo Type only the Photo's GridField would show.Then I would like to be able to call something like
public function PostList() {
Posts::get()
}
and be able to output all PostTypes sorted by date.
Does anyone know how this might be accomplished? Thanks.
Well the first part can be accomplished using javascript. Check out this tutorial and the docs let me know if you have questions on it.
The second part would be trickier but I think you could do something with the page controller. Include a method that outputs a different template based on the enum value but you would have to set links somewhere.
I managed this with DataObjectManager in 2.4.7 as I had numerous DataObjects and all were included in one page but I'm not sure if that is feasible in SS3.
return $this->renderWith(array('CustomTemplate'));
This line of code will output the page using a different template. You need to include it in a method and then call that method when the appropriate link is clicked.

Magento: add icons to the menu-links in your Account-dashboard

Is it possible to add icons to all/some of the menu-links in your account-dashboard? Is there a node/style-attribute in the layout XML-file that should come with the addLink-action?
<action method="addLink" translate="label" module="randomname"><name>randomname</name><path>randomname/index/credits</path><label>Credits</label></action>
You have your default Account Dashboard, Account Information, Addresses, My Orders,... menu-items, but I added a new one; "Credits", and I want to make it "pop" out with an icon and/or another background-color. Couldn't figure out how to do it so far.
Thanks!
EDIT:
Ok, I've found out that there's no parameter to set a css class or id in the addLink() function:
public function addLink($name, $path, $label, $urlParams=array())
{
$this->_links[$name] = new Varien_Object(array(
'name' => $name,
'path' => $path,
'label' => $label,
'url' => $this->getUrl($path, $urlParams),
));
return $this;
}
Now you have two options to add icons to the links. 1. Overwrite the Mage_Customer_Block_Account_Navigation Block Class within your own module and extend the addLink method or 2. you could set the css class/id via jQuery. Good Luck!

Resources