Showing posts with label drupal entity. Show all posts
Showing posts with label drupal entity. 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?

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.

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