Skip to content

Instantly share code, notes, and snippets.

@modcab
Last active November 8, 2016 15:10
Show Gist options
  • Save modcab/31b714f5b11c6632be7ce44cfb462cbf to your computer and use it in GitHub Desktop.
Save modcab/31b714f5b11c6632be7ce44cfb462cbf to your computer and use it in GitHub Desktop.
Drupal 8 - View - Add a footer programmatically.
<?php
/**
* Implements hook_views_pre_view().
*/
function projectname_views_pre_view(\Drupal\views\ViewExecutable $view, $display_id, array &$args) {
// Project - Assets - 'Add' link.
if ($view->id() == 'project_assets' && $display_id === 'block_1') {
$account = \Drupal::currentUser();
// If we're in a group.
if (($group = \Drupal::routeMatch()->getParameter('group')) &&
// And user has permissions to edit the group.
($group->hasPermission('edit group', $account))) {
$options = array(
'id' => 'area_text_custom',
'table' => 'views',
'field' => 'area_text_custom',
'relationship' => 'none',
'group_type' => 'none',
'admin_label' => '',
'empty' => TRUE,
'tokenize' => FALSE,
// Add an 'add' link for assets.
'content' => \Drupal\Core\Link::fromTextAndUrl(
t('Add'),
\Drupal\Core\Url::fromUri(
'internal:/group/' . $group->id() . '/content/add/group_node%3Alibrary_asset',
array('query' => [\Drupal::destination()->getAsArray() ])
)
)->toString(),
'plugin_id' => 'text_custom',
);
$view->setHandler($display_id, 'footer', 'area_text_custom', $options);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment