Drupal Code Development
How to access Drupal
8 views index fields? PUBLIC
First, we have to
create a views template:
The code snippet below shows the way to access, the Drupal 8
views index fields also to carry that out you will need to create a views
template. Using the code is easy and efficient as you can carry out your tasks,
as you need to within the prescribed limits. Making the appropriate use of the
base template definition is a start to the access of index fields in Drupal.
- [base template name]--[view machine name]--[view display
id].html.twig
- [base template name]--[view machine name]--[view display
type].html.twig (recommended)
- [base template name]--[view display type].html.twig
- [base template name]--[view display type]--[view machine
name]--[view display id].html.twig
- [base template name]--[view machine name].html.twig
- [base template name].html.twig
for instance if
views-view-unformatted--search_view--page_search_view.html.twig
(OK)
views-view--search_view--page_search_view.html.twig (OK and
recommended)
Coding in Drupal
Specific view displays are for unformatted ones customary to
show you the defined characteristics that go along with each view screened. An
ID of first row in your screened list checked shows you the ID as a start of
the view list labelling an exact one you want to find out.
php
// If starts with a specific views DISPLAY like:
"unformatted" see:
views-view-unformatted--search_view--page_search_view.html.twig
// Let's get nid of first row! (as long as ID (nid) exists
in views field UI.
// rows[0].content['#view'].style_plugin.render_tokens[
loop.index0 ]['{{ nid }}']|striptags|trim //< "34"
// rows[1].content['#view'].style_plugin.render_tokens[
loop.index0 ]['{{ nid }}']|striptags|trim //< "35"
// this is a real example of how code would look like:
<div class="search-page">
{% include
"@molecules/search-header.twig" with {
phrase:
view.args[0],
results:
view.total_rows,
browse_link:
'/browse',
}%}
<div
class="ga-row">
<div
class="ga-col ga-col-lg-8 ga-col-lg-centered ga-p-bottom-xxx-lg">
<div
class="search-results">
<div class="search-results__items">
{% for row in rows %}
{% set nid = row.content['#view'].style_plugin.render_tokens[
loop.index0 ]['{{ nid }}']|striptags|trim %}
{% set title =
row.content['#view'].style_plugin.render_tokens[ loop.index0 ]['{{ title }}']
%}
{% set body = row.content['#view'].style_plugin.render_tokens[
loop.index0 ]['{{ body }}'] %}
{% set changed =
row.content['#view'].style_plugin.render_tokens[ loop.index0 ]['{{ changed }}']
%}
{% include "@molecules/search-result-card.twig" with {
item: {
url: path('entity.node.canonical',
{'node': nid}),
title: title,
description:
body,
date: changed
}
}%}
{% endfor %}
</div>
</div>
</div>
</div>
<div
class="ga-row">
<div
class="ga-col ga-col-lg-centered">
{% if
pager %}
{{
pager }}
{% endif
%}
</div>
</div>
</div>
// Disadvantage: is that unfortunately when you have NO
RESULTS views do not recognize this template!
(keep in mind!)
/**
* The other solution
I recommend if you want to custom fields and no results is to use:
* [base template name]--[view machine
name]--[view display type].html.twig for
instance: views-view--search_view--page_search_view.html
*
rows[0]['#rows'][0]['#view'].style_plugin.render_tokens[1]['{{ nid
}}']|striptags|trim //< "34"
*
rows[0]['#rows'][0]['#view'].style_plugin.render_tokens[1]['{{ nid
}}']|striptags|trim //< "35"
* see example:
*/
Drupal Development Practice
Avail the phrase and result together with the browse link
using the Drupal web development company
Code snippet shown below making it simple and easy to attain all the
labelled particulars to use. Seek the exact location and retrieve information that
seems easy once you come into process with this code snippet.
<div class="search-page">
{% include
"@molecules/search-header.twig" with {
phrase:
view.args[0],
results:
view.total_rows,
browse_link:
'/browse',
}%}
<div
class="ga-row">
<div
class="ga-col ga-col-lg-8 ga-col-lg-centered ga-p-bottom-xxx-lg">
<div
class="search-results">
<div class="search-results__items">
{% for row
in rows[0]['#rows'] %}
{% set
nid = row['#view'].style_plugin.render_tokens[ loop.index0 ]['{{ nid
}}']|striptags|trim %}
{% set
title = row['#view'].style_plugin.render_tokens[ loop.index0 ]['{{ title }}']
%}
{% set
body = row['#view'].style_plugin.render_tokens[ loop.index0 ]['{{ body }}'] %}
{% set
changed = row['#view'].style_plugin.render_tokens[ loop.index0 ]['{{ changed
}}'] %}
{%
include "@molecules/search-result-card.twig" with {
item: {
url:
path('entity.node.canonical', {'node': nid}),
title:
title,
description: body,
date:
changed
}
}%}
{% endfor
%}
</div>
</div>
</div>
</div>
<div
class="ga-row">
<div
class="ga-col ga-col-lg-centered">
{% if pager %}
{{ pager }}
{% endif %}
</div>
</div>
</div>
// See how it is different is to access fields? It was very
tricky! However, with this there is permission for you to use it in custom with
NO RESULTS. Therefore, this is good to use.
Comments
Post a Comment