Default variant options on Sylius product show page - php

I am using Sylius eCommerce framework. I want to preselect product options for provided route parameters. I created a custom route for the product show page. An additional parameter of the route should result in another pre-selection (default value) of the product option.
For example http://example.org/products/t-shirt/red will add the route parameter color=red and preselect the color option the the value "red".
Actually I am not sure how to solve my problem. Any ideas?

I resolved my problem providing the parameter from the main request to the rendered one:
{{ render(url('sylius_shop_partial_cart_add_item', {'template': '#SyliusShop/Product/Show/_addToCart.html.twig', 'productId': product.id, 'manufacturer': app.request.get('myparam')})) }}
An in the rendered template I used this value for the default param:
{{ form_row(option_form, { 'attr': { 'data-option': option_form.vars.name, 'disabled': 'disabled' }, 'value': ~ app.request.get('myparam') }) }}
I think it's not the best solution but it fits my needs.

Related

Sonata admin: list one to many in edit page

I have a many many to many relation in Sonata (two one to many relations to be exact), Brand and Retailer.
In the Brand admin Edit page, I want to display all the retailers as a list (so just a read only version), instead of having the normal edit (at the moment, on this brand edit page, I can manage the relationship between this brand and retailers - add a new one, delete an existing one).
I tried to explore two routes so far:
Edit page will load a custom twig
Using a custom field type for this field only
My issue is, with both options, I didn't manage to get to a solution
So here is what I have done:
1 - Loading a custom edit twig:
services:
xx_brand.admin.brand_brand:
calls:
- [ setTemplate, [edit, xxBrandBundle:Admin:base_edit.html.twig]]
On this case, base_edit is an exact copy of the sonata base_edit, but it loads my custom base_edit_form:
{% use 'xxBrandBundle:Admin:base_edit_form.html.twig' with form as parentForm %}
From here I can exclude the default rendering of the retailers, but can't find a way to then render it as I want, as I am not sure how the retailers entity is managed here:
{% if admin.formfielddescriptions[field_name] is defined and field_name != 'retailers' %}
{{ form_row(form[field_name])}}
{% else %}
<ul>
<li>retailer1</li>
<li>retailer2</li>
</ul>
{% endif %}
2 - For the approach of a custom field type, I tried to follow the documentation
Creating the Bundle/Form/Type/ListType.php
Creating the /BrandBundle/Resources/views/form/list.html.twig
Using the ListType in configureFormFields:
use XX\BrandBundle\Form\Type\ListType;
...
->add('retailers', 'ListType');
But I then get an error XX\BrandBundle\Form\Type\ListType
So basically, because I couldn't get it to work, are any of these two options good to solve my issue ?
If so, could anyone please advice on what I am missing there ??
Any help will be very much appreciated :)
You can use sonata_type_model_list: https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html#example
Like so:
class BrandAdmin extends Admin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('retailer', 'sonata_type_model_list', array(
'btn_add' => false,
'btn_delete' => false,
));
}
}

Hypertext to route not working

Hi I have a hyperlink from a page:
<h3>hitest</h3>
the route:
Route::get('hitest', function(){ return 'hitest message';});
There is an error:
No query results for model [App\User2].
hyper link is from a page with this url
/userpage/1
the 1 is a model object.
Shouldn't the hyperlink route to /hitest ?
Please see my other post: Strange behavior with routing and hypertext.
I'm new at web development. Is there configurations for routing? The app is hosted (not local).
As bytesarelife already mentioned, you can use the url()-function, like so:
{{ url('your/url/') }}
A better way in terms of maintainability would be to give your routes names, so you would not have to replace every url in every template once you want to change it. You can do so, by adding the name in your routes:
Route::get('hitest', function(){ return 'hitest message';})->name('getHittest');
And then you can use the route function in your view:
{{ route('getHittest') }}

Retrieving a value from the request in twig - Symfony2

