I have form $form, which have text field for each entity User.
$form = $this->createFormBuilder();
$form->add('users', 'collection');
foreach ($query->getResult() as $user)
{
$form->get('users')->add($user->getName(), 'text', array('data' => $user->getName()));
}
I need to access specific input form by its ID number. So i have to be able to do something like this:
{{ form_widget(form.users[0]) }}
But it doesn't work. I can't use this:
{% for user in form.users %}
I need to access text fields specificly by their id's
Found the solution.
{% set interator = form.users.iterator %}
{{ interator.seek(0) }}
{{ form_widget(interator.current) }}
Related
I have a problem with properties in (probably) Twig. I have controller in Symfony where getCategories(), getWords(), getTranslations() methods (from Doctrine) return the objects (relations). Every property in the controller is an array because I call findAll() method (from Doctrine again) which returns the array. Finally I return all the properties from controller to view (Twig file) where I try display the results by Twig for loop.
The problem is the Twig loop only iterates over flashcards property (I know why ;)) and I have no idea how to make many-properties iterating. I'd like the loop to iterate over all properties returned by the controller.
In the controller foreach loop I tried update the flashcards array with new associative keys such as: category, word and translation so that all the results returned by Doctrine (including relations) are stored in one flashcards property but then Symfony throws exceptions.
I wondered if create one array in the controller to which I would push the flashcards, cateogry, word and translation arrays and then return this one array to the view but I don't think this is good practice.
Here's the controller method code:
public function showAllCards()
{
$flashcards = $this->getDoctrine()->getRepository(Flashcards::class)
->findAll();
foreach ($flashcards as $flashcard) {
$category = $flashcard->getCategories()->getName();
$word = $flashcard->getWords()->getWord();
$translation = $flashcard->getTranslations()->getWord();
}
return $this->render('try_me/index.html.twig', [
'flashcards' => $flashcards,
'category' => $category,
'word' => $word,
'translation' => $translation
]);
}
Here's the Twig loop code:
{% for flashcard in flashcards %}
{{ word }}
<br>
{{ flashcard.pronunciation }}
<br>
{{ flashcard.exampleSentence }}
<br>
{{ category }}
<br>
{{ translation }}
<br>
{% endfor %}
I tried to execute the following controller code...
public function showMeAll()
{
$flashcards = $this->getDoctrine()->getRepository(Flashcards::class)
->findAll();
foreach ($flashcards as $flashcard) {
$flashcards['categories'] = $flashcard->getCategories()->getName();
$flashcards['words'] = $flashcard->getWords()->getWord();
$flashcards['translations'] = $flashcard->getTranslations()->getWord();
}
return $this->render('try_me/index.html.twig', [
'flashcards' => $flashcards,
]);
}
...with the following Twig loop...
{% for flashcard in flashcards %}
{{ flashcard.words }}
<br>
{{ flashcard.pronunciation }}
<br>
{{ flashcard.exampleSentence }}
<br>
{{ flashcard.categories }}
<br>
{{ flashcard.translations }}
<br>
{% endfor %}
...but then Symfony says:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Proxies__CG__\App\Entity\Words could not be converted to string").
Could you give me some tips how to solve this problem, please? I'd like the Twig loop to iterates over many properties (flashcard, word, category, translation). Or write if there's a better solution, please.
Thank you in advance for every answer!
According to your snippets, I'm guessing you want something like the following:
{% for flashcard in flashcards %}
{% for word in flashcard.getWords() %}
{{ word }}<br />
{% endfor %}
{{ flashcard.getPronunciation() }}<br>
{{ flashcard.getExampleSentence() }}<br>
{% for category in flashcard.getCategories()() %}
{{ category.getName() }}<br />
{% endfor %}
{% for translation in flashcard.getTranslations() %}
{{ translation.getWord() }}<br />
{% endfor %}
{% endfor %}
Have a look at this section of the documentation. Basically if you had foo.bar, twig will test if bar is a public property of foo and if not test if there is a public getter, getBar, to fetch bar.
Some sidenotes in both of your loops, the values category, word and translation will only hold the last value of your flashcards, because you are overwriting the value each time.
I have been trying to figure out how I can pull the url of a node content type using an entity reference.
Apparently using {{ links.entity.uri }} or {{ links.entity.url }} does not work
<div>
{% for links in node.field_related_items %}
<h2>{{ links.entity.label }}</h2>
<a href="{{ links.entity.uri }} " ></a>
{% endfor %}
</div>
This worked for me:
This will only take you to /node/entity_id though and not /entity_name/entity_id
I would like to expand upon this answer by saying that you should add a Drupal::entityQuery() to your .theme file. Inside of this query, you'll want to grab the "alias" which is the link to the content. In your case this will give you these links /migrations and /aboutiom.
Here's an example of what that might look like...
function theme_name_preprocess_node(&$variables) {
$query = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'related_links')
$nids = $query->execute();
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
$related_links = [];
foreach ($nodes as $item) {
$alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/'.$item->id());
$related_links[] = [
'title' => $item->getTitle(),
'id' => $item->id(),
'field' => $item->get('field_name')->value,
'alias' => $alias,
];
}
$variables['related_links'] = $related_links;
}
Now in your twig template you can do something like this...
{% for link in related_links %}
{{ link['title'] }}
{% endfor %}
How could I achieve a form for each list item using csrf and validation in symfony way?
I have a Task entity, which has comments property with a relation OneToMany. So I want to list all tasks and include a hidden comment form for every task. Usually I pass generated forms from controller to template, but how to create them dinamically in template?
{% for task in tasks %}
<taskinfo>
<task comment form>
{% endfor %}
Solved using this way:
In controller:
$forms = array();
foreach($tasks as $task) {
$entity = new TaskComment();
$forms[$task -> getId()] = $this ->createTaskCommentForm($entity, $task -> getId());
}
return $this->render('Bundle:blah:index.html.twig', array(
....
'forms' => $forms
));
An then comment box near every Task box in view:
...task info...
{% for task in tasks %}
<div class="comment-box">
{{ form(forms[task.id]) }}
</div>
{% endfor %}
P.S. I'm using collapsible panels to show/hide each task.
Maybe you need to embed a collection of forms? If so, here and here you can read more.
I have a list of tasks stored in the doctrine database which I return like this:
$task = $this->getDoctrine()
->getRepository('SeotoolMainBundle:Tasks')
->findAll();
return array('form' => $form->createView(), 'message' => '', 'list_tasks' => $task);
In one row the ID of and entry from a second table is stored. I don't want only output the ID but also the Name, Description and so on stored in the other table. With normal PHP & MYSQL this would be done by using JOIN - how can I do it with Symfony & TWIG ?
Twig Output:
{% for task in list_tasks%}
{{ task.Id }}
{{ task.TaskTitle }}
{{ task.TaskDescription }}
{{ task.TaskTypes }} /* Here I want not only get the ID but also other fields stored in the database with TaskType ID = task.TaskTypes */
{{ task.User }} /* Here I want not only get the ID but also other fields stored in the database with User ID = task.User */
{% endfor %}
I am assuming that TaskType and User are entities, which belong within the Tasks entity. In which case, try the following. Note that I am getting just one task with id of 1 in my example:
$task = $this->getDoctrine()->getRepository('SeotoolMainBundle:Tasks')->find(1);
$taskTypes = $task->getTaskTypes();
$user = $task->getUser();
return array(
'form' => $form->createView(),
'message' => '',
'list_tasks' => $task,
'task_types' => $taskTypes,
'user' => $user,
);
And in your Twig:
{% for task_type in task_types %}
{{ task_type.Id }} // or whatever fields a task_type has
{% endfor %}
And the same for the user
Edit:
As you are wanting to have ALL tasks processed at once, I wonder if simply the following willl work:
{% for task_list in task_lists %}
{% for task_type in task_list.taskType %}
{{ task_type.Id }} // or whatever fields a task_type has
{% endfor %}
{% endfor %}
I did it now on this way, but I dont think, that it is 100% correct and conform.
How to do better? Luckily this works for me actually:
$task = $this->getDoctrine()
->getRepository('SeotoolMainBundle:Tasks')
->findAll();
$taskTypes = $this->getDoctrine()
->getRepository('SeotoolMainBundle:TaskTypes')
->findAll();
$user = $this->getDoctrine()
->getRepository('SeotoolMainBundle:User')
->findAll();
return array(
'form' => $form->createView(),
'message' => '',
'list_tasks' => $task,
'list_task_types' => $taskTypes,
'list_user' => $user
);
TWIG:
{% for task in list_tasks %}
Task ID: {{ task.ID }} <br/>
{% for type in list_task_types if type.id == task.tasktypes %}
{{ type.tasktypetitle }} <br/>
{% endfor %}
{% for user in list_user if user.id == task.user %}
{{ user.username }} <br/>
{% endfor %}
<hr/>
{% endfor %}
Should it work in Silex with symfony/twig-bridge ?
{{ render(controller('MyController')) }}
Now I have message like this:
Twig_Error_Syntax: The function "controller" does not exist in "...
You can use it this way:
{{ render(path('your_route_id', {'id': id, 'anotherParam': param})) }}
I've found this working:
{{ render(controller('services.controller:action', {[params]}) }}
And you can define the controller as a service:
$app['services.controller'] = function() use ($dependecy1, .., $dependencyN){
return new \\PathToYourControllerClass($dependecy1, .., $dependencyN);
}
I've found this working :
{{ render(controller('Full\\Namespace\\To\\Your\\Controller::listAction')) }}
please don't forget a double slash '\\'
Example:
{{ render(controller('Acme\\ProductController::listAction')) }}
In Your ProductController (I'm using Doctrine 2 in this example) :
public function listAction(Application $application)
{
$em = $application['orm.em'];
$produits = $em->getRepository('Acme\Entity\Produit')->findAll();
return $application['twig']->render('list.html.twig', array(
'products' => $products
));
}
Then in your list.html.twig
{% for product in products %}
<h2> {{ product.name }} </h2>
{% endfor %}