You are here

public function DemoSystem::createContent in Open Social 10.2.x

Same name and namespace in other branches
  1. 8.9 modules/custom/social_demo/src/DemoSystem.php \Drupal\social_demo\DemoSystem::createContent()
  2. 8 modules/custom/social_demo/src/DemoSystem.php \Drupal\social_demo\DemoSystem::createContent()
  3. 8.2 modules/custom/social_demo/src/DemoSystem.php \Drupal\social_demo\DemoSystem::createContent()
  4. 8.3 modules/custom/social_demo/src/DemoSystem.php \Drupal\social_demo\DemoSystem::createContent()
  5. 8.4 modules/custom/social_demo/src/DemoSystem.php \Drupal\social_demo\DemoSystem::createContent()
  6. 8.5 modules/custom/social_demo/src/DemoSystem.php \Drupal\social_demo\DemoSystem::createContent()
  7. 8.6 modules/custom/social_demo/src/DemoSystem.php \Drupal\social_demo\DemoSystem::createContent()
  8. 8.7 modules/custom/social_demo/src/DemoSystem.php \Drupal\social_demo\DemoSystem::createContent()
  9. 8.8 modules/custom/social_demo/src/DemoSystem.php \Drupal\social_demo\DemoSystem::createContent()
  10. 10.3.x modules/custom/social_demo/src/DemoSystem.php \Drupal\social_demo\DemoSystem::createContent()
  11. 10.0.x modules/custom/social_demo/src/DemoSystem.php \Drupal\social_demo\DemoSystem::createContent()
  12. 10.1.x modules/custom/social_demo/src/DemoSystem.php \Drupal\social_demo\DemoSystem::createContent()

Creates content.

Return value

array An array with list of created entities.

Overrides DemoContentInterface::createContent

File

modules/custom/social_demo/src/DemoSystem.php, line 83

Class

DemoSystem
Class DemoSystem.

Namespace

Drupal\social_demo

Code

