touchstone-1.0.x-dev/touchstone.install

touchstone.install
<?php

/**
 * @file
 * Install, update and uninstall functions for the touchstone installation profile.
 */

use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\shortcut\Entity\Shortcut;

/**
 * Implements hook_install_tasks().
 */
function touchstone_install_tasks(&$install_state) {
  return [
    'touchstone_install_content' => [],
    'touchstone_views_cleanup' => [],
    'touchstone_blocks_cleanup' => [],
  ];
}

/**
 * Modifies and creates content for the Touchstone profile.
 *
 * @throws \Drupal\Core\Entity\EntityStorageException
 *   If one of the entities cannot be saved.
 */
function touchstone_install_content(&$install_state) {
  // Assign user 1 the "administrator" role.
  $role = Role::load('administrator');
  if ($role) {
    $user = User::load(1);
    $user->addRole($role->id());
    $user->save();
  }

  // Shortcut module may not be installed if the site is being installed from
  // existing configuration.
  // @todo Also check for the default shortcut set, when
  //   https://www.drupal.org/project/drupal/issues/2083123 is fixed.
  if (\Drupal::moduleHandler()->moduleExists('shortcut')) {
    // Populate the default shortcut set.
    $shortcut = Shortcut::create([
      'shortcut_set' => 'default',
      'title' => t('Add content'),
      'weight' => -20,
      'link' => ['uri' => 'internal:/node/add'],
    ]);
    $shortcut->save();

    $shortcut = Shortcut::create([
      'shortcut_set' => 'default',
      'title' => t('All content'),
      'weight' => -19,
      'link' => ['uri' => 'internal:/admin/content'],
    ]);
    $shortcut->save();
  }

  // Create a Home page.
  $node_storage = \Drupal::service('entity_type.manager')->getStorage('node');
  $body = '<h1>Welcome to your new <a href="https://drupal.org" target="_blank">Drupal</a> site, customized with <a href="https://drupal.org/project/touchstone" target="_blank">Touchstone</a>.</h1><p>The login link has been removed in an attempt to keep the site clean. You can <a href="/user">click here to log in</a> or navigate directly to <code>/user</code>.</p><h2>About the Front Page</h2><p>This site uses <a href="/node/1/edit">a Page node</a> as its default front page. You can <a href="/admin/structure/types/manage/page/fields">customize the Page type</a> by configuring Layout, Paragraphs, or other styling to suit this and other pages. You could also use an entirely different method to handle your front page and <a href="/admin/config/system/site-information">update your front page settings</a> accordingly.</p>';
  $home = $node_storage->create([
    'type' => 'page',
    'title' => 'Front Page',
    'uid' => '1',
    'moderation_state' => 'published',
    'body' => [
      'format' => 'full_html',
      'value' => $body,
    ],
  ]);
  $home->setPublished();
  $home->save();
  $site_config = \Drupal::configFactory()->getEditable('system.site');
  $site_config->set('page.front', "/node/{$home->id()}");
  $site_config->save();

  // Create some default pages (in draft) with footer menu links.
  // The footer links will be in this order after contact.
  $default_pages = [
    'Privacy',
    'Terms',
    'Accessibility',
  ];
  $menu_link_storage = \Drupal::service('entity_type.manager')->getStorage('menu_link_content');
  foreach ($default_pages as $title) {
    $page = $node_storage->create([
      'type' => 'page',
      'title' => $title,
      'uid' => '1',
    ]);
    $page->save();
    $menu_link_storage->create([
      'title' => $title,
      'link' => ['uri' => "entity:node/{$page->id()}"],
      'menu_name' => 'footer',
      'weight' => $page->id(),
    ])->save();
  }
}

/**
 * Removes some default Views that come from user and node modules.
 */
function touchstone_views_cleanup(&$install_state) {
  $views_to_remove = [
    'who_s_online',
    'who_s_new',
    'glossary',
    'archive',
    'content_recent',
    'frontpage',
  ];
  $views_storage = \Drupal::service('entity_type.manager')->getStorage('view');
  foreach ($views_to_remove as $view_id) {
    $view = $views_storage->load($view_id);
    if ($view) {
      $view->delete();
    }
  }
}

/**
 * Removes some default Blocks that come from Olivero.
 */
function touchstone_blocks_cleanup(&$install_state) {
  $blocks_to_remove = [
    'olivero_powered',
    'olivero_help',
    'olivero_syndicate',
    'olivero_account_menu',
  ];
  $block_storage = \Drupal::service('entity_type.manager')->getStorage('block');
  foreach ($blocks_to_remove as $block_id) {
    $block = $block_storage->load($block_id);
    if ($block) {
      $block->delete();
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc