Showing posts with label node::load. Show all posts
Showing posts with label node::load. Show all posts

Thursday 8 August 2019

How to node load by name in drupal 8?

In drupal 8, we have easy way to node load by name/title. We can use "loadByProperties" function for node/user/file data.
here we are going to give an example with 3 type of entity.

Syntax 1:
$title = 'dummy';
//node load by name/title
$node_data = \Drupal::entityTypeManager()
        ->getStorage('node')
        ->loadByProperties(['title' => $title]); 
 OR

$result = \Drupal::entityQuery("node")
                ->condition('type', 'resources') // put your content type here
                ->condition('title', $title) // pass the title value or take any other condition
                ->execute();
               
Syntax 2:               
$email = 'dummymailid@gmail.com';
//load user by email id
$user_data = \Drupal::entityTypeManager()
           ->getStorage('user')
           ->loadByProperties(['mail'=> $email]); // here we can use any other property/field

          
Syntax 3:
$filr_uri = 'dummy file uri';          
//load file by uri
$file_data = \Drupal::entityTypeManager()
           ->getStorage('file')
           ->loadByProperties(['uri' => $file_uri]); // here we can use any other field


Note: How to node load by name in drupal 8?

How to delete content node programmatically in drupal 8?

Here we have simple way to 'delete content programmatically'. We can use this code in our "custom module/plugin" or  "hook_entity_predelete" or in "hook_entity_delete". Just use that simple code..

Syntax:

 $result = \Drupal::entityQuery("node")
                ->condition('type', 'resources');  // here change the content type of your
 $nids = $result->execute();

  if (!empty($nids)) {
    $nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
    $nodes->delete();
  }


#Extra notes: here we can add more conditions in 'entityQuery' as per our requirement.









Note: How to delete content node programmatically in drupal 8?

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

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