public function createContent($generate = FALSE, $max = NULL) {

  // Fetch data from yml file.
  $data = $this
    ->fetchData();
  if ($generate === TRUE) {
    $data = $this
      ->scrambleData($data, $max);
  }

  // First let's load the active theme.
  $active_theme = \Drupal::theme()
    ->getActiveTheme()
    ->getName();

  // Site.
  if (isset($data['site'])) {
    $this->content['site'] = TRUE;

    // Get editable config.
    $config = $this->configFactory
      ->getEditable('system.site');

    // Set the site name and save.
    $config
      ->set('name', $data['site']['name'])
      ->save();
  }

  // Homepage block.
  if (isset($data['homepage'])) {
    $this->content['homepage'] = TRUE;

    // This uuid can be used like this since it's defined
    // in the code as well (@see social_core.install).
    $block = $this->blockStorage
      ->loadByProperties([
      'uuid' => '8bb9d4bb-f182-4afc-b138-8a4b802824e4',
    ]);
    $block = current($block);
    if ($block instanceof BlockContent) {
      $this
        ->replaceAnBlock($block, $data['homepage']);
    }
  }

  // Theme settings.
  if (isset($data['theme'])) {
    $this->content['theme'] = TRUE;

    // Get theme settings.
    $config = $this->configFactory
      ->getEditable($active_theme . '.settings');

    // Favicon.
    if (isset($data['theme']['favicon'])) {
      $favicon = [
        'mimetype' => $data['theme']['favicon']['mimetype'],
        'path' => $data['theme']['favicon']['path'],
        'url' => $data['theme']['favicon']['url'],
        'use_default' => FALSE,
      ];

      // And save it.
      $config
        ->set('favicon', $favicon)
        ->save();
    }

    // Logo.
    $logo = $this
      ->fetchImage($data['theme']['logo']);

    // Must be a valid file.
    if ($logo instanceof File) {
      $theme_logo = [
        'path' => $logo
          ->getFileUri(),
        'url' => file_create_url($logo
          ->getFileUri()),
        'use_default' => FALSE,
      ];

      // Store the array.
      $config
        ->set('logo', $theme_logo)
        ->save();
    }

    // Font.
    $config
      ->set('font_primary', $this
      ->getOrCreateFont($data['theme']['font_primary']))
      ->save();

    // Borders.
    $config
      ->set('card_radius', $data['theme']['card_radius'])
      ->save();
    $config
      ->set('form_control_radius', $data['theme']['form_control_radius'])
      ->save();
    $config
      ->set('button_radius', $data['theme']['button_radius'])
      ->save();

    // Get the colors.
    $color = $this->configFactory
      ->getEditable('color.theme.' . $active_theme);

    // Set as a palette.
    $palette = [
      'brand-primary' => $data['theme']['color_primary'],
      'brand-secondary' => $data['theme']['color_secondary'],
      'brand-accent' => $data['theme']['color_accents'],
      'brand-link' => $data['theme']['color_link'],
      'navbar-bg' => $data['theme']['navbar-bg'],
      'navbar-text' => $data['theme']['navbar-text'],
      'navbar-active-bg' => $data['theme']['navbar-active-bg'],
      'navbar-active-text' => $data['theme']['navbar-active-text'],
      'navbar-sec-bg' => $data['theme']['navbar-sec-bg'],
      'navbar-sec-text' => $data['theme']['navbar-sec-text'],
    ];

    // Save the palette.
    $color
      ->set('palette', $palette)
      ->save();

    /*
     * The code below has been stolen from the color.module. This module makes
     * no use of services or any other way to make its code a bit more
     * re-usable. Therefore a rip/copy was needed.
     *
     * The code makes sure that the above enforced color configuration is
     * in fact applied.
     */

    // Define the library name(s).
    $libraries = [
      'assets/css/brand.css',
    ];
    $id = $active_theme . '-' . substr(hash('sha256', serialize($palette) . microtime()), 0, 8);
    $paths['color'] = 'public://color';
    $paths['target'] = $paths['color'] . '/' . $id;
    foreach ($paths as $path) {
      \Drupal::service('file_system')
        ->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY);
    }
    $paths['target'] = $paths['target'] . '/';
    $paths['id'] = $id;
    $paths['source'] = drupal_get_path('theme', $active_theme) . '/';
    $paths['files'] = $paths['map'] = [];
    $css = [];
    foreach ($libraries as $stylesheet) {

      // Build a temporary array with CSS files.
      $files = [];
      if (file_exists($paths['source'] . $stylesheet)) {
        $files[] = $stylesheet;
      }
      foreach ($files as $file) {
        $css_optimizer = new CssOptimizer();

        // Aggregate @imports recursively for each configured top level
        // CSS file without optimization.
        // Aggregation and optimization will be handled by
        // drupal_build_css_cache() only.
        $style = $css_optimizer
          ->loadFile($paths['source'] . $file, FALSE);

        // Return the path to where this CSS file originated from, stripping
        // off the name of the file at the end of the path.
        $css_optimizer->rewriteFileURIBasePath = base_path() . dirname($paths['source'] . $file) . '/';

        // Prefix all paths within this CSS file, ignoring absolute paths.
        $style = preg_replace_callback('/url\\([\'"]?(?![a-z]+:|\\/+)([^\'")]+)[\'"]?\\)/i', [
          $css_optimizer,
          'rewriteFileURI',
        ], $style);

        // Rewrite stylesheet with new colors.
        $style = _color_rewrite_stylesheet($active_theme, $info, $paths, $palette, $style);
        $base_file = $this->fileSystem
          ->basename($file);
        $css[] = $paths['target'] . $base_file;
        _color_save_stylesheet($paths['target'] . $base_file, $style, $paths);
      }
    }

    // Maintain list of files.
    $color
      ->set('stylesheets', $css)
      ->set('files', $paths['files'])
      ->save();
  }

  // Return something.
  return $this->content;
}