Showing posts with label d8. Show all posts
Showing posts with label d8. Show all posts

Sunday 28 June 2020

How to set the flood limit in Drupal 8?

Sometimes, we get the flood of Unlimited attempts of login fail message, block user, block IP. In that case our website handle unwanted extra data, extra burden on database that is not good for our website health.
So what we have to do, in Drupal 7, we have module that name is "Flood control" https://www.drupal.org/project/flood_control

But in Drupal 8 we don't have that module, So many times we get the error message that is very famous: "Login blocked after 5 failed login attempts".
In that case or if we want to overcome this problem Drupal 8 we have solution, we are going to share:

Code for Flood limit change:

$flood_limit = 5;                 //Default value is 5, change whatever you want. 
// for login form
\Drupal::configFactory()->getEditable('login.settings')
      ->set('flood.limit', $flood_limit)
      ->save();
// for contact form
$flood_limit = 5;                //Default value is 5, change whatever you want. 
\Drupal::configFactory()->getEditable('contact.settings')
      ->set('flood.limit', $flood_limit)
      ->save();   
  
Code for Flood interval change:

// for login form
$flood_interval = 3600;      //Default value is 3600sec (1hour), change whatever you want
 \Drupal::configFactory()->getEditable('login.settings')
      ->set('flood.interval', $flood_interval)
      ->save();
// for contact form
$flood_interval = 3600;     //Default value is 3600sec (1hour), change whatever you want
 \Drupal::configFactory()->getEditable('contact.settings')
      ->set('flood.interval', $flood_interval)
      ->save();   

  
Important Notice: Put this code in custom module or template or theme file.

Or we can use 'settings.php' option for this code
$flood_limit = 10;
$config['login.settings']['flood']['limit'] = $flood_limit; 

And another we have a module in drupal 8, we can use this and all this code feature is there. Please find this "Flood settings" module. https://www.drupal.org/project/flood_settings 
Note: If have any suggestions or issue regarding 'How to set the flood limit in Drupal 8?' then you can ask by comments.  

Thursday 30 August 2018

Programmatically Node load and Multiple Node load in drupal 8.

In drupal 7, we have node_load($nodeid) for load the node but now in drupal 8 syntax is changed and now node load by Node::load($nodeid) or Add the name space at the top of the page. \Drupal\node\Entity\Node::load($nodeid);

If we are using Symfony library and calling name space

use Drupal\node\Entity\Node;

So we do not need to use node class in node::load, we just need the syntax like Node::load($nodeid);

but if we are not calling or render name space then for node load we need to use the syntax like

\Drupal\node\Entity\Node::load($nodeid);

For get the node detail fields by Node::load($nodeid) syntax is..

Example: This example is for single node load.

              $node_detail = Node::load($nodeid);

                                   OR

              $node_detail = \Drupal\node\Entity\Node::load($nodeid);

              $node_id = $node_detail->id();

              $node_title = $node_detail->getTitle();

              $node_status = $node_detail->isPublished();

              $node_type = $node_detail->type->target_id; 

              $node_field_select_id = $node_detail->get('field_machine_name')->target_id;

              $node_field_select_value = $node_detail->get('field_machine_name')->value;

--------------------------------------------------------------------------------------------------------------------------

In drupal 8 for load multiple nodes of any `Content Type`, we have Node::loadMultiple() and Drupal::entityQuery() functions for load all the nodes of given content type. We have an Example with Syntax for this, please have a look.
Syntax:
$all_nids = \Drupal::entityQuery('node')->condition('type','write content type machine name ')->execute();

Then pass the $get_all_nids variable to Node::loadMultiple function for get data..

$nodes = \Drupal\node\Entity\Node::loadMultiple($get_all_nids);

Example:
    $nids = \Drupal::entityQuery('node')->condition('type','applications')->execute(); // There application is my content type machine name..
    $node_detail = \Drupal\node\Entity\Node::loadMultiple($nids);
    foreach ($node_detail as $node){  // In node_detail array we have all nodes details..
        // here we are fetching fields detail.
        $title = $node->getTitle();
        $application_category_id = $node->field_application_category->target_id;       
        $img = file_create_url($node->field_application_image->entity->getFileUri());
        $image_gallery .='<div class ="imggallery"><img src="'.$img.'"><p>'.$title.'</p></div>';
    }
    // here returning $image_gallery array with image_gallery detail.
    return array(
    '#markup' => t($image_gallery),
    );       

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

Thursday 9 August 2018

drupal 8 images with custom style.

If we want to apply the custom styling on image path then we have to create first with go to Configuration -> media -> image styles or /admin/config/media/image-styles
then click on Add image style and according to requirement choose effect option like Convert, Crop, Resize, Rotate, Scale, Scale and Crop, Desaturate and apply height/width and save it, then get the machine name of your custom create style and apply like this syntax.

Image style listing

Add new image style

Apply Effect


Call the ImageStyle library on header of file.
use Drupal\image\Entity\ImageStyle;

Then get the uri of image..

$path = 'public://images/image.jpg';
$url = ImageStyle::load('style_machine_name')->buildUrl($path);   

OR direct use this syntax..
$product_image = \Drupal\image\Entity\ImageStyle::load('related_products')->buildUrl($nodeload->field_banner_image->entity->getFileUri());   

here 'related_products' is the machine name of custom image style and 'field_banner_image' is the machine name of banner image, and this is $nodeload->field_banner_image->entity->getFileUri() the uri of banner image by node::load.

Note: If have any suggestions or issue regarding 'drupal 8 images with custom style.' then you can ask by comments.

Friday 20 July 2018

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.

Friday 29 June 2018

New features in Drupal 8.

I am going to start describe few new things in Drupal 8 in easy way.

Now in drupal 8 version uses Symfony few Components, rather than all of Symfony. If you want to check the list of use components in druapl 8. You can check in 'core/composer.json' file.

  1. We will render/call all variables of twig file in double Curly Braces/Brackets, Syntax..  {{ write variable name here }}   

      2. For comment in twig file we have to use # keyword, Syntax..  

          {#{ write variable name here }#}   or   {#  commenting syntax  #}

 Note: If have any suggestions or issue regarding 'New features 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...