Theming Drupal 8 - Loop through all posted articles and print specific section - drupal

I am trying to create my first Drupal 8 theme. On the frontpage, I want to list all articles with the title, summary, date posted, etc.
I more or less achieved this using views. Basically {{ page.content }} in twig, but found it to be really inflexible and I didn't really get the results that I wanted. What I want to do is just to iterate through all the articles and print each section of that article "manually". For instance:
{% for page in pages %}
{{ page.content.title }}
{{ page.content.datePosted }}
{{ page.content.body }}
{% endfor %}
So that I can have more control of what is happening and not making a lot of configuration in the views module when deploying. What is the best soltuion to achieve this? Thanks!

I recommend to use Views. Configure you view (filter by content type, etc.), you have a lot of parameters to achieve what you need.
Set the view to show "Content" and give it a view mode (you can use the default teaser, full, or create your own view mode). Then you can create a custom template for this :
node--xxxx--teaser.html.twig
In order to know how to name your custom template file, enable Twig Debugging in sites/default/services.yml. Set the debug variable to true. And clear cache. Then, you will see in source code the template name suggestion like this :
<!-- FILE NAME SUGGESTIONS:
* node--1--full.html.twig
* node--1.html.twig
x node--oeuvre--full.html.twig
* node--oeuvre.html.twig
* node--full.html.twig
* node.html.twig
-->
In your twig, you can do that kind of templating :
{{ content.my_field_image[0] }}
<h2>{{ node.title.value }}</h2>
<p>{{ content.body }}</p>

Related

Translate ACF field that renders in twig template

I'm working on a site where i in twig renders content that gets set via ACF-fields. Currently i'm implementing translations and was wondering how i would do it since it not really strings, its twig.
Php file:
$context = Timber::get_context();
$context['header'] = array(
'title' => get_field('header_title')
);
Timber::render('/templates/index.twig', $context);
My template looks like this.
<header>
{% if header.title %}
<h1>
{{ header.title }}
</h1>
{% endif %}
</header>
But for the translation tool (po files) want the syntax to be:
{{ __("string to translate") }}
So how can i instead pass in {{ header.title }} into that?
You can’t pass the header title into __(). Only static strings that are written in your code are handled with gettext functions like __(). They don’t work with variables. If you have strings from the database, you won’t use string translation functions. Instead, you need a multilingual solution for WordPress.
Read the Codex page about Multilingual WordPress to get started. You’ll probably want to use a plugin if you want to have content that is pulled from the database translated. Among the popular ones are:
MultilingualPress
WPML
Polylang

CollectionType custom prototype via form_theme

