Skip to content

Instantly share code, notes, and snippets.

@yanniboi
Created January 20, 2016 17:03
Show Gist options
  • Save yanniboi/9816e6e7a44c8c3030de to your computer and use it in GitHub Desktop.
Save yanniboi/9816e6e7a44c8c3030de to your computer and use it in GitHub Desktop.
Drupal 8 views alter
<?php
/**
* @file
* Install file to handle module install and uninstall.
*/
/**
* Implements hook_install().
* Add an exposed filter for comment approval.
*/
function cmi_test_install() {
// Check to make sure views is enabled.
if (\Drupal::moduleHandler()->moduleExists('views')) {
// Get the view.
$view = \Drupal::entityTypeManager()->getStorage('view')->load('comments_recent');
// Add the filter.
$display =& $view->getDisplay('page');
$display['display_options']['filters']['status'] = [
'id' => 'status',
'table' => 'comment_field_data',
'field' => 'status',
'relationship' => 'none',
'group_type' => 'group',
'admin_label' => '',
'operator' => '=',
'value' => TRUE,
'group' => 1,
'exposed' => TRUE,
'expose' => [
'operator_id' => '',
'label' => 'Approved comment',
'description' => '',
'use_operator' => FALSE,
'operator' => 'status_op',
'identifier' => 'status',
'required' => TRUE,
'remember' => FALSE,
'multiple' => FALSE,
'remember_roles' => [
'authenticated' => 'authenticated',
'anonymous' => 0,
'administrator' => 0,
],
],
'is_grouped' => FALSE,
'group_info' => [
'label' => '',
'description' => '',
'identifier' => '',
'optional' => TRUE,
'widget' => 'select',
'multiple' => FALSE,
'remember' => FALSE,
'default_group' => 'All',
'default_group_multiple' => [],
'group_items' => [],
],
'plugin_id' => 'boolean',
'entity_type' => 'comment',
'entity_field' => 'status',
];
// Save.
$view->save();
}
}
/**
* Implements hook_uninstall().
* Remove an exposed filter for comment approval.
*/
function cmi_test_uninstall() {
// Check to make sure views is enabled.
if (\Drupal::moduleHandler()->moduleExists('views')) {
// Get the view.
$view = \Drupal::entityTypeManager()->getStorage('view')->load('comments_recent');
// Remove the filter.
$display = &$view->getDisplay('page');
unset($display['display_options']['filters']['status']);
// Save.
$view->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment