Showing posts with label template file. Show all posts
Showing posts with label template file. Show all posts

Thursday 24 January 2019

How to render template for custom module in drupal 8?

It is easy way to render/call template(.html.twig) file for custom module in drupal 8, But here concept is slightly different from drupal 7.
First we will go to the Controller file:
Like our controller name is ContactController.php so we will write there code like.
//Go to the ContactController.php file..
/**
 * @file
 * Contains \Drupal\contact\Controller\ContactController.
 */

namespace Drupal\contact\Controller;
use Drupal\node\Entity\Node;
use Drupal\Core\Controller\ControllerBase;


class Contactv4Controller extends ControllerBase {  
    public function indexv2() {
     
      $data = "<div>Testing</div>";
     
      return [
       '#theme'  => 'specific_contact',
       '#contact'   => $data,
    ];
  }
}

// Then go to the contact.module file.
/**
* Implements hook_theme() to add the template definition..
**/
function contact_theme($existing, $type, $theme, $path) {
  return array(
       'specific_contact' => [
        'variables' => [
            'contact' => NULL,
       ]
    ]
  );
}
 
// Now in the create template folder and the file with name of 'specific_contact.html.twig'.
And render the variable there like: <p> {{ contact }} </p>
And clear the cache after this.
Now Front end result will be 'Testing' on new template page.


Note: If have any suggestions or issue regarding 'How to render template for custom module in drupal 8?' then you can ask by comments.   

Monday 6 August 2018

Naming convention for node/content type template in drupal 8.

The naming convention of node/content type is very simple and we will explain by example here..

Example 1. If we have content type with name of 'customer story' so our this content type machine name will be 'customer_story' but we will take as a 'node--customer-story--full.html.twig' in naming convention.

Example 2. If we have content type with name of 'products' so our this content type machine name will be 'products' but we will take as a 'node--products--full.html.twig' in naming convention.

Example 3. If we have basic page single node like node id '58', So we will take as a 'node--58.html.twig' in naming convention.


Note: If have any suggestions or issue regarding 'Naming convention for node/content type template' then you can ask by comments.

Friday 3 August 2018

Naming convention for taxonomy template in drupal 8.

The naming convention of taxonomy is very simple and we will explain by..

Example 1. If we have taxonomy with name of 'product groups' so our this taxonomy machine name will be 'product_groups' but we will take as a 'taxonomy-term--product-groups.html.twig' in naming convention.

Example 2. If we have taxonomy with name of 'service support' so our this taxonomy machine name will be 'service_support' but we will take as a 'taxonomy-term--services-support.html.twig' in naming convention.


Note: If have any suggestions or issue regarding 'Naming convention for taxonomy template' then you can ask by comments.

Multiple images store in variable and render on twig file.

Yes, we have option for render multiple images on any template file. We just have to create variable in .theme file and collect in array and then print array value by variable.

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 == "applications"){  // we are going with example of applications 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

         $img_urls = array();
        foreach ($node_detail->field_image as $item) { // get the field image with multiple image url
          if ($item->entity) {
            $img_urls[] = $item->entity->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'    => $img_urls

        );


        /* Now pass the $arr in final productfields variable like this.. */

        $variables['productfields'] = $arr;  

        This is our final product fields variable now we have to move on applications type twig file because we are working for specially applications type of contents.

        Suppose are applications type twig file name is 'node--applications--full.html.twig'. That file is for applications detail pages.

        <ul>

        {% for key, value in productfields %}
        {% if key == 'image' %}
            <ul>
            {% for keys, imgvalue in value %}
                <li class="{{ keys }}"><img src="{{ imgvalue }}"></li>
            {% endfor %}
            </ul>
        {% endif %}
        {% endfor %}  

        </ul>

  /* here 'productfields' 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 'Multiple images store in variable and render on 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.

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