3

I update my views from views 2 and I do the same on embedded code. But actually I can't set footer content as it changes in views 3.

My views 2 code :

$emptyTextVue = $view->display_handler->get_option('empty');
$emptyText = $view->display_handler->set_option('empty','<div class="ma-classe">Pas images.</div>');

I think I do use the export code like :

$handler->display->display_options['empty']['area']['content'] = 'Pas d\'images à afficher.';

but how ?

EDIT : extract result for var_dump() : ["handler_type"]=> string(5) "empty" ["options"]=> array(11) { ["id"]=> string(4) "area" ["table"]=> string(5) "views" ["field"]=> string(4) "area" ["relationship"]=> string(4) "none" ["group_type"]=> string(5) "group" ["ui_name"]=> string(0) "" ["label"]=> string(13) "Zone de texte" ["empty"]=> int(0) ["content"]=> string(25) "Pas d'images à afficher." ["format"]=> string(1) "2" ["tokenize"]=> int(0) } ["definition"]=> array(4) { ["handler"]=> string(23) "views_handler_area_text" ["group"]=> string(6) "Global" ["title"]=> string(13) "Zone de texte" ["help"]=> string(33) "Provide markup text for the area." } ["is_handler"]=> bool(true) ["localization_keys"]=> array(4) { [0]=> string(7) "default" [1]=> string(5) "empty" [2]=> string(5) "views" [3]=> string(4) "area" } ["table"]=>

5
  • Why views_embed_view?
    – Alex Gill
    Commented Oct 30, 2012 at 9:57
  • because i put the code into a custom node.tpl; but for now empty text from views isn't display too..i think code has changed in V3...but i can't find a list of that snippets.actually the view display "Array" as empty text..
    – user8753
    Commented Oct 30, 2012 at 10:26
  • See below for snippet...
    – Alex Gill
    Commented Oct 30, 2012 at 10:37
  • Do you have an empty text field added to your view via the UI and you just want to set the text, or there is no empty text handler on the view and you want to add it in the code?
    – rooby
    Commented Apr 21, 2013 at 0:50
  • it's the second option, no handler and i want ot add it via code
    – user8753
    Commented Apr 22, 2013 at 12:24

2 Answers 2

7

First up, don't use views_embed_view(). It is a very simple function that doesn't allow for customisation.

Do something like this:

// Load the view.
$view = views_get_view('VIEW_NAME');
// Set the options for the header text.
// To get an idea of what options are available, set up a view with header text
// and then use the devel module to print out the view definition.
// For example, add a header text area field and print it with
// dpm($view->header['area']); after you have loaded it with views_get_view().
$header_options = array(
  'label' => t('Header text'), // Administrative label. Not really needed here.
  'content' => 'This is the header text content.', // Content of header text.
  'format' => 'filtered_html', // Format of header text.
  'empty' => 1, // Show even when there are no results. Set to zero otherwise.
);
// This adds a header handler for a text area. The table is views because it is not
// a database field. The field is area because it is a text area.
// The header_options are the settings.
$view->add_item('DISPLAY_NAME', 'header', 'views', 'area', $header_options);
// This is the preferred way to get the rendered view.
$output = $view->preview('DISPLAY_NAME');
// If you want you can check for results with this.
if (!empty($view->result)) {
  // Do something if there are rows.
}

You can do the same for footer handlers:

$view->add_item('DISPLAY_NAME', 'footer', 'views', 'area', $footer_options);

and empty text:

$view->add_item('DISPLAY_NAME', 'empty', 'views', 'area', $empty_options);

You can also change the field type if you don't want to use Global: Text area.

For more information see $view->add_item()

3

You can do something like the following. I'm using some Views hook functions to interact with a View object -- you probably want to review a blog post tutorial on some ways to embed views:

  1. get a view
  2. assign a display
  3. add/remove filters
  4. embed the view

After (2) above you have a $view object you can modify essentially as I do below. Another good resource (I think) on embedding views can be seen in the Embed Views contrib module documentation page.

I just happen to have this code around, the first function alters view Filters. The second function alters the default empty text.

Remember to edit the particular $view[$display_id] and its $handlers to see your changes at run-time.

/**
 * Implementation of hook_views_pre_view().
 *
 * Change the set filter to be the current Quarter/Year set in the CCS admin area.
 */
