Creating a theme file

The *.theme file is a PHP file that contains theme hooks for preprocessing variables. We will create a theme file specific to our theme that we can use to grab the comment count, based on each individual post, and then return the count to our Twig template as a variable that can be printed.

Let's begin by creating a new file called octo.theme and saving it to our themes/octo folder.

Next, we will add the following PHP code:

<?php

function octo_preprocess_node(&$variables) {
  $node = $variables ['elements']['#node'];
  $id = $node->id();

  // Create comment count variable for template
  $count = _octo_comment_count($id);
  $variables['comment_count'] = _octo_plural($count, 'Comment',
  'Comments'),
}

The octo_preprocess_node(&$variables) function is known as a theme hook and is an adaptation of theme_preprocess_node. Within this function, we are passing by reference any variables accessible to the Node using &$variables. Since everything in Drupal is an array, we can traverse the $variables array to retrieve the Node ID, which we use to pass to a custom function that returns the number of comments for each node.

Next, we will add the two custom functions directly below our preprocess function:

function _octo_comment_count($id) {
  $count = db_query("SELECT comment_count FROM
  comment_entity_statistics WHERE entity_id = :id",
  array(':id' => $id))->fetchField();
  
  return empty($count) ? '0' : $count;
}
function _octo_plural($count, $singular, $plural) {
  if ( $count == 1 )
    return $count . ' ' . $singular;
  else
    return $count . ' ' . $plural;
}

Our first custom function returns the comment count for a specific node ID, which is passed to the function from our preprocess function. This custom function uses db_query to select the count from the comment_entity_statistics table in Drupal.

Our second custom function allows us to pluralize the count and return a more formatted count to our preprocess function, which we in turn will assign to our comment_count variable for use in our Twig template.

Once finished, make sure to save our file and clear Drupal's cache.

Printing our comment count

Now that we have utilized the theme layer to create a new variable containing the comment count for each node, we can print the variable to our template by following these steps:

  1. Open node--post--listing.html.twig.
  2. Add the following markup directly after our post-meta-tag section:
    <span class="post-meta-comments">
      <i class="fa fa-comments"></i>
      <a href="{{ url }}/#comments">{{ comment_count }}</a>
    </span>

Make sure to save the template, clear Drupal's cache, and then refresh the Blog listing page. Based on the markup we added, we now have our comment count displayed for each post:

Printing our comment count

Adding a read more link

We have almost completed the theming of our Post listing. We have one more component to add, and that is our read more link. We have all the elements we need to create this link, so let's start by following these steps:

  1. Open node--post--listing.html.twig.
  2. Add the following markup directly after our post-meta-comments section:
    <a href="{{ url }}" class="button button--primary button--xs pull-right">Read more...</a>

Make sure to save the template, clear Drupal's cache, and then refresh the Blog listing page. The main content area of our Post listing is now finished. It is time to move on to the sidebar and our three blocks of content, which contain Categories, Popular content, and our About block.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.212.102