Any idea how I would get the view's title to show above the included view? I don't see any theme functions...

Comments

tanjerine’s picture

+1 on this please.

rhache’s picture

Category: support » feature

Same here. It would be nice as an option in the field configuration to show the View Title

Further more, I would recommend also having the option to show/hide view headers, footers & empty text in the viewfield config.

I have changed this to a Feature Request.

Thanks,
Rene

jax’s picture

Version: 5.x-1.5 » 6.x-1.x-dev
Category: feature » bug

When view field includes a view it doesn't display the title of the view.

jax’s picture

Status: Active » Needs review
StatusFileSize
new643 bytes

And here's a patch that conditionally inserts the title within a div. Maybe there are better classes to add, suggestions welcom.

darren oh’s picture

Status: Needs review » Closed (duplicate)

Issue 377796 applies to this.

wpanssi’s picture

Status: Closed (duplicate) » Active

This is still active. Viewsfield doesn't show the title of the view. The title of this issue queue is more descriptive than Issue 377796 so I think it's better to post here.

keithm’s picture

Title: Show view titles of included views » Show view title in viewfield output
Version: 6.x-1.x-dev » 6.x-2.x-dev
Component: Miscellaneous » Code
Category: bug » feature
keithm’s picture

Version: 6.x-2.x-dev » 7.x-2.x-dev
StatusFileSize
new625 bytes

Here is a patch that makes the title (if set) available to the theming layer. Then you can simply override theme_viewfield_formatter_default() using the #view_title key to display the title as you wish, for example:

function mytheme_viewfield_formatter_default($variables) {
  $element = $variables['element'];
  $view_title = $element['#view_title'];
  $view_output = $element['#view']->preview($element['#view_display'], $element['#view_arguments']);
  return '<div class="viewfield-wrapper"><h2 class="view-title">' . $view_title . '</h2><div class="view-output">' . $view_output . '</div></div>';
}
knalstaaf’s picture

I've applied this patch and the title still wouldn't show. Is that the only thing we should do, apply the patch, or do we have to do something more?

(You lost me at "Then you can simply override theme_viewfield_formatter_default() using "the #view_title key" to display the title as you wish".)

Edit: Ok, figured it out.

This code:

<?php
function mytheme_viewfield_formatter_default($variables) {
  $element = $variables['element'];
  $view_title = $element['#view_title'];
  $view_output = $element['#view']->preview($element['#view_display'], $element['#view_arguments']);
  return '<div class="viewfield-wrapper"><h2 class="view-title">' . $view_title . '</h2><div class="view-output">' . $view_output . '</div></div>';
}
?>

... goes around line 214 in viewfield.module. Thought that was the actual patch you were describing, sorry. All works now, thanks!

knalstaaf’s picture

Let's make those titles translatable: #1751350: Block titles not translated

nhck’s picture

Category: feature » bug

I have tried all of those solutions - and the error persits. I selected a block-type view from the list.

Yuri’s picture

Issue summary: View changes

I can't get it to work either.

ambrojio’s picture

So two things about the above solutions:

1. I don't see the View's title as #view_title directly inside the #element object. I had to dig around to find it, and eventually settled on:

$view_title = $element['#view']->display_handler->display->handler->view->display_handler->display->display_options['title'];

It's possible that there is another one, but that worked well for me.

2. Don't hack or patch the module! This is a theme function, so you can place it in your template.php file and change "mytheme" to your theme name. Don't forget to clear caches after you do this!

Gussees’s picture

OK, the patch works and show the 'title' set in View's Display, but when I want to change the 'title' in Views Display, Viewfield module doesn't show the change. The first value of title stayed glued.

That is, the value of title can not be changed. I'm using the modules:
Views 7.x-3.6 UI Views 7.x-3.6 and 7.x-2.0 Viewfield

Someone who can help me !

tobiberlin’s picture

In #8 the line to change has to be

'#view_title' => isset($view->display[$view_display]->display_title) ? $view->display[$view_display]->display_title: NULL,

Then it works

tobiberlin’s picture

Sorry, my mistake... this would output the title of the display as it is defined for administration page.

But I found another way: use $view->get_title();:

        $view = views_get_view($view_name);
        $view_title = $view->get_title();

        $elements[$delta] = array(
          '#type' => 'viewfield',
          '#access' => $view && $view->access($view_display),
          '#view' => $view,
          '#view_name' => $view_name,
          '#view_title' => !empty($view_title) ? $view_title : NULL,
          '#view_display' => $view_display,
          '#view_arguments' => $item['vargs'],
          '#entity_type' => $entity_type,
          '#entity_id' => $entity_id,
          '#entity' => $entity,
        );

viewfiled.module, line 131 ff.

Additionally you need to use the theming function in your theme's template.php as described in https://www.drupal.org/node/281409#comment-6085354

tobiberlin’s picture

Update: if you overwrite the titles for each display this still not works fine... it take the title of the default/ master display. Little adjusting:

        $view = views_get_view($view_name);
        $view->set_display($view_display);
        $view_title = $view->get_title();

        $elements[$delta] = array(
          '#type' => 'viewfield',
          '#access' => $view && $view->access($view_display),
          '#view' => $view,
          '#view_name' => $view_name,
          '#view_title' => !empty($view_title) ? $view_title : NULL,
          '#view_display' => $view_display,
          '#view_arguments' => $item['vargs'],
          '#entity_type' => $entity_type,
          '#entity_id' => $entity_id,
          '#entity' => $entity,
        );
clau_bolson’s picture

I have a view with a contextual filter content:nid and I use %1 as title, so it displays the node title as title. It is displayed well if I use the view alone. But when using viewfield all I get as title is "%1"
I have tried different combinations of the above. Can anyone tell me how I should rewrite these functions?

megachriz’s picture

Status: Active » Needs review
StatusFileSize
new1.41 KB

Here is a patch that combines the ideas from #17 and #9 with the following adjustments:

  1. The result of $view->get_title() is assigned to #view_title even if it is empty. I saw no good reason to explicitly set the value to NULL if it is empty. The theming layer can then decide for itself what to do with an empty value.
  2. The extra divs introduced in #9 are removed. The field will be already inside several divs, so I saw no reason to add more divs here.
  3. The title is wrapped inside a check_plain() for security reasons. Not entirely sure if that should happen in this case though as you need to have admin permissions already to edit a view.

Room for improvement: make it configurable to:

  1. Whether or not to show the title.
  2. Which HTML element should be used for the title. Right now it is a h2.
Anonymous’s picture

@MegaChriz's patch (#19) did the job perfectly for me, thanks.

+1 to getting this merged into the module

eloivaque’s picture

@MegaChriz's patch (#19) did the job perfectly for me, thanks.

drupalgin’s picture

Status: Needs review » Reviewed & tested by the community

Patch worked for me.

jerdavis’s picture

Status: Reviewed & tested by the community » Needs work

Before this patch could be merged we'd need to add the configuration option and default it to false. This may also require an update hook to ensure that the configuration option is set correctly for existing sites and to do whatever other updates are required. Merging the patch as-is would result in a significant and likely un-expected behavioral change for existing sites.

I'd also personally like to see the markup moved to a template or theme function that could be overridden rather than hard-coded in the formatter which would be more difficult to alter on a site-by-site basis.

jerdavis’s picture

Category: Bug report » Feature request
jerdavis’s picture

Version: 7.x-2.x-dev » 7.x-3.x-dev