function courses_db_views_pre_view(&$view, &$display_id, &$args) {
  // This is /DEPT/courses
  #dpm($view);
  if ($view->name == 'departmental_courses') {
    $filter =& $view->filter;

    $fall_tid = 11;
    $year_2012_tid = 26;
    // We created these _tid variables so that we didn't have to look up the TID
    // on every page view by students.
    $q = variable_get('ccs_current_quarter_tid', $fall_tid);
    $y = variable_get('ccs_current_year_tid', $year_2012_tid);

  $view->display[$display_id]->handler->options['filters']['field_coursedb_quarter_tid']['value'] = $q;
  $view->display[$display_id]->handler->options['filters']['field_coursedb_year_tid']['value'] = $y;
  }
  if ($view->name == 'courses_for_instructor') {
    #dpm($view->display['block']->handler, 'handler');
    $quarter = variable_get('ccs_current_quarter', 'Fall');
    $year = variable_get('ccs_current_year', '2012');

    $view->display['block']->handler->options['title'] = t('@quarter @year Instructor of:', array('@quarter' => $quarter, '@year' => $year));
    $view->args[0] = $year;
    $view->args[1] = $quarter;
    #dpm($view->display['block']->handler, 'handler');
  }

}

/**
 * Implementation of hook_views_post_execute().
 *
 * For a view listing of migrated course data, show original legacy
 * data in the view header to allow auditing/verification of the
 * migrated data.
 */
function courses_db_views_post_execute(&$view) {
  if ($view->name == 'migration_audits') {  # Courses

    $drupal_nids = array();
    if (count($view->result)) {
      foreach ($view->result as $i => $node) {
        $drupal_nids []= $node->nid;
      }
    }

    $ccs_sql = <<<EOFSQL
SELECT mccd.*
FROM ccs_dump.mos_ccs_courses_db mccd
LEFT JOIN ccs_dump.migrate_map_csscourse mmc ON mmc.sourceid1 = mccd.id
WHERE mmc.destid1 IN (:drupal_nids)
EOFSQL;
    $result = db_query($ccs_sql, array(':drupal_nids' => $drupal_nids));
    $output = '';
    foreach ($result as $course) {
      #dpm($course);
      $title = html_entity_decode($course->course_title, ENT_COMPAT, 'UTF-8');
      $instructors = html_entity_decode($course->instructor, ENT_COMPAT, 'UTF-8');
      $output .= "<div class=\"entry\">$course->quarter $course->year - $title Instructors:&nbsp;$instructors</div>";
    }


    $ccs_values = "<style type=\"text/css\">div.legacy_values { background-color: pink;} div.legacy_values div.entry { padding-bottom: 5px; }</style><div class=\"legacy_values\">$output</div>";
    $view->display['page']->handler->handlers['header']['area_1']->options['content'] = $ccs_values;
    $view->display['page']->handler->handlers['header']['area_1']->options['empty'] = 0;
  }
}
7
  • 1
    i go ahead with this but my footer doesn't display ,my variable is the right one..$footer_text = '<a href="/node/8" >Voir les archives</a>'; //Pour Views 3 $emptyTextVue_v3 = $view->handler->handlers['header']['area_1']->options['empty'] = TRUE; $FooterV3 = $view->display[$viewdisplay_ag5]->display_options['footer'] = "$footer_text";i think the issue is in the output of the view actually done with : $output = '<div id="other-sortie"><h3 class="CLASS_NAME">'.$view->get_title().'</h3>' .$view->preview($viewdisplay_ag5,$args_ag5).'</div>';
    – user8753
    Commented Nov 5, 2012 at 13:28
  • no need to change code for empty text..only footer/header seem to need change whith views 3
    – user8753
    Commented Nov 5, 2012 at 13:55
  • still there...your example is to complicated for me,i don't use a module, only an php includes files in a custom tpl file to embed my view..actually
    – user8753
    Commented Jan 15, 2013 at 10:12
  • still there...your example is to complicated for me,i don't use a module, only an php includes files in a custom tpl file to embed my view..actually i would to display header content and footer content from the views UI..
    – user8753
    Commented Jan 15, 2013 at 10:24
  • It doesnt matter if you use a module. You can call a view from any tpl file using PHP code. You want to set something like: $view->display['page']->handler->handlers['header']['area_1']->options['content'] = "your custom message"; if you want a footer you just set handlers[footer] i believe.
    – tenken
    Commented Jan 15, 2013 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.