With ...MyBundle\Resources\views\Menu\knp_menu.html.twig, deleting the </li> has no effect on the rendered menu. (Removing the tag is done to remove the space between inline list elements.) I have followed the advice provided in this answer, including the {% import 'knp_menu.html.twig' as knp_menu %} mentioned toward the bottom of that post. Is this because knp_menu.html.twig already extends knp_menu_base.html.twig? Or what?
layout.html.twig:
...
{{ render(controller('VolVolBundle:Default:userMenu')) }}
...
userMenuAction:
$user = $this->getUser();
$tool = $this->container->get('vol.toolbox');
$type = $tool->getUserType($user);
return $this->render(
'VolVolBundle:Default:userMenu.html.twig', array('type' => $type)
);
userMenu.html.twig
...
{% if type is not null %}
{% set menu = "VolVolBundle:Builder:"~type~"Menu" %}
{{ knp_menu_render(menu) }}
{% endif %}
The answer was found deep in here. All that's required to do a global override of the template is to modify config.yml.
config.yml:
...
knp_menu:
twig: # use "twig: false" to disable the Twig extension and the TwigRenderer
template: VolVolBundle:Menu:knp_menu.html.twig
...
Related
I am trying to go throught this tutorial: http://www.sitepoint.com/adding-social-network-features-php-app-neo4j/ But using the Symfony Framework instead of Silex.
I have been able to set up Neo4j to run with Symfony and am able to right user data to the graph. Now I would like to display all user email addresses in a list. I have taken this script:
public function home(Application $application, Request $request)
{
$neo = $application['neo'];
$q = 'MATCH (user:User) RETURN user';
$result = $neo->sendCypherQuery($q)->getResult();
$users = $result->get('user');
return $application['twig']->render('index.html.twig', array(
'users' => $users
));
}
And adapted it to read:
public function showUsersAction()
{
$em = $this->container->get('neo4j.manager');
$query = 'MATCH (n:`User`) RETURN n';
$users = $em->cypherQuery($query);
//print_r($users);
return $this->render('UserBundle:Account:showUsers.html.twig', array('users' =>$users));
}
And The twig looks as follows:
{% extends '::base.html.twig' %}
{% block content %}
<h1>get all users:</h1>
<ul>
{% for user in users %}
<li>{{ user.property('email') }}</li>
{% endfor %}
</ul>
{% endblock %}
But something in the twig is wrong, im getting the error:
Method "property" for object "Everyman\Neo4j\Query\Row" does not exist in UserBundle:Account:showUsers.html.twig at line 6
The problem was found in the syntax of the twig file. After consulting this page: https://github.com/jadell/neo4jphp/wiki/Cypher-and-gremlin-queries it became clear, that I had to include user['n'] in my twig template. The twig template now looks as such:
{% extends '::base.html.twig' %}
{% block content %}
<h1>get all users:</h1>
<ul>
{% for user in users %}
<li>{{ user['n'].getProperty('email') }}</li>
{% endfor %}
</ul>
{% endblock %}
I'm the author of the article you mentioned. The thing is that you use a different neo4j library than the one used in the article, hence neoclient, so the methods used in the article are different than the methods provided with neo4jphp.
As NeoClient uses heavily the Symfony components, integrating it in Symfony is really easy, you just need to override the DI. Example here : https://github.com/graphaware/GithubNeo4j/tree/master/src/GraphAware/Neo4jBundle
You'll then be able to use the methods illustrated in the 3 articles I wrote on Sitepoint.
So your problem with the twig template is that he doesn't find the getProperty method of the node object class, which is normal as neo4jphp returns Row object classes.
If you switch back to neoclient, as in the article, in the Twig template you can just write :
{% for user in users %}
<li>{{ user.getProperty('email') }}</li>
{% endfor %}
I'm facing a problem with Sonata Admin. Is there a way to disable the "list view" ? I would like to fetch the first entity in the database and to go on it when clicking on the link in the sidebar. But not for all entry.
Is there a clean way to do it ? (I have the idea to check the entity in a custom controller, and to redirect to list view or edit view depending on the entity, but that's not really clean)
If you want to do a custom query for your list view, you could override the createQuery method in your Admin class like this :
class EntityAdmin
{
public function createQuery($context = 'list')
{
$query = parent::createQuery($context);
$query->andWhere(
$query->expr()->eq($query->getRootAlias() . '.id', ':id')
);
$query->setParameter('id', 1);
return $query;
}
}
You will have only your first entity in your list view.
UPDATE
You could override the standard_layout.html.twig to change the link in your sidebar :
First you need to set where your template is located:
app/config/config.yml
sonata_admin:
templates:
layout: ApplicationSonataAdminBundle::standard_layout.html.twig
Change the behaviour of the sidebar, for Sonata Admin 2.3 this is how you do it :
src/Application/Sonata/AdminBundle/Resources/Views/standard_layout.html.twig l.224
<ul class="treeview-menu{% if active %} active{% endif %}">
{% for admin in group.items %}
{% if admin.code == 'sonata.admin.entity' and
admin.hasroute('edit') and
admin.isGranted('EDIT') %}
<li{% if app.request.get('_sonata_admin') == admin.code %} class="active"{% endif %}><i class="fa fa-angle-double-right"></i> {{ admin.label|trans({}, admin.translationdomain) }}</li>
{% else %}
{% if admin.hasroute('list') and admin.isGranted('LIST') %}
<li{% if app.request.get('_sonata_admin') == admin.code %} class="active"{% endif %}><i class="fa fa-angle-double-right"></i> {{ admin.label|trans({}, admin.translationdomain) }}</li>
{% endif %}
{% endif %}
{% endfor %}
</ul>
You must change 'sonata.admin.entity' by the identifier of your admin service.
Also if you want to remove access to the list you should add in your Admin class
use Sonata\AdminBundle\Route\RouteCollection;
class EntityAdmin
{
protected function configureRoutes(RouteCollection $collection)
{
$collection->remove('list');
}
}
I'm using Symfony 2.3 and the KnpMenuBundle.
Is it possible to use a translation domain for menu items?
Like this:
$menu['management']->addChild(
'msg.user.list',
array(
'route' => 'user_list',
'translation_domain' => 'navigation'
)
);
According to the Symfony documentation and KnpMenuBundle documentation,you may set the translation domain (menu in my snippets), while adding menu items in your MenuBuilder class:
$menu->addChild('Home', array('route' => 'homepage'))
->setExtra('translation_domain', 'menu');
You may better want to add the translation domain into the whole menu instead:
$menu = $this->factory->createItem('root')
->setExtra('translation_domain', 'menu');
Then create a file named knp_menu.html.twig in:
app/Resources/views/menu/
and put this in it:
{% extends 'knp_menu.html.twig' %}
{% block label %}
{% if options.allow_safe_labels and item.getExtra('safe_label', false) %}
{{ item.label | raw | trans(item.getExtra('translation_params', {}), item.getExtra('translation_domain', 'menu'))}}
{% else %}
{{ item.label | trans(item.getExtra('translation_params', {}), item.getExtra('translation_domain', 'menu')) }}
{% endif %}
{% endblock %}
(If the file already exists, just replace the {% block label %}{% endblock %} part)
Be carefull to have translation files (.xliff or whatever) naming strategy like:
{translation_domain}.{locale}.{extenstion}
for example:
menu.fa.xliff
In this path:
app/Resources/translations/
The last part is:
# app/config/config.yml
knp_menu:
twig:
template: knp_menu.html.twig
Up-Vote this nice tutorial.
Have fun!
When I've created a form:
$builder = $this->createFormBuilder();
$form = $builder->add( 'add', 'button')->getForm();
and render it:
<div><button type="button" id="form_add" name="form[add]">Add</button></div>
the attributes type, id and name are created.
I want to erase this attributes but I don't know how to do it. I've tried to do:
$builder = $this->createFormBuilder();
$form = $builder->add( 'add', 'button', array( 'attr' => array() ) )->getForm();
without any success.
How could I do it?
Greetings and thanks
I messed around with this for a while and the closest I could get was to have them render with empty attributes. However, that's because the project I tested with is Symfony 2.0, and in that version it's impossible to completely remove the attributes since Symfony\Component\Form\FormView::$vars is private.
However, in Symfony 2.1 and later, that same property is public so you should be able to modify (or delete) the attributes/vars directly without being constrained by the FormView api.
First, create your own type to represent this "naked button"
src/Your/Bundle/Form/NakedButtonType.php
<?php
namespace Your\Bundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
class NakedButtonType extends AbstractType
{
/**
* (non-PHPdoc)
* #see Symfony\Component\Form.FormTypeInterface::getName()
*/
public function getName()
{
return "naked_button";
}
/**
* (non-PHPdoc)
* #see Symfony\Component\Form.AbstractType::getParent()
*/
public function getParent(array $options)
{
return 'button';
}
/**
* (non-PHPdoc)
* #see Symfony\Component\Form.AbstractType::buildViewBottomUp()
*/
public function buildViewBottomUp(FormView $view, FormInterface $form)
{
// Symfony 2.0
// This will still render the attributes, but they will have no value
$view->set('id', null);
$view->setAttribute('type', null);
// Symfomy >= 2.1
// This *should* remove them completely
unset( $view->vars['id'] );
unset( $view->vars['attr']['type'] );
}
}
Now, tell the service container how to build your type
app/config/config.yml
services:
form.type.naked_button:
class: Your\Bundle\Form\NakedButtonType
tags:
- {name: form.type, alias: naked_button}
Then update your parent form to use your new type instead of the ootb "button" type.
$builder = $this->createFormBuilder();
$form = $builder->add( 'add', 'naked_button')->getForm();
All of that being said...
If you want these buttons w/o any attributes, why not just put them like that directly into your view?
<form>
{{ form_errors(form) }}
{{ form_rest(form) }}
<div>
<button>Add</button>
</div>
</form>
All of this custom type nonsense seems like alot of overhead to render something you clearly don't need Symfony to manage for you.
While not a good idea, I will help you load the gun and shoot yourself in the foot :-)
Create a new Resources/views/Form/fields.html.twig file, and put the following in it:
{% block widget_attributes %}
{% spaceless %}
id="{{ id }}" name="{{ full_name }}"{% if read_only %} readonly="readonly"{% endif %}{% if disabled %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}
{% for attrname, attrvalue in attr %}{% if attrname in ['placeholder', 'title'] %}{{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}" {% else %}{{ attrname }}="{{ attrvalue }}" {% endif %}{% endfor %}
{% endspaceless %}
{% endblock widget_attributes %}
If you really, really want to remove all the attributes on there, you can remove the {% for attrname ... %} line. That will remove the attributes from all form fields. If you add some logic, you can have it apply to just specific fields.
Next step, you need to register your fields helpers. In your app/config/config.yml file, add the following line:
twig:
form:
resources:
- 'SomeBundle:Form:fields.html.twig'
I am trying to change the template for the registration form in my application so that I can add some other HTML to it. Here is the /My/UserBundle/Resources/views/Registration/register.html.twig file:
{% extends "MyUserBundle::layout.html.twig" %}
{% block fos_user_content %}
<section class="register site-content">
<header>
<h1>{{ 'layout.register'|trans({}, 'FOSUserBundle') }}</h1>
</header>
<div class="block">
{% include "FOSUserBundle:Registration:register_content.html.twig" %}
</div>
</section>
{% endblock fos_user_content %}
And I have successfully overridden the layout.html.twig:
{% extends 'MyMainBundle::layout.html.twig' %}
{% block title %}{{ site_name }}{% endblock %}
{% block content %}
{% for key, message in app.session.getFlashes() %}
<div class="{{ key }}">
{{ message|trans({}, 'FOSUserBundle') }}
</div>
{% endfor %}
{% block fos_user_content %}{% endblock %}
{% endblock %}
as well as form.html.twig:
{% extends 'FOSUserBundle::form.html.twig' %}
{% block field_row %}
<li class="form_row">
{{ form_label(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</li>
{% endblock field_row %}
{% block form_widget %}
<ul {{ block('container_attributes') }}>
{{ block('field_rows') }}
{{ form_rest(form) }}
</ul>
{% endblock form_widget %}
config parts:
# FOS User Configuration
fos_user:
db_driver: orm
firewall_name: main
user_class: My\UserBundle\Entity\User
from_email:
address: %admin_email%
sender_name: %site_name%
template:
engine: twig
theme: MyUserBundle::form.html.twig
I have cleared my cache.
Whenever I go to
http://localhost/register/
apache just hangs until it times out.
The best I can figure out, is the PHP maximum execution message says it crashes on a twig template in the cache on line 16. That line is function doGetParent(...) The file is:
<?php
/* FOSUserBundle::form.html.twig */
class __TwigTemplate_9cf68a2af1db50466c556a735bcdeba0 extends Twig_Template
{
public function __construct(Twig_Environment $env)
{
parent::__construct($env);
$this->blocks = array(
'field_row' => array($this, 'block_field_row'),
'form_widget' => array($this, 'block_form_widget'),
);
}
protected function doGetParent(array $context)
{
return "FOSUserBundle::form.html.twig";
}
protected function doDisplay(array $context, array $blocks = array())
{
$this->getParent($context)->display($context, array_merge($this->blocks, $blocks));
}
// line 3
public function block_field_row($context, array $blocks = array())
{
// line 4
echo " <li class=\"form_row\">
";
// line 5
echo $this->env->getExtension('form')->renderLabel($this->getContext($context, "form"));
echo "
";
// line 6
echo $this->env->getExtension('form')->renderErrors($this->getContext($context, "form"));
echo "
";
// line 7
echo $this->env->getExtension('form')->renderWidget($this->getContext($context, "form"));
echo "
</li>
";
}
// line 11
public function block_form_widget($context, array $blocks = array())
{
// line 12
echo " <ul ";
$this->displayBlock("container_attributes", $context, $blocks);
echo ">
";
// line 13
$this->displayBlock("field_rows", $context, $blocks);
echo "
";
// line 14
echo $this->env->getExtension('form')->renderRest($this->getContext($context, "form"));
echo "
</ul>
";
}
public function getTemplateName()
{
return "FOSUserBundle::form.html.twig";
}
public function isTraitable()
{
return false;
}
}
It has also timed out on \vendor\twig\lib\Twig\Template.php on line 65 Which is public function getParent(array $context)
So clearly there is some problem with getParent but I don't know what that means or how to fix it.
According to the FOSUserBundle documentation:
The easiest way to override a bundle's template is to simply place a
new one in your app/Resources folder. To override the layout template
located at Resources/views/layout.html.twig in the FOSUserBundle
directory, you would place you new layout template at
app/Resources/FOSUserBundle/views/layout.html.twig.
As you can see the pattern for overriding templates in this way is to
create a folder with the name of the bundle class in the app/Resources
directory. Then add your new template to this folder, preserving the
directory structure from the original bundle.
In my project I override FOSUserBundle's layout as they said and it's work like a charm.
So doing it at the same way you will need to create app/Resources/FOSUserBundle/views/Registration/register.html.twig. (or the form you want to override)
EDIT
Ok, I just realize that you've chosen to extend the FOSUserBundle. In that case instead of app/Resources/ you need to do it inside your bundle. But you don't need to put
{% extends 'FOSUserBundle::form.html.twig' %}
The FOSUserBundle will detect that you are overriding the bundle and will be extended automatically.
And you also need to tell your bundle that FOSUserBundle is its parent.
class YourBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
Copy the file:
{% include "FOSUserBundle:Registration:register_content.html.twig" %}
to the: app/Resources/FOSUserBundle/views/ catalogue. Then it will overwrite the base FOS form. You can edit the copied file.
Otherwise you must override the FOS RegisterController.
I feel stupid, just removed the {% extends ... %} line in form.html.twig and it worked. I guess I don't understand enough about what is inherited in bundles. (I did want to inherit from the FOSUserBundle form.html.twig, but I don't know how to access that from a bundle which inherits from FOSUserBundle)