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.
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.
Hi,
ReplyDeleteCan you plz explain how to get the multiple images in twig file by drupal variable?
Hi,
ReplyDeletehere is article for multiple images render in template file.
https://drupalwebsolutions.blogspot.com/2018/08/multiple-images-store-in-variable-and.html