Using Symfony2, we have a Product form which has a collection of productOption forms. This works fine, however we wish to have some values within the productOption form set to 0 by default.
I have tried solutions given in other StackOverflow questions, but I still just get blank fields. Currently, the desired default values are being set via the Entity's constructor as such:
// ProductOptionValue Entity
public function __construct()
{
$this->option_values = new \Doctrine\Common\Collections\ArrayCollection();
$this->subtract = true;
$this->price = 0.00;
}
The prototype of the form collection is not standard - it's modified via a macro within the template which outputs the form:
{% macro product_option_value_prototype(product_option_value) %}
<td>
<div class="product_option_value_widget">{{ form_widget(product_option_value.option_value) }}</div>
<div class="product_option_value_error">{{ form_errors(product_option_value.option_value) }}</div>
</td>
<td>
<div class="product_option_value_widget">{{ form_widget(product_option_value.quantity) }}</div>
<div class="product_option_value_error">{{ form_errors(product_option_value.quantity) }}</div>
</td>
<td>
<div class="product_option_value_widget">{{ form_widget(product_option_value.subtract) }}</div>
<div class="product_option_value_error">{{ form_errors(product_option_value.subtract) }}</div>
</td>
<td>
<div class="product_option_value_widget">{{ form_widget(product_option_value.price_multiplier) }}</div>
<div class="product_option_value_error">{{ form_errors(product_option_value.price_multiplier) }}</div>
</td>
<td>
<div class="product_option_value_widget">{{ form_widget(product_option_value.taxed_price) }}</div>
<div class="product_option_value_error">{{ form_errors(product_option_value.taxed_price) }}</div>
</td>
{% endmacro %}
... Which, in the same template is used via:
<table class="product_option_values" data-prototype="{{ _self.product_option_value_prototype(form.product_option_values.vars.prototype)|e }}">
I have tried setting other values as the defaults (10 instead of 0) in case Symfony ignored them at 0.
I have also tried implementing this CollectionTypeExtension Gist. Whatever I try, we still get blank fields by default. Because other solutions on StackOverflow seem to give people success, I feel there must be something wrong elsewhere to the entity or FormType, but we are having no success in finding it. Any ideas?
Related
objective : products database + twig component rendered in a loop.
I have this setup:
A .twig product component with my component markup:
<article class="productCard "><h3 class="productCard__name">{{ product.name }}</h3></article>
A loop in my page which I want to display all the instances of the product but with the twig component template:
{% for product in products %}
<div class="col">
{{ include('components/productCard.html.twig'),{'product.name': product.name}) }}
</div>
{% if loop.index % 4 == 0 %}
</div><div class="row" >
{% endif %}
A Database with the actual list of product.
I'm not sure of the correct syntax of the loop call to achieve what I want (passing the parameters of the database to the loop to the component).
The proper syntax would be:
{% include 'components/productCard.html.twig' %}
No need for using with or passing variables. Then your other snippet should work as-is:
<article class="productCard">
<h3 class="productCard__name">{{ product.name }}</h3>
</article>
I have a course description web page as a university project, where I'm currently working on the page where I'm displaying information regarding the approval status of all the courses. I'm retrieving all the courses stored in a database to an array in php, and then passing this array to the twig-file where I want to iterate through the course-array.
However twig doesn't seem to recognize what the 'course' variable in 'courseList' is, as no information is displayed.
Twig
<tbody>
{% for course in courseList %}
<tr>
<th scope="row">{{ loop.index }}</th>
<td>{{ course.getCourseCode }}</td>
<td>{{ course.getCourseName(0, course.getCourseId) }}</td>
<td class="font-weight-bold">{{ course.getCourseApprovalDue }}</td>
<td>
<div class="progress">
<div class="progress-bar {% if course.getStatus == 0 %} bg-warning
{% elseif coruse.getStatus == 1 %} bg-danger {% else %} bg-success {% endif %}" role="progressbar"
style="width: 100%" aria-valuenow="10" aria-valuemin="0" aria-valuemax="10">{{ course.getStatus }}
</div>
</div>
</td>
</tr>
{% endfor %}
</tbody>
PHP
if ($session->has('loggedin')) {
$courseList = $database->getAllCourses();
echo $twig->render('courseStatus.twig', array('user' => $session->get('user'), 'loggedIn' => true, 'courseList' => $courseList));
} else {
echo $twig->render('login.twig');
}
The database queries and the php functions are working properly. I can verify this by passing a hardcoded course object to the twig file. The coruseList array isn't empty either, as the twig file displays 5 rows when rendering it, which is the same amount as the courses stored in the database.
The array returned from getAllCourses() does not create a new instance of Course, it's just an array containing arrays with Course-data. The solution is to get the arrays, and create a new array with Course-objects with the information returned, and pass this array to the php-file.
I'm in a WP project. I created a custom post type called Team Members
I also created a custom field block with a field called contact_members which is a repeater and has two sub-fields called location_map and member. This member sub-field is related with the custom post type team member.
I have no issues with getting the location_map.
The issue is that I can't get the post type fields. (coming from contact_members->member->post type
<?php
$members = get_field('contact_members');
?>
#foreach($members as $member)
<div class="member {{ $member['location_map'] }}">
<img class="map" src="{{ get_the_post_thumbnail_url($member['member']->ID) }}">
<h3>{{ get_the_title($member['member']->ID) }}</h3>
<p class="position">{{ $member['member']->position }}</p>
<p class="location">{{ $member['member']->location }}</p>
Contact
</div>
#endforeach
On a first look i cant see any problems with your code. What i consider the most possible senario is ( since that i guess you are using the ACF plugin ) the $member['meber'] variable holds the (int) post_id and not the Post Object. Have you tried var_dump() ?
Cheers!
I found the answer, I should put [0] after $member['member'], in order to reach the array's first element and after that I can finally go after what I want from the object, so:
<?php $members = get_field('contact_members'); ?>
#foreach($members as $member)
<div class="member {{ $member['location_map'] }}">
<img class="member-image" src="{{ get_the_post_thumbnail_url($member['member'][0]->ID) }}">
<h3 class="member-name">{{ $member['member'][0]->post_title }}</h3>
<p class="position">{{ $member['member'][0]->position }}</p>
<p class="location">{{ $member['member'][0]->location }}</p>
Contact
</div>
#endforeach
I'm following the basic quickstart with laravel 5. I'm deviating only in that I'm using TwigBridge to allow me to use twig templates instead of blade. I'm getting in error when I tried to load my view.
Unknown "method_field" function in "/home/vagrant/laravel-test/resources/views/tasks.twig" at line 63.
method_field should be an available default helper function in laravel. I'm not really sure why twigbridge isn't finding it. Is there something I'm missing I need to make this function available to TwigBridge?
Edit
Here's my code:
{% if tasks|length > 0 %}
<div class="panel panel-default">
<div class="panel-heading">
Current Tasks
</div>
<div class="panel-body">
<table class="table table-striped task-table">
<!-- Table Headings -->
<thead>
<th>Task</th>
<th> </th>
</thead>
<!-- Table Body -->
<tbody>
{% for task in tasks %}
<tr>
<!-- Task Name -->
<td class="table-text">
<div>{{ task.name }}</div>
</td>
<td>
<form action="{{ url('task/'~task.id) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button>Delete Task</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endif %}
Edit 2
I partially figured it out. I had to add method_field to the config/twigbridge.php file in the functions array:
'functions' => [
'elixir',
'head',
'last',
'method_field',
].
This got it to run the function, however the output is converted to html entities because it actually renders the input tag text instead of the tag itself. So I need to figure out away to make it not escape the output.
The answer is that you have to add the function to the config/twigbridge.php with an is_safe option. That can be done this way:
in /app/config/twigbridge.php
'functions' => [
// Other functions
'method_field' => [
'is_safe' => [true],
],
],
I am trying to implement a comment section on a blog with Symfony and Doctrine, using Twig templates.
I am having some trouble trying to implement a response system for my comments. I would like to have something like this:
<div class="comment">
<p>This is comment number 1</p>
<div class="response">
<p>This is a response to comment number 1</p>
<div class="response">
<p>This is a response to response just above</p>
</div>
</div>
</div>
So I would like to have sort of a nested comment system.
I have a Comment entity with a $response property having a OneToOne Relation with another Comment entity:
/**
* #ORM\OneToOne(targetEntity="Comment")
*/
protected $response;
In my controller, I just get my comments like this:
$comments = $this->getDoctrine()
->getManager()
->getRepository('AppBundle:Comment')
->findByArticle($article);
Now I am trying to create the HTML (Twig) but I don't know how to loop throught all comment and associated response so I can create the HTML as I wrote just above...
Anyone who can help me with that?
Thanks.
All you need is recurrence. You have a few options to choose from, one of them is using a macro.
Create twig file with your macro:
{# src/AppBundle/Resources/views/Default/_macro.html.twig #}
{% macro print_comments_recursively(comment) %}
<div class="response">
<p>{{ comment }}</p> {# implement __toString on your Comment class or print appropriate property #}
{% if comment.response is not null %}
{{ _self.print_comments_recursively(comment.response) }}
{% endif %}
</div>
{% endmacro %}
Import macro in your view and use it:
{% import 'AppBundle:Default:_macro.html.twig' as macros %}
<div class="comment">
<p>{{ comment }}</p>
{% if comment.response %}
{{ macros.print_comments_recursively(comment.response) }}
{% endif %}
</div>
Here you have similar problem, already solved, with other solutions: How to render a tree in Twig