How to embed Comment on Custom Template in Drupal 8 Twig - drupal

I have a content type named Project and I created a template for it page--project.html.twig I am new with Drupal and I want to include a comment box and comment list on this page but not sure what to do. I already added a comment field on my content type. I tried rendering it using {{ node.field_comments }} but I am getting error. How can I include a comment on a

Because variable node is not available in template page so you have to get field comment, render it and then append it to your template by implementing hook_preprocess_page:
<your_theme>.theme
/**
* Implements hook_preprocess_HOOK().
*/
function <your_theme>_preprocess_page(array &$variables) {
$node = \Drupal::routeMatch()->getParameter('node');
if (!empty($node)) {
$variables['field_comment'] = $node->comment->view('full');
}
}
page--project.html.twig
{{ field_comment }}

Related

Drupal block twig file

I have a content type as "news". for this content type i need to show latest 5 articles. so i created block by using views. it is showing articles finely but i need to add my own css using twig files. i tried following options but none of them worked correctly.
block--newsblock.html.twig
block--newsblock-block.html.twig
views--view-newsblock-block.html.twig
But when i applied in following way , the field template is calling.
views-view-fields--newsblock--block.html.twig
What i want is for first element in block i need to show some teaser text for rest of them i need to show only title. how can i do it?
activate twig debug and look at your console
it will tell you suggestions of file names to use
Alternatively add this hook to your_theme_name.theme to proppose your own suggestion
/**
* Implements hook_theme_suggestions_HOOK_alter() for block templates.
*/
function your_theme_theme_suggestions_block_alter(array &$suggestions, array $variables) {
$block_id = $variables['elements']['#id'];
/* Uncomment the line below to see variables you can use to target a block */
// print $block_id . '<br/>';
/* Add classes based on the block id. */
switch ($block_id) {
case 'your_block_id':
$suggestions[] = 'block__newsblock';
break;
}
}
it will be then block--newsblock.html.twig

SilverStripe custom FormField_Holder

I've created a simple contact form within the page controller. For the front-end view of this contact form, I wish to use a customised FormField_Holder rather than the default one.
I've created a FormField_Holder.ss within themes/templates/Includes. How do I apply this template to my $ContactForm?
I've tried this already:
public function ContactForm() {
$form = Form::create(
...
);
foreach($form->Fields() as $field) {
$field->setFieldHolderTemplate('myHolder');
}
return $form;
}
I relocated the custom form template from
themes/mytheme/templates/Includes/
to
themes/mytheme/templates/forms/
..and it works now.
Edit: The official documentation mentions the following folder for form templates: mysite/templates/Includes but this oddly doesn't work oddly.
https://docs.silverstripe.org/en/3.4/developer_guides/forms/form_templates

Symfony project; SonataUser, FOSUser and their registration_content.html.twig

Solved -- see bottom of entry
I'm trying to get familiar with the SonataUserBundle extending the FOSUserBundle.
The installation worked fine (as far as I can tell) and now I want to customize
the login and registration forms.
I overwrote templates in app/Resources and it worked fine.
However, for the registration form I do not understand why it works...
Here's my problem:
The SonataUserBundle registration controller (RegistrationFOSUser1) sets up the form
and renders it with FOSUserBundle:Registration:register.html.twig as template:
$form = $this->container->get('sonata.user.registration.form');
$formHandler = $this->container->get('sonata.user.registration.form.handler');
[...]
return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
'form' => $form->createView(),
));
register.html.twig includes FOSUserBundle:Registration:register_content.html.twig:
{% block fos_user_content %}
{% include "FOSUserBundle:Registration:register_content.html.twig" %}
{% endblock fos_user_content %}
register_content.html.twig contains the twig code to render the form.
However, what is actually rendered is SonataUserBundle:Registration:register_content.html.twig
I just can't figure out where, when and how SonataUserBundle substitutes FOSUserBundle here...
Thanks for any hints!
Ok, I now see that the solution to my question is well documented in the symfony cookbook:
http://symfony.com/doc/current/cookbook/bundles/inheritance.html
For those as new to symfony as myself:
If you define a parent 'ParentBundle' for another bundle 'ChildBundle', then everytime a function, template etc. from ParentBundle is called, symfony will first look whether there is a file with the same name in ChildBundle.
The parent bundle is defined in the ChildBundle.php:
public function getParent()
{
return 'ParentBundle';
}
This works, as long as the file of the parent bundle is called via the usual ParentBundle:path:file notation.
Ok, I now see that the solution to my question is well documented in the symfony cookbook: http://symfony.com/doc/current/cookbook/bundles/inheritance.html
For those as new to symfony as myself:
If you define a parent 'ParentBundle' for another bundle 'ChildBundle', then everytime a function, template etc. from ParentBundle is called, symfony will first look whether there is a file with the same name in ChildBundle.
The parent bundle is defined in the ChildBundle.php:
public function getParent()
{
return 'ParentBundle';
}
This works, as long as the file of the parent bundle is called via the usual ParentBundle:path:file notation.

transfer data in symfony2

I have created two functions in controller of Symfony as follow:
first is newAction
public function newAction()
{
return $this->render('AcmeTaskBundle:Default:index.html.twig');
}
then subAction
public function subAction()
{
echo "hello";
}
I want to use some data from index.html.twig into subAction function.
How I can do that?
All you need is to use
$content = $this->renderView('AcmeTaskBundle:Default:index.html.twig')
This will render contents of template in variable
http://symfony.com/doc/current/book/controller.html#rendering-templates
EDIT according to comment
If you need to render only part of template - then you should refactor your templates.
Exclude that part of code from your index.html.twig into separate template file and include it in index.html.twig:
...
{% include 'AcmeTaskBundle:Default:subpage.html.twig' %}
...
And then in your subAction() call:
$content = $this->renderView('AcmeTaskBundle:Default:subpage.html.twig')

Get current URL in Twig template?

I looked around for the code to get the current path in a Twig template (and not the full URL), i.e.
I don't want http://www.sitename.com/page, I only need /page.
{{ path(app.request.attributes.get('_route'),
app.request.attributes.get('_route_params')) }}
If you want to read it into a view variable:
{% set currentPath = path(app.request.attributes.get('_route'),
app.request.attributes.get('_route_params')) %}
The app global view variable contains all sorts of useful shortcuts, such as app.session and app.security.token.user, that reference the services you might use in a controller.
Get current url: {{ app.request.uri }} in Symfony 2.3, 3, 4, 5
Get path only: {{ app.request.pathinfo }} (without parameters)
Get request uri: {{ app.request.requesturi }} (with parameters)
In symfony 2.1 you can use this:
{{ path(app.request.attributes.get('_route'),
app.request.attributes.get('_route_params')) }}
In symfony 2.0, one solution is to write a twig extension for this
public function getFunctions()
{
return array(
'my_router_params' => new \Twig_Function_Method($this, 'routerParams'),
);
}
/**
* Emulating the symfony 2.1.x $request->attributes->get('_route_params') feature.
* Code based on PagerfantaBundle's twig extension.
*/
public function routerParams()
{
$router = $this->container->get('router');
$request = $this->container->get('request');
$routeName = $request->attributes->get('_route');
$routeParams = $request->query->all();
foreach ($router->getRouteCollection()->get($routeName)->compile()->getVariables() as $variable) {
$routeParams[$variable] = $request->attributes->get($variable);
}
return $routeParams;
}
And use like this
{{ path(app.request.attributes.get('_route'), my_router_params()|merge({'additional': 'value'}) }}
You won't need all this unless you want to add additional parameters to your links, like in a pager, or you want to change one of the parameters.
You can get the current URL in Twig like this:
{{ app.request.schemeAndHttpHost ~ app.request.requestUri }}
It should be noted that if you have additional query parameters in your URL, which are not part of the configured route, the accepted answer will not include them in the current URL (path).
Why would you want extra parameters?
For example, if you have a list page with records that can be filtered by keyword and the page has pagination, most likely the query variables for "keyword" and "page" will not be in your route. But in your forward and back buttons for paging, you need the full current URL (that contains the keywords so the next page is still filtered). And you need to modify the page variable.
How to Merge In Extra Query Parameters
So you can get the current route, and merge in the extra variables (after modifying one or more of those extra variables). Note that you are merging in your own variables to the app.request.query.all, and then merging that array into the app.request.attributes.get('_route_params'). The path() method requires that you provide all the required parameters of the route, which is why you need to include the _route_params.
{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge(app.request.query.all|merge({'page': 2 }))) }}
That's really ugly, but if you are developing pagination, you will need to modify the page variable on each separate link, so you have to include the whole thing each time. Perhaps others have a better solution.
Using Symfony 5 you can use this:
{{ app.request.uri }}
If you are using Silex 2 you can not access the Request object anymore.
You can access the current request attributes this way.
app.request_stack.currentrequest.attributes.get('_route')
And to generate the full current URL :
path(app.request_stack.currentrequest.attributes.get('_route'), app.request_stack.currentrequest.attributes.get('_route_params'))

Resources