Showing posts with label variable. Show all posts
Showing posts with label variable. Show all posts

Monday 12 August 2019

How to replace function use in Twig template?

It's a simple way to use "Replace" function in twig file just go through simple method:

Syntax with example:                   

{#
*** replace '-' with '$' sign in vidyard ID ***
#}

{% set twig_content_variable = node.field_resources_vidyard_id.value %}    //field_resources_vidyard_id is are vidyard id field
{% set replace_value_var = '-' %}
{% set replace_with_value_var = '$' %}
{% if twig_content_variable %}
{% set vidyard_function_id = twig_content_variable|replace({ '-': '$' }) %}
{% endif %}







Note: How to replace function use in Twig template?

Friday 29 March 2019

How to Check the Condition in Twig Template?

It's easy process to check the condition of our 'id' exist or not for particular variable render time in Twig template.
Like we have to check any "Product type" exist or not and then variable call there, So we are giving an example:
Example 1:
{% if node.field_product_type.value not in ['0', '2', '4', '5', '7'] %}
 /*** print variable here ***/
{% endif %}

Description: Here our condition is, if Product type of node value is not in these id's ['0', '2', '4', '5', '7'], then our variable will show the result otherwise not.

Example 2:
{% set node_id = node.id %}
{% if node_id in ['216','217','9','1258'] %} 
 /*** print variable here ***/
{% endif %}

Description: Here our condition is, if node id of node is in these id's ['216','217','9','1258'], then our variable will show the result otherwise not.


Note: If have any suggestions or issue regarding 'How to Check the Condition in Twig Template?' then you can ask by comments.

Tuesday 4 September 2018

Replace any string or value in twig template variable.

If we want to Replace the value in twig template then we have an example and syntax, please have a look..
{#
    ****  replace '-' with '$' sign in vidyard ID ****  
#}

{% set twig_content_variable  = node.field_resources_vidyard_id.value  %}   // here we are applying on field_resources_vidyard_id field..
{% set replace_value_var      = '-' %}
{% set replace_with_value_var = '$' %}
{%if twig_content_variable %}
   {% set vidyard_function_id = twig_content_variable|replace({ '-': '$' }) %}
{% endif %}


Note: If have any suggestions or issue regarding 'Replace any string or value in twig template variable.' then you can ask by comments. 

Friday 6 July 2018

Render view fields in custom view template in drupal 8.

First we have to create view according to our requirement and then create the custom view template file, suppose our view machine name is 'customer_story' and on 'block_1', so our template file name will be 'views-view-fields--customer-story--block-1.html.twig'.
and we can render our views fields variables like..

{{ fields.title.content }} //for title field
{{ fields.field_machine_name.content }} //for any other field render.
{{ file_url(fields.field_landing_page_image.content) }} //for image field render
{{ fields.field_page_url.content }}  //for link field render

you can get these variable name by 'custom text field' "Replacement pattern" settings.

Note: If have any suggestions or issue regarding 'Render view fields in custom view template in drupal 8' then you can ask by comments.

Thursday 5 July 2018

Loop in twig file drupal 8.

Here we have syntax of for/foreach loop in Twig file. Please have a look the syntax..
Suppose we have array with name of 'data', so our for loop for twig will be.

{% for key, value in data %}
      {{ key }} | {{ value }}
{% endfor %}

                      OR

In case we need key with value then our syntax will be..

<ul>
 {% for key, value in data %}
   {% if key!= ' '%}

      <li class="{{ key }}"> {{ value }}</li>
    {% endif %}
{% endfor %}
</ul>

                       OR

In case we just need value without key then our syntax will be..

{% for value in data %}
   {{ value }}     
{% endfor %}

Note: If have any suggestions or issue regarding 'Loop in twig file drupal 8' then you can ask by comments.

Wednesday 4 July 2018

Drupal 8 base_path and base_path_url.

We have syntax change in drupal 8 for base_path and base_path_url, Please have a look..

  • base_path = base_path();  // This code is for base_url of site.                                                    
  • base_path_url = \Drupal::request()->getpathInfo();  // This code for current page path.
  • Front page = {{ url('<front>') }}  // This code is for front page url.
  • Theme directory path = url('{{ base_path ~ directory }} //This code is for theme's root directory
Note: If have any suggestions or issue regarding 'Drupal 8 base_path and base_path_url' then you can ask by comments.

Check twig file field is empty or not.

For check Twig file field is empty or not, look at the syntax..
 
{% if content.field_machine_name[0] is not empty %}
           /* write comment here */
{% else %}
          /* write comment here */
{% endif %}

                                 OR

{% if content.field_machine_name|render %}
       /* write comment here */
{% endif %}


Note: If have any suggestions or issue regarding 'Check twig file field is empty or not' then you can ask by comments.

Tuesday 3 July 2018

Break line in between variables of twig file.

We can break the lines in between variables in twig file with add div syntax, please have a look.

<div>{{ content.field_machine_name }}</div>   //for insert line break 

 Note: If have any suggestions or issue regarding 'Break line in between variables of twig file' then you can ask by comments.

Friday 29 June 2018

Twig file feature in Drupal 8.

We use '.tpl.php' extension for druapl 7 but now in case of drupal 8 extension is '.html.twig'.
Lets talk about twig file..
  1. For node id in twig file syntax..  {{ node.id }}
  2. For node title in twig file syntax.. {{ node.label }}
  3. For node body in twig file syntax.. {{ node.body }} 
  4. For node any field.. {{ content.field_machine_name[0] }} or {{ content.field_machine_name.0 }}
if else syntax..

{% if ... %}
            /* write comment here */
{% else %}
           /* write comment here */
{% endif %}


For create variable in twig file syntax..
 
{% set variable name = "variable value" %}  then write {{  variable name }} and use it easily.

 Note: If have any suggestions or issue regarding 'Twig file feature in Drupal 8' then you can ask by comments.

How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error: Steps:   You can put t...