[Drupal] How to add custom content in views header or footer?

| | 1 min read

While working on a report generating system, I wanted to display footer value that should reflect based on the date filter, added in the report. I thought of creating a block in the content region and manage the contents in the block. But when I went to know about header and footer options in views module, I decided to go for it. This article is on how to use header or footer in views pages.

  1. In views edit page, the center column has option to add header and footer. In this select Global: PHP.

    image1.png

  2. Select add footer and in "Output code" we can manually add php code in this box.
  3. But, it is not a good method to allow Drupal to save php code in database. So we can write a code to include a file having php script to minimize the PHP content in DB.

    image2.png

    NOTE: Saving the code in database or writing it in .inc file varies on requirement.

  4. While including a file or manually enters the code, it offers three variables.
    1. $view (The view object)
    2. $handler (The handler object)
    3. $results (Array containing the view's result.)
  5. We can do the manipulations with the data and "echo" the content we needed to show in header or footer.

    image3.png

By using the above method you can easily add header and footer in views. Style the header or footer the way you need. Try this...