Knp menu translations - symfony

According to knp, this should be how you can easily add translations to your menu. It ain't working for me.
My menu class:
public function createMainMenuLeft(array $options)
{
$menuLeft = $this->factory->createItem('root', array('childrenAttributes' => array('class' => 'left')));
$menuLeft->addChild('test', array('route' => 'test_route'))
->setExtra('translation_domain', 'AppBundle');
return $menuLeft;
}
In AppBundle/Resources/translations/messages.en.yml I have
test: nothing
However, my menu still has the label 'test' not the value from the translations file. Am I missing something here?
My locale is set in config.yml to en.

After some research I found the way to do it inside the documentation from the bundle, not the one from symfony. The template hast to be overwritten. Source

Related

How to style your Drupal 8 module?

We are building a custom Drupal 8 module, but we want control over the admin styling of it. For example style the default links above our table, convert the word -MISSING- to a proper icon, etc.
All can be easily done with CSS and Twig, but the problem is how do you overwrite it? We want to keep using the default seven admin template.
So all CSS and Twig has to be inside the module I guess.
Otherwise a new user that installs our module don't get the new templates.
Update
We think the hook_thme method was the way to go, but still no succes to overwrite the classy/seven block:
function ejb_project_theme() {
$theme['block'] = [
'template' => 'block',
];
$theme['block__ejb_project'] = [
'template' => 'block',
];
$theme['block--ejb_project'] = [
'template' => 'block',
];
$theme['page--block'] = [
'template' => 'block',
];
return $theme;
}
Yes, you need to add the theming to the module through a libraries.yml file on your module folder. Check https://www.drupal.org/developing/api/8/assets for complete information about it.
For templates creation/overriding check Create custom twig templates from custom module and Drupal 8 - Override template with module
The approach you considering is a good one: define your our theme files using hook_theme().
In your .module file:
function ejb_project_theme($existing, $type, $theme, $path) {
return [
'ejb_admin_block' => [
'variables' => [
'attributes' => [],
'var_1' => 'default_output',
],
],
];
}
Then in the module's templates directory you can create a file named: ejb-admin-block.html.twig to provide your markup.
This is basically the inverse of what you describe: instead of overriding Seven's template, you're defining a new one and admin themes have the option to override yours.
For more on the details of the array in hook_theme see the documentation above and the theme system overview article from Drupal.org.
You can check for this also. see screenshot here
place suggested file_name.html.twig in themes/yourtheme/templates dir
there is also a filename from where that output is coming from. you can copy that file and paste into yourtheme/template dir. clear cache. this will work.
You can try:
use \Symfony\Component\HttpFoundation\RedirectResponse;
function hook_page_attachments_alter(array &$attachments) {
$route_match = \Drupal::service('current_route_match');
$route_name = $route_match->getRouteName();
if ($route_name == 'some.route') {
$response = new RedirectResponse("your_path");
$response->send();
}
Either you should redirect to a path or try to call a custom template which will be in your_module/template dir

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)

How can I add css classes to specific symfony2 form choices?

I could do this with Javascript, but I was wondering if I could add a css class to specific symfony2 form choices (not the choice field itself, but the individual choices).
For example I want to apply different css styles to individual 'option' tags inside a 'select'. I could only find a way to add a class to the tag.
Thanks in advance.
I think you can simply do:
{{ form_widget(form.name, { 'attr' : { 'class' : 'myClass' } }) }}
... as explained here, without creating your own form style.
You can override the layout of specific widgets in your form, which means you can override the way the select renders and put in custom code to check what the value of the option is and output your custom class there.
You need to apply a custom layout to your form, like so
{% form_theme form 'form_theme.html.twig' %}
Then inside the layout file you need to override the specific field for that specific form (unless of course you want to edit the choice_widget directly in which case all fields that use choice will have the functionality).
To do that you have to copy the widget, so choice_widget, then name it [_formName_fieldName_widget]
So if your form name was events and your field name was requireTickets, it'd be _events_requireTickets_widget
The answers that were already provided are very good, and I think #CriticalImpact's is the most flexible. But I just wanted to mention that if you're using a form class, you can also add extra attributes to the field via the form builder definition itself:
class SomeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('someField', "text", array("attr" => array("class" => "someCssClass")))
->add("save", "submit");
}
}
I've found this helpful for basic forms, because it still allows you to make a simple {{ form(someForm) }} call in your Twig file.
(Note that this solution still has the drawback that #CriticalImpact mentioned above)
Add attributes like CSS styles to individual choices can nowadays be achieved with choice_attr, e.g.:
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
// ...
$builder->add('attending', ChoiceType::class, array(
'choices' => array(
'Yes' => true,
'No' => false,
'Maybe' => null,
),
'choice_attr' => function($val, $key, $index) {
// adds a class like attending_yes, attending_no, etc
return ['class' => 'attending_'.strtolower($key)];
},
));

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!

Drupal 7 hook_theme() not loading template file

I'm trying to get a very simple module to load a template file using drupal's hook_theme(). It's pretty much as simple as you can possibly imagine.
function sectionheader_theme ( $existing, $type, $theme, $path ) {
return array(
'sectionheader' => array(
'variables' => array( 'foo' => NULL ),
'template' => 'sectionheader',
),
);
}
The template is named sectionheader.tpl.php. The rest of the module is working as expected. I've cleared the Drupal cache. I've inserted a die("Debug") statement in this function, and it is being executed, but my template is simply not being called, ever. The template merely has some debug text in it so I can see that it's working, but is not visible in any view of the module.
I've done everything in every example I can find, I've even copied and pasted code directly from other modules, and this template will still not load.
Note, if you have put your template file in a /theme subfolder in your module dir ( which is best practice), you'll also need to specify the file path in hook_theme
function example_theme($existing, $type, $theme, $path) {
return array(
'example_function' => array(
'variables' => array('var1' => array(), 'var2' => array(), 'var3' => array()),
'template' => 'example-template',
'path' => drupal_get_path('module', 'example').'/theme'
),
);
}
I had the same problem as yours, and I solved it by clearing the cache, I searched from the database, and in the cid column of table cache, I get something like "theme_registry:*", and I remove them, it works.
As mentioned in the comment above I hit the same problem. Everything was working fine in a development module but when I simply copied this module into a new one that would become my production module the template file no longer worked. I tried everything mentioned above withut luck. The original module was disabled and only the new one was enabled.
I even went back to see if the original module's theme could work and it didn't. hmmmm.
When I changed the name of the theme it suddenly started to work: the template file was located and displayed.
So, it appears that any module that registers a theme name ---- even if it is disabled --- still registers a theme AND it seems that theme names need to be unique throughout the system.
Answer: look for the same theme name being declared in other modules

Resources