Thursday 12 July 2018

Get the current logged in user detail.

We have the syntax of current logged-in user detail, Please have a look.

1. Logged-in user id :    $uid = \Drupal::currentUser()->id();
2. Current User Detail Array :     $current_user_detail = \Drupal::currentUser();
3. Current Logged-in User Role Array: $current_user_role = \Drupal::currentUser()->getRoles();     
If we have multiple roles for single user and we have to check one role is exist or not in roles array.
Syntax..
if(in_array("my_role", $current_user_role )){         
   \\ my role exist in this array..
}

4. Current User Default Language : $langcode = \Drupal::currentUser()->getPreferredLangcode();

 
Note: If have any suggestions or issue regarding 'Get the current logged in user detail' 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 AND /OR use in twig file.

Check AND /OR use in twig file, look at the syntax..

For OR Syntax..

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

For AND Syntax..
 
{% if content.field_machine_name|render and content.field_machine_name|render %}
        /* write comment here */
{% endif %} 

Note: If have any suggestions or issue regarding 'Check AND /OR use in twig file' 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.

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...