1

I need add a footer to a view based on certain conditions. I followed this article and as per my understanding this should work. But this is not giving me any result.

Here is my code:

function utility_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
  $item = array(
    'id' => 'area_text_custom',
    'table' => 'views',
    'field' => 'area_text_custom',
    'relationship' => 'none',
    'group_type' => 'none',
    'admin_label' => '',
    'empty' => NULL,
    'tokenize' => NULL,
    'content' => array(
      'value' => "Footer content is great.",
      'format' => "basic_html",
    ),
    'plugin_id' => 'text',
  );
  $view->setHandler($display_id, 'footer', 'area_text_custom', $item);
}

Update: Using the above hook the footer is getting called but renders as Array

  • based certain conditions like what? – No Sssweat Jun 29 '16 at 6:14
  • The conditions isn't important. That can be handled. But the problem is even the footer isn't showing up anyway. (Without any condition) – Malabya Tewari Jun 29 '16 at 6:24
1

I was able to add a Views footer with some code in a custom module.

use Drupal\views\ViewExecutable;
function YOURMODULENAME_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
  if ($view->id() == 'view_machine_name' && $display_id === 'view_display') {
    $options = array(
      'id' => 'area_text_custom',
      'table' => 'views',
      'field' => 'area_text_custom',
      'relationship' => 'none',
      'group_type' => 'none',
      'admin_label' => '',
      'empty' => TRUE,
      'tokenize' => FALSE,
      'content' => ‘Footer content is great.’,
      'plugin_id' => 'text_custom',
    );
    $view->setHandler('view_display', 'footer', 'area_text_custom', $options);
  }
}

Reference: Drupal 8: Programmatically Adding a Views Footer

  • Thanks Sanjay, I was missing out the content and making it as an array for which the render was messed up. – Malabya Tewari Jun 29 '16 at 11:19
  • If it really helps you. Mark vote up for this answer... Thanks :) – Sanjay Jun 29 '16 at 12:06
1

User area instead of area_text_custom, below solutions is working for me

$item = array(
  'id' => 'area',
  'table' => 'views',
  'field' => 'area',
  'relationship' => 'none',
  'group_type' => 'none',
  'admin_label' => '',
  'empty' => NULL,
  'tokenize' => NULL,
  'content' => array(
    'value' => 'Footer Text goes here',
    'format' => "full_html",
  ),
  'plugin_id' => 'text',
);

$view->setHandler('display_name', 'footer', 'area', $item); 
1

You can attach a few other types of content also. Here is an example for a paragraph entity, but any entity type will be similiar.

/**
 * Implements hook_views_pre_view().
 */
function mytheme_views_pre_view(ViewExecutable $view, $display_id, array &$args)
{
  if ($view->id() == 'my-view-id') {

    // Load your entity however you want.
    $paragraph_entity = '[my-entity]'; 

    $item = [
      'id' => 'area',
      'table' => 'views',
      'field' => 'entity_paragraph',
      'empty' => TRUE,
      'target' => $paragraph_entity->uuid(),
    ];

    // This can be header, footer, etc. See ViewExecutable::getHandlerTypes();
    $type = 'header'; 

    // Handler ID. 
    $id = 'paragraph_entity';

    // Finally, attach the handler to the view.
    $view->setHandler($display_id, $type, $id, $item);

  }
} 

To understand better how to interface with handlers, see core/modules/views/src/ViewExecutable.php

view->getHandler() and view->removeHandler() are also useful.

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.