Monday 23 July 2018

Drupal entity render on twig template.

We have few short codes or cheat codes for render views or blocks, entity, menu, field, form on twig template. This will help to publish all these things on twig file in easy way. we have some examples for easy to use these short code, Please have a look,
First thing most important is that we have to install 'Twig tweak' module on site that is required for render views, block and entity on twig file.
link for twig tweak module is https://www.drupal.org/project/twig_tweak

Render View on twig file:
{{ drupal_view('view_machine_name', 'block or view machine_name') }}
example:-  {{ drupal_view('application_resources', 'block_10') }}  

Render Block on twig file: 
{{ drupal_block('block_machine_name', wrapper=false) }}
example:-  {{ drupal_block('bartik_breadcrumbs') }}

Render Region on twig file: 
{{ drupal_region('sidebar_first') }}

Render Entity on twig file:
{{ drupal_entity('block_content', 1) }}

Render entity edit form:
{{ drupal_entity_form('node', 1) }}
/* here 1 is node id.*/

Render entity add form:
{{ drupal_entity_form('node', values={type: 'page'}) }}

Render single entity field:
{{ drupal_field('field_image', 'node', 1) }}
/* here field_image is from node 1 */

Render menu on twig file:
{{ drupal_menu('main') }}  or {{ drupal_menu('footer') }}
/* here main and footer parameter is the name of menu machine_name. */

Render any form on twig file:
{{ drupal_form('Drupal\\search\\Form\\SearchBlockForm') }}
/* here we are calling search block form. */



Note: If have any suggestions or issue regarding 'Drupal entity render on twig template' then you can ask by comments.

Friday 20 July 2018

Programmatically render Block in a twig template drupal 8.

In drupal 7 syntax of render block in twig template is.

$block = module_invoke('module_name', 'block_view', 'block_delta');
print render($block['content']);

But now in drupal 8 syntax is changed, but first we have to install "Twig tweak" module that provides
a twig extension with useful functions an filters that can improve development experience.
here is the link for twig tweak module, please download from here..
https://www.drupal.org/project/twig_tweak
there will be cheat sheet for drupal 8 that helps in calling/render block or views in twig template file.

Drupal_view render..

Suppose we have to render/call view in twig template then syntax will be
{{ drupal_view('view_machine_name', 'block/page machine name') }}
Example:
{{ drupal_view('product_overview_video', 'block_2') }} 
here 'product_overview_video' is view machine name and 'block_2' is we created 2nd block on this view.

Drupal_block render..
Suppose we have to render/call block in existing block then our syntax will be
{{ drupal_block('block_machine_name', wrapper=false) }}
 Example:
{{ drupal_entity('block_content', 27) }}
here block_content is block machine name and 27 is block id.
OR
 {{ drupal_block('bartik_breadcrumbs') }}
we can direct call block by machine_name like breadcrumbs block. we just have to put the drupal_block function with machine_name and clear the cache and it will work.

Note: If have any suggestions or issue regarding 'Programmatically render Block in a twig template' then you can ask by comments.

Taxonomy terms load in drupal 8.

In drupal 7, we have taxonomy_term_load($tid), taxonomy_term_load_multiple($tids) functions for load the taxonomy term or multiple terms but now in drupal 8 syntax is changed and now Single/Multiple term load by Term::load($tid)
or
Add the name space at the top of the page.
\Drupal\taxonomy\Entity\Term::load($tid);

If we are using Symfony library and calling name space 
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy;

or direct use in function calling time like  \Drupal\taxonomy\Entity\Term::load($tid);

Example:
$term_detail_array = \Drupal\taxonomy\Entity\Term::load($tid);
or
$term_detail_array = Term::load($tid); //if we are already using taxonomy library..

Then get the fields detail by $term_detail_array like,

Term id:  $term_detail_array ->tid->value;
Term name:  $term_detail_array ->name->value;
Term manually added field:  $term_detail_array ->get('field_machine_name')->value;  // manually added field value..
Term image field:  $term_detail_array ->field_machine_name->entity->getFileUri();

 If we have field with entity reference then field value will be in form of $term_detail_array ->get('field_machine_name')->target_id;
or
If we have field without entity reference then field value will be in form of $term_detail_array ->get('field_machine_name')->value;

Now we will discuss on Term::loadMultiple.
And the syntax is $terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids); // here $tids is the array of multiple tid..
$terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids); 
foreach ($terms as $term) {  //then the fields value will be..
$term_id = $term->tid->value;
$term_name = $term->name->value;
$term_file field = $term->field_machine_name->uri;
}
and so on..

Note: If have any suggestions or issue regarding 'Taxonomy terms load in drupal 8' then you can ask by comments.

Thursday 19 July 2018

User load by mail in drupal 8.

Yes, we can load the user detail by mail id in drupal 8. As per drupal syntax first we have to add user library for classes then we will user drupal load by mail function that is "user_load_by_mail()".

$check_user = user_load_by_mail($mailid);
 if (!empty($check_user)) {
     $uid = $check_user->id();
     $user_detail = \Drupal\user\Entity\User::load($uid);
                $user_id= $user_detail->get('uid')->value;
                $user_roles = $user_detail->getRoles();
                $user_status = $user_detail->isActive();
                $user_name = $user_detail->getUsername();
                $user_body = $user_detail->get('body')->value;               
                $user_email = $user_detail->get('mail')->value;
                $user_created_time = $user_detail->created->value;
                $user_field = $user_detail->get('field_machine_name')->value;  
                $user_loggedin_check = $user_detail ->login->value; // that will return 1 or 0
                if ($user_detail->login->value != 0) {
                    $first_login_data = date('Y-m-d H:i:s', $user_detail->login->value);
                } else {
                    $first_login_data = 'never';
                }
  }

Note: If have any suggestions or issue regarding 'User load by mail in drupal 8' then you can ask by comments.

Programmatically User load and User load Multiple in drupal 8.

In drupal 8 User::load, we have to use library and calling name spaceuse Drupal\user\Entity\User;

So same as node::load, we can use User::load($userid);
or
we can direct call \Drupal\user\Entity\User::load($userid);

For get the user detail fields by User::load($userid) syntax is..

Example: This example is for single user load.
                $user_detail = User::load($userid);       
                                    OR
                $user_detail = \Drupal\user\Entity\User::load($userid);
           
                $user_id= $user_detail->get('uid')->value;
                $user_roles = $user->getRoles();
                $user_status = $user_detail->isActive();
                $user_name = $user_detail->getUsername();
                $user_body = $user_detail->get('body')->value;               
                $user_email = $user_detail->get('mail')->value;
                $user_created_time = $user_detail->created->value;
                $user_field = $user_detail->get('field_machine_name')->value;  

Example: This example is for multiple user load.
First you have to use user library and calling name space
use  \Drupal\user\Entity\User;

                $users = User::loadMultiple($ids);
                foreach($users as $u) {
                   $users_mailid= $u->getEmail();
                   $user_id= $u->get('uid')->value;
                   $user_name = $u->getUsername();
                   $field_value = $u->get('field_machine_name')->value;
                 } 

Note: If have any suggestions or issue regarding 'Programmatically User load and User load Multiple in drupal 8.' then you can ask by comments.

Monday 16 July 2018

Page Redirection programmatically in drupal 8.

In drupal 8 for page redirection, we have to use Symfony components class. Here page redirection is done by 'RedirectResponse' class from Symfony HttpFoundation. In drupal 7 we using drupal_goto() function for redirect.
But in case of drupal 8, RedirectResponse' class is..

use Symfony\Component\HttpFoundation\RedirectResponse;

and suppose redirection page is node 2691 so we will give redirect url is '/node/2691'.
Example:
$url = '/node/2691';
return new RedirectResponse($url);

               OR

$redirect_variable = new Symfony\Component\HttpFoundation\RedirectResponse('/node/2691');     $redirect_variable ->send();
 return;

     For front page redirection, we can use..

return new \Symfony\Component\HttpFoundation\RedirectResponse(\Drupal::url('<front>'));

             OR
 In Function we can use redirect..
 public function submitForm(array &$form, FormStateInterface $form_state) {
   $form_state->setRedirect('redirect router name');
Example:
  $form_state->setRedirect('account_login.content');
 }

Note: If have any suggestions or issue regarding 'Page Redirection programmatically in drupal 8' then you can ask by comments.

Thursday 12 July 2018

Custom login create in drupal 8.

We are trying to clear the code of custom login with authentication. here we have the code.

 // Authentication of username and password..
$uid = \Drupal::service('user.auth')->authenticate($username,$password);
// Get the user id by database and load..
$user = \Drupal\user\Entity\User::load($uid);
// finally pass the user detail array in user login function..
 user_login_finalize($user);

Example:
 Like we have username and password by POST method and fields are.
<input id="login-username" type="text" class="form-control" name="name" value="" placeholder="username">
<input id="login-password" type="password" class="form-control" name="pass" placeholder="password">

so for got these credentials in function the code is.
$username = \Drupal::request()->request->get('name'); // form username
$password = \Drupal::request()->request->get('pass'); // form password

And the final code for login..

 $uid = \Drupal::service('user.auth')->authenticate($username,$password);
 $user = \Drupal\user\Entity\User::load($uid);
 user_login_finalize($user);
 
Note: If have any suggestions or issue regarding 'Custom login create in drupal 8' then you can ask by comments.

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