I'm sure that I have managed to do this before but for some reason it's not working correctly.
I have a page in my project that lists clients by their account status. It's not mandatory, but a status CAN be passed via the URL, else it simply lists the active clients.
example:
myurl.com/clients/closed
would list all the clients with closed accounts.
My route is set up as follows:
app_show_clients:
path: /clients/{status}
defaults: { _controller: AppBundle:Client:showClients, status: 'active' }
requirements:
status: active|closed
And in my Twig file, I am attempting to retrieve the status as follows:
{{ app.request.get('status') }}
in order to show the status in the title of the page.
However, this just comes back as blank - though if I write:
{{ dump(app.request.get('status')) }}
It gives me the value "closed" in the var dump.
What am I missing here?
Thanks in advance
Michael
Wouldn't it be better to use the controller to handle the request properly?
function showClientsAction($status)
{
// Do some important stuff to fetch clients matching the request
// Return array assumes #Template annotation usage
return array('clients' => $clients, 'status' => $status);
}
So you could simply write {{ status }} in your template.
Did you try by this way:
{{ app.request.query.get("status") }}
Issue has been solved - I had placed the code in the wrong place, therefore I thought it had failed when it actually was working using:
{{ app.request.get('status') }}
which is the correct way.

Why is passing of second variable on href much flexible in Laravel?

I am using Laravel Framework 4.1.30.
My Original Problem was...
I got a working route that when I type "localhost/user/alvin" it works but when I click a drop-down bar, it does not work.
drop-down bar from template was: (li tag removed)
{{ Auth::user()->username }}
route is:
Route::get('/user/{username}', array(
'as' => 'profile-user',
'uses' => 'ProfileController#user'));
View of URL when I click the dropdown bar is:
"localhost/user/%7Busername%7D"
I got 3 working answers for this from other communities.
First was a simple:
{{ Auth::user()->username }}
Second was:
{{ Auth::user()->username }}
Last was:
<a href="{{ URL::route('profile-user',['username'=>Auth::user()->username]) }}">
{{ Auth::user()->username}}</a>
Originally I thought the simplest (First Solution) form would be the wisest choice but as per most developer they choose the Third answer because it allows some flexibility to some extent.
I want to implement a good Quality Assured code as much as possible.
My question... Why would the 3rd solution be much more flexible compared to a much simpler code?
You can even use a more flexible, shorter modification of the third one:
<a href="{{ route('profile-user', Auth::user()->username) }}">
When you are using named routes, you do not put actual URLs, nor controller method names into your Views. You only provide a route name to your Views, and all other things are defined in your routes, and can be changed, if needed, without making breaking anything else. It would be a waste not to use this feature.
It is also more flexible, by not specifying the parameter name, so it can be changed in the routes too, without breaking the Views. This way they will be just taken in the order they were inputted. They are also read by a controller method in the order, i.e. public function index($parameter1, $parameter2) not by an associative array.
The route() is an alias to URL::route().

symfony2 -How to retrieve data from database globally in every page

I want to retrieve Cities names from a table in the database and put them as options in a select input (combobox) which is defined in 'layout.html.twig' . All my views extends 'layout.html.twig', so how can I access to cities names in every page?
[Solution]
I'm not able to respond to my topic ,I didn't have much reputation so I edit my topic
I have found the solution, using "embedding controllers"
first I've created an action to retreive all cities names:
public function listCitiesAction(){
// retreiving cities
$entities = $this->getDoctrine()->getRepository("MedAdBundle:City")->findAll();
return $this->render('MedAdBundle:Ville:list_cities.html.twig',
array('entities' => $entities));
}
this action render list_cities.html.twig defined as :
<select class="form-control">
{% for entity in entities %}
<option>{{ entity.name}}</option>
{% endfor %}
</select>
finnaly I edit my layout.html.twig
<div>
{{ render(controller('MedAdBundle:City:listCities'))}}
</div>
In this way I can access to cities combobox in every page in my app ;)
Another nice way would be use render.
This allows you to call a controller out of your layout.html.twig
{{ render(controller("AcmeDemoBundle:Helper:citySelector")) }}
you also can cache the output with ESI.
It's well explained in the cookbook.
http://symfony.com/doc/current/cookbook/templating/global_variables.html
I would go with these steps:
Write a form hosting a symfony entity field configured to work with the Cities table
Define this form as a service in the DIC
Define a twig extension that exposes a function to output the form HTML
Use the twig function in the layout.html.twig that is extended by all the other templates
As an optimization I would look how I could wire Doctrine with some caching system (e.g. memcached) to avoid hitting the database on each page load.
This is where you can find documentation about the entity field: http://symfony.com/doc/current/reference/forms/types/entity.html
Use the Symfony documentation to find how to define a form as a service and how to write your own twig extension.

Categories