I want to customize the way an entry of a CollectionType in a form will be rendered, that is the way the prototype is generated, I need my own HTML. So I need to do it with the Twig form_theme, but it doesn't work.
I am currently using Symfony 4.1.2.
Here is what I have for now:
In the template that renders the whole form:
{% form_theme form.additionalEmails.vars.prototype 'form/additionalEmail.html.twig' %}
In the form/additionalEmail.html.twig file:
{% block form_row %}
<div style="margin: 10px 0">
{{ form_label(form) }}
{{ form_widget(form.children.value) }}
<div>Recevoir les mails de type : {{ form_row(form.children.isUsedForAdminCommChannel) }}{{ form_row(form.children.isUsedForInfosPratiquesCommChannel) }}{{ form_row(form.children.isUsedForAgendaCommChannel) }}</div>
</div>
{% endblock %}
If I do that I get this error:
Key "value" does not exist as the array is empty.
What is weird is that if I replace the above code by this (by commenting the old one and adding the new line):
{{ dump(form.children) }}
I see that form.children is an array with 4 items and one named "value" so it is clearly broken.
Your help would be much appreciated, as I'm just an amateur dev trying to make a website for a youth movement ^^
Best Regards,
[EDIT]
Here is the form that includes the CollectionType:
https://gist.github.com/MrPOC/a4af94cc5e577a1244dac59de5ea3506
The field is named "additionalEmails"
Just so I understand you correctly. You have a Form which contains a CollectionType and you want to change the prototype of that CollectionType?
If yes could you please post the Form with the CollectionType?
EDIT: Ok so here's how to overwrite the prototype template for a CollectionType.
First we have to find out what your forms unique_block_prefix is. You can find this by going to the page where this form is rendered, you then click in the symfony debug bar on the form icon. Then you have to expand the View variables block and look for unique_block_prefix.
I assume your block prefix could look like this _user_type the name of the block you would overwrite would then be _user_type_additionalEmails_entry_widget (for more details please read the link provided below)
You can replace the widget part with row or label depending on what you want to overwrite (I would suggest trying to overwrite them all to see what exactly changes because I don't know of the top of my head)
In twig you would then take your block prefix and overwrite the prototype field in the same way you would overwrite any other field
{% block '_user_type_additionalEmails_entry_widget' %}
//Content
{% endblock %}
What I wrote down here is what I found out after reading https://github.com/symfony/symfony-docs/issues/6056 (read HeahDude' comment specifically)
I hope this helps!

Is there way in Shopify to categorize "Collections" in lists?

Say I want to show 2 different collection lists(just the titles with links to their relative pages) on 2 different pages in Shopify; is there a way in Shopify that can be done? As far as I can see, either I show all "Collections" on a page or none at all. Is there no option to categorize them so that I can use liquid to only show certain collections?
The only other option to this I found is to hard code those collection menus on the page. But the issue with that is, any new collection I create, I will have to manually add it in to the code if I want to show it.
UPDATE
If you want to display all collections after a certain collection, once you find that handle, just have it parse the rest of them after it finds the handle.
Sorry my liquid is a little rusty so this is pseudo code.
{{ liquidtempvar = 0 }}
{% loop through collections %}
{% if thiscollection.handle == 'firstcollectioniwanttodisplay' %}
{{ liquidtempvar = 1 }}
{% endif %}
{% if liquidtempvar eq 1 %}
{{ loop through this collection and every collection after }}
{{ collection.product.handle }}
{{ endloop }}
{% endif %}
{{ endloop }}
Something like that...
END UPDATE
If you want to display a list of the items in a collection as links, then you would need to parse the collections object in the template of the page where you want to display them.
You would want to use the collection handle to qualify/filter which collections you wanted to display.
Using liquid loop over each collection, if the handle matches the collection you want, loop over the collection and display the links.
If you are looking to do this as a menu through the navigation, I think you can probably do it using query strings in the URL.
collection.next_product
and
collection.prev_product
will return the URL of the next and previous products in the collection.
Collection Navigation
Collection Object Reference

symfony2: index in individual form field for collection type

After hours of searching, I found a solution how to apply themes to individual fields for collection types here: symfony2: Applying theme to individual field for collection type (thanks #TroodoN-Mike).
So a custom theme for a collection type could be:
{% block _author_tags_entry_label %}
{# ... #}
{% endblock %}
The word "entry" replaces the row index, which works fine, but I need the Row index inside the custom block, something like {{ entry }}. Any hints?
Thanks
Chris
Ok, I found the solution. I wasn't aware of the fact, that you can pass custom variables to the form theme like this:
{{ form_row(form.field, {'customVariable': 'anyValue'}) }}

How to reference twig for custom field types in dedicated bundle?

I am (still) trying to introduce http://xoxco.com/clickable/jquery-tags-input into a dedicated bundle. As far, I have a type as a child of text and a data transformer that converts comma-separated strings into arrays of Objects and vice versa.
Now I want to decorate the text field with the JQuery code linked above. As far as I understand, I have to define a block like
{% block manytomanycomboselector_widget %}
{% spaceless %}
{{ block('text_widget') }}
<script>
$(function(){
$("#{{ id }}").tagsInput();
});
</script>
{% endspaceless %}
{% endblock manytomanycomboselector_widget %}
in [MyTypeBundle]Resources/views/Form/fields.html.twig
Now, both the documentation and the answers for this question at StackOverflow state that I have to reference fields.html.twig somewhere either in the template that uses the form or in app/, but this doesn't seem to be necessarily for other field-type bundles, though I cannot see in their code why.
What do I have to configure inside the bundle besides this block in this file?
Also I didn't get where I have to put the css and js requirements for the header and how I deal with general requirements like jQuery itself.
I have the same issue & I resolve it by merging my field template in the twig.form.resources parameter.
So, in the DI extension of my bundle (MyBundle/DependencyInjection/MyBundleExtension.php), I add:
$container->setParameter('twig.form.resources', array_merge(
array('MyBundle:Form:field_widget.html.twig'),
$container->getParameter('twig.form.resources')
));
Be aware, your bundle must be registered after the TwigBundle in your AppKernel.
EDIT:
A form field is not linked to any JS or CSS. So, IMO, you have 2 solutions.
Firstly, you directly wrap your JS & CSS in your field template and your bundle stays stand-alone.
Secondly, you instruct final users that they need to include manually some JSS & CSS each time they use your field type.
The IoFormBundle & GenemuFormBundle uses the second solution like explain in their documentation.

Resources