Showing posts with label node load multiple. Show all posts
Showing posts with label node load multiple. 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.   

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