1

I know there is a post on how to set a view header/footer value here, but how do I set a view header/footer without having to create it through views UI first?

The reason why I want to implement a view header programmatically is because (a) I want to write php code in it, by leveraging the Drupal's API, something that is not so convenient in a textarea field, like the one that the views UI provides -and any other textarea field- plus the fact that some functions may never get invoked and (b) because I want to conditionally set/unset the view header/footer.

6

After some research I managed to answer my own question by implementing the hook_views_pre_view in my custom module. The method set_item is used to set the configuration of an item (field/sort/filter/etc) on a given display. The $print variable bares the content to be rendered in the view header, it is just an example and it can be replaced by anything. Apparently now I can take advantage of the Drupal API or even set/unset any view item conditionally.

/**
 * Implementation of hook_views_pre_view.
 */
function my_module_views_pre_view(&$view, &$display_id, &$args) {

  $path = $_GET['q'];
  // Construct the print link.
  $print = l(t('Display a printer-friendly version of this page'), '/print/' . $path . '/print-friendly',  array(
    'attributes' => array(
      'class' => array('print-friendly'),
      'target' => '_blank',
      'title'=> t('Display a printer-friendly version of this page.'),
      ),
    )
  );

  if ($view->name == 'my_view_name' && $display_id == 'my_view_display') {
    // View's header declaration.
    $item = array(
      'id' => 'area',
      'table' => 'views',
      'field' => 'area',
      'relationship' => 'none',
      'group_type' => 'group',
      'ui_name' => '', 
      'label' => '',
      'empty' => 0,
      'content' => $print,
      'format' => 'filtered_html',
      'tokenize' => 0,
    );
    // Render the header.
    $view->set_item($display_id, 'header', 'area', $item);
  }
}
  • Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - From Review – Shawn Conn Nov 25 '16 at 19:20
  • @ShawnConn I really appreciate your words but I don't understand what do you mean by complete answer, can you guide me please? Did you try that code snippet and didn't work ? – mchar Nov 25 '16 at 20:40
  • I take @ShawnConn is saying it's not clear this is the answer for the question. It seems more additional information for the question. Maybe you should remove the part telling why you want to do it programmatically and not using the UI; that part is for the question. – kiamlaluno Nov 25 '16 at 21:12
  • @kiamlaluno Thanks for the clarification. I hope that is more clear now, but if someone still thinks that the answer is incomplete, ambiguous or inappropriate kindly let me know. I just am trying to share my findings. – mchar Nov 25 '16 at 21:40
  • @mchar So-called "code only" answers aren't generally considered to be great for Q+A. There's no explanation of how you solved the problem, just a dump of code that purports to do so – Clive Nov 25 '16 at 22:23
1

Just as a second option, you could always use a preprocess function to override the theme hook.

For starters, turn on theme_debug in your settings.php file, clear your site cache and navigate to the page that contains the view display that you'd like to alter.

Open your browser's developer tools (e.g. Chrome: Ctrl+Shift+I, or right click and choose "Inspect") and find the display's markup within the source code. You should see something similar directly above the element.

Notice the output provided by theme_debug. Specifically, the "Call: theme('views_view')"

Next, inside template.php of your theme, write a preprocess function that calls the same theme hook:

function THEMENAME_preprocess_views_view(&$vars) {
    //logic
}

The $vars variable is an array that contains the view object. Use a debug tool like devel to introspect the render array. Then set the header and footer attribute by assigning it a value.

/**
 * Implements template_preprocess_views_view().
 */
function THEMENAME_preprocess_views_view(&$vars) {
    //logic
    $vars['header'] = "Something Special";
}

Source: template_preprocess_views_view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.