mynotes-8.x-1.3/mynotes.install
mynotes.install
<?php
/**
* @file
* Contains mynotes.install.
*/
use Drupal\Core\Session\AccountInterface;
use Drupal\block\Entity\Block;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\facets\Entity\Facet;
use Drupal\views\Entity\View;
use Drupal\search_api\Entity\Server;
use Drupal\search_api\Entity\Index;
/**
* Implements hook_install().
*/
function mynotes_install() {
// Make 'notes' default front page.
\Drupal::configFactory()
->getEditable('system.site')
->set('page.front', '/notes')
->save(TRUE);
// Grant access to notes to all authenticated users.
user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, ['access mynotes']);
// Remove search block.
$block = Block::load('bartik_search');
if ($block) {
$block->delete();
}
// Remove tools block.
$block = Block::load('bartik_tools');
if ($block) {
$block->delete();
}
// Add menu items to the main menu.
$menu_items = [
'Add Note' => '/node/add/note',
'Labels' => '/admin/structure/taxonomy/manage/labels/overview',
];
foreach ($menu_items as $title => $link) {
$weight = 1;
$menu_link = MenuLinkContent::create([
'title' => $title,
'link' => ['uri' => 'internal:' . $link],
'menu_name' => 'main',
'expanded' => TRUE,
'weight' => $weight,
]);
$weight++;
$menu_link->save();
}
// Clear all plugin caches.
\Drupal::service('plugin.cache_clearer')->clearCachedDefinitions();
// Rebuild the menu router to ensure the menu links are valid.
\Drupal::service('router.builder')->rebuild();
}
/**
* Implements hook_uninstall().
*/
function mynotes_uninstall() {
// Set default front page.
\Drupal::configFactory()
->getEditable('system.site')
->set('page.front', '/node')
->save(TRUE);
// Remove all module menu items.
$menu_items = MenuLinkContent::loadMultiple();
foreach ($menu_items as $menu_item) {
if ($menu_item->getMenuName() == 'main' && in_array($menu_item->getTitle(), ['Add Note', 'Labels'])) {
$menu_item->delete();
}
}
// Remove all module blocks.
$module_blocks = [
'stared',
'archived',
'labels',
'views_block__recent_notes_block_1',
];
foreach ($module_blocks as $module_block) {
$block = Block::load($module_block);
if ($block) {
$block->delete();
}
}
// Remove all module facets.
$module_facets = [
'stared',
'archived',
'labels',
];
foreach ($module_facets as $module_facet) {
$facet = Facet::load($module_facet);
if ($facet) {
$facet->delete();
}
}
// Remove 'Notes' view.
$view = View::load('notes');
if ($view) {
$view->delete();
}
// Remove 'Recent notes' view.
$view = View::load('recent_notes');
if ($view) {
$view->delete();
}
// Remove Search API server.
$server = Server::load('notes_server');
if ($server) {
$server->delete();
}
// Remove Search API index.
$index = Index::load('notes_index');
if ($index) {
$index->delete();
}
drupal_flush_all_caches();
}
