Showing posts with label drupal 8 components. Show all posts
Showing posts with label drupal 8 components. Show all posts

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

Vocabulary or Taxonomy load in Drupal 8.

If we have vocabulary/taxonomy name then we can easily load by loadTree, follow the syntax.

$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('write vocabulary machine name here');
foreach ($tree as $term) {
  echo $term->tid.' - '.$term->name;
}

Example:
$select_category .= '<select><option value="0">- Select -</option>';
$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('product_family'); //here product_family is machine name of Product Family taxonomy.
foreach ($tree as $term) {
$select_category .= '<option value="' . $parent . '_' . $term->tid . '">' . $term->name . '</option>';
}
$select_category .= '</select>';


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

Monday 6 August 2018

Create custom twig file and include in twig file.

For create custom twig file,
Example 1: We just have to create a file like 'instrument.html.twig' so for include this file simple code is..
     {% include('instrument.html.twig') %}

Example 2: We just have to create a file like 'consumable.html.twig' so for include this file simple code is..
     {% include('consumable.html.twig') %} 

Example 3: We just have to create a file like 'abc.html.twig' so for include this file simple code is..
     {% include('abc.html.twig') %}


Note: If have any suggestions or issue regarding 'include twig file' then you can ask by comments.

Thursday 2 August 2018

Create custom variable in drupal 8 and render on template file.

We have alternate option for custom variable in drupal 8. Now we will going to describe that how to create custom variable and use on any template file.
here we are going to giving an example with easy steps for custom variable use and render on file.
Example: First we have to take the step on hook_preprocess_node(&$variables). we have to check node value is available or not.
if(isset($variables['node'])){
    $node = $variables['node'];
    $current_node_id = $node->id(); //get the current node id
    $node_type = $node->type->target_id;  //get the current node type
    if($node_type == "products"){  // we are going with example of products content type nodes..
   
    /*  Now we will get the fields by node::load  */
   
    $categoryid = $node->get('field_category')->target_id; //here field_category is Category field machine_name..
        $category_detail = \Drupal\taxonomy\Entity\Term::load($categoryid);
        $variables['category_name'] = $category_detail->name->value;  // get the category name
        $variables['landing_page']  = $category_detail->field_setup_landing_page->value;  // get the field_setup_landing_page field value
       
        /* here 'category_name' and 'landing_page' is our variable keys */
       
        $node_detail = Node::load($current_node_id);                           

        $node_id = $node_detail->id();                   // get the node id
        $node_title = $node_detail->getTitle();          // get the node title
        $node_Published = $node_detail->isPublished();   // get the publish status
        $node_type = $node_detail->getType();            // get the node type
        $node_field_model_value = $node_detail->get('field_model')->value;   // get the field model value
        $node_field_price_value = $node_detail->get('field_price')->value;   // geth the field price value
        $node_field_image_value = $node_detail->get('field_image')->entity->uri->value; // get the field image with single image url

        /* Now put the all values in array with keys for differentiate */
        $arr = array('id'       => $node_id,
                     'title'    => $node_title,
                     'published'=> $node_Published,
                     'type'     => $node_type,
                     'model'    => $node_field_model_value,
                     'price'    => $node_field_price_value,
                     'image'    => $node_field_image_value
        );

        /* Now pass the $arr in final productfields variable like this.. */
        $variables['productfield'] = $arr;
       
        This is our final product fields variable now we have to move on products type twig file because we are working for specially products type of contents.
        Suppose are products type twig file name is 'node--products--full.html.twig'. That file is for products detail pages.
        <ul>
        {% for key, value in productfield %}
       
        <li>{{ key }} | {{ value }}</li>
       
        {% endfor %}   
        </ul>
       
        /* here 'productfield' is same variable name which we are created in 'hook_preprocess_node(&$variables)' hook in .theme file, so we just have to print the array keys and value by for loop in Symphony syntax, So that is the easy way to create custom variable and then render values in twig file. */
    }
       
}

Note: If have any suggestions or issue regarding 'Custom variable in drupal 8 and render on template file.' then you can ask by comments.

